<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\FailedJobsModel;
use App\Models\JobModel;

class MailQueueController extends Controller
{
    // This method handles the API request and checks the IP
    public function getJobRecord(Request $request)
    {
        // return;
        $allowedIps = ['127.0.0.1', '102.88.36.185', '198.252.102.78', '198.252.105.16'];
        
        $clientIp = $request->ip();

        if (!in_array($clientIp, $allowedIps)) {
            return response()->json(['error' => 'Forbidden: Invalid IP'], 403);
        }

        $record = JobModel::all(); 

        if ($record) {
            return response()->json(['data' => $record], 200);
        } else {
            return response()->json(['error' => 'Record not found'], 404);
        }
    }
    
    public function sanitizeJobRecord(Request $request)
    {
        $allowedIps = ['127.0.0.1', '102.88.36.185', '198.252.102.78', '198.252.105.16'];
        
        $clientIp = $request->ip();

        if (!in_array($clientIp, $allowedIps)) {
            return response()->json(['error' => 'Forbidden: Invalid IP'], 403);
        }

        $ids = $request->input('ids');
        if (is_array($ids) || !empty($ids)) {
            $deleted = JobModel::whereIn('id', $ids)->delete();

            return response()->json(['message' => 'Job deleted'], 200);
        }
    }
}
