import { NextFunction, Request, Response } from "express";
import { CatchAsyncError } from "./Catch";
import { IpService } from "../controllers/ip/ip.service";

export const InternetProtocolChecker = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {

    let ipaddress = req.headers['x-forwarded-for'] || req.socket.remoteAddress || null

    if (!ipaddress) {
        return res.status(200).json({ success: true, restricated: true })
    }

    const findIP = await IpService.getIpByIPAddress(ipaddress as string);


    if (!findIP) {
        return res.status(200).json({ success: true, restricated: true })
    }
    return res.status(200).json({ success: true, restricated: false })
})