import { NextFunction, Request, Response } from "express";
import { CatchAsyncError } from "../../middleware/Catch";
import { Iwebsite } from "../../interface/websites";
import { WebsiteService } from "./website.service";
import { ApiResponse } from "../../middleware/ApiResponse";
import { ApiError } from "../../middleware/ApiError";
import { getAuth } from '@clerk/express'

export const createWebsite = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {



    const data = req.body as Iwebsite;




    const response = await WebsiteService.createWebsite(data);

    return res.status(200).json(
        new ApiResponse("Websites Created", 201, [])
    )

})



export const getAllWebsites = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {

    const response = await WebsiteService.getAllWebsites();



    return res.status(200).json(
        new ApiResponse("Websites All", 201, response)
    )

})


export const getWebsiteById = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {

    const id = req.params.webId as string;;

    const response = await WebsiteService.getUniqueWebsites(id);

    return res.status(200).json(
        new ApiResponse("Websites All", 201, response)
    )

})



export const updateWebsites = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {


    const data = req.body as Iwebsite;
    const id = req.params.webId as string;;

    if (!id) {
        throw new ApiError("Id not Found", 404);
    }


    const uniqueWebsite = await WebsiteService.getUniqueWebsites(id);

    if (!uniqueWebsite) {
        throw new ApiError("Web not Found", 404);
    }



    const response = await WebsiteService.updateWebsite(data, id);

    return res.status(200).json(
        new ApiResponse("Websites Updated", 201, [])
    )

})