import { NextFunction, Request, Response } from "express";
import { CatchAsyncError } from "../../middleware/Catch";
import { ApiResponse } from "../../middleware/ApiResponse";
import { Links } from "./links.service";
import { BlogLinks } from "./links.validation";
import { ApiError } from "../../middleware/ApiError";

export const getAllBlogsLinks = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {

    const response = await Links.getAllLinkService();

    return res.status(200).json(
        new ApiResponse(
            "Success",
            200,
            response
        )
    )
})

export const getAllBlogsLinksById = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {

    const id = req.params.webId;

    const response = await Links.getLinksById(id);

    return res.status(200).json(
        new ApiResponse(
            "Success",
            200,
            response
        )
    )
})


export const createBlogsLinks = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {

    const data = req.body as BlogLinks

    const response = await Links.createBlogsLinks(data);

    return res.status(200).json(
        new ApiResponse(
            "Success",
            200,
            response
        )
    )
})


export const updateBlogsLinks = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {

    const id = req.params.webId;
    const data = req.body as BlogLinks

    const findId = await Links.getLinksById(id);

    if (!findId) {
        throw new ApiError("Links not Found !!", 404);
    }

    const response = await Links.updateBlogsLinks(data, id);

    return res.status(200).json(
        new ApiResponse(
            "Success",
            200,
            response
        )
    )

})


export const deleteBlogsLinks = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {

    const id = req.params.webId;

    const findId = await Links.getLinksById(id);

    if (!findId) {
        throw new ApiError("Links not Found !!", 404);
    }

    const response = await Links.deleteLinks(id);

    return res.status(200).json(
        new ApiResponse(
            "Success",
            200,
            response
        )
    )

})