import { NextFunction, Request, Response } from "express";
import { CatchAsyncError } from "../../middleware/Catch";
import { IpService } from "./ip.service";
import { ApiResponse } from "../../middleware/ApiResponse";
import { ApiError } from "../../middleware/ApiError";
import { IP } from "../../interface/ip";

export const getAllIP = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {
    const response = await IpService.getIP();
    return res.status(200).json(
        new ApiResponse(
            "Success",
            200,
            response
        )
    )
})


export const createIP = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {


    const body = req.body as IP

    const checker = await IpService.getIpByIPAddress(body.ip_address);

    if (checker) {
        throw new ApiError("Ip Already Added", 403);
    }

    const creteIPAddress = await IpService.createIP(body);

    return res.status(201).json(
        new ApiResponse("IP added", 201, [])
    )
});


export const updateIP = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {

    const id = req.params.ipId as string

    const body = req.body as IP


    const checker = await IpService.getIpByID(id);

    if (!checker) {
        throw new ApiError("Ip Not Found", 400);
    }

    const update = await IpService.updateIP(body, id);

    return res.status(200).json(
        new ApiResponse("Ip Updated Successfully", 201, [])
    )

});



export const deleteIp = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {

    const id = req.params.ipId as string

    const checker = await IpService.getIpByID(id);

    if (!checker) {
        throw new ApiError("Ip Not Found", 400);
    }

    const deleteIp = await IpService.deleteIP(id);

    return res.status(200).json(
        new ApiResponse("IP Deleted", 200, [])
    )

})