import { NextFunction, Request, Response } from "express";
import { CatchAsyncError } from "./Catch";
import { ApiError } from "./ApiError";
import { IpService } from "../controllers/ip/ip.service";
import { ApiResponse } from "./ApiResponse";

export const InternetProtocol = CatchAsyncError(async (req: Request, res: Response, next: NextFunction) => {

    let ipaddress = req.headers['x-forwarded-for'] || req.socket.remoteAddress || null

    console.log(ipaddress)

    if (!ipaddress) {
        return res.status(200).json(
            new ApiResponse("success", 501, [])
        )
    }

    // const allIps = await IpService.getIP();

    const findIP = await IpService.getIpByIPAddress(ipaddress as string);

    // if (allIps.length > 0) {

    if (!findIP) {
        return res.status(200).json(
            new ApiResponse("success", 501, [])
        )
    }

    // }

    next()
})