<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Inertia\Inertia;

class WorkerVerificationController extends Controller
{
    /**
     * Display a listing of worker identity verifications.
     */
    public function index(Request $request)
    {
        $status = $request->input('status', 'all');

        // Scaffolding mock dataset supporting pagination and status filtering
        $allVerifications = [
            [
                'id' => 101,
                'worker_name' => 'Fatima Zahra',
                'nationality' => 'Morocco',
                'passport_number' => 'MA9823471',
                'processed_at' => '2026-05-14 10:15',
                'status' => 'approved',
                'verification_method' => 'Auto-OCR',
                'document_images' => [
                    'https://images.unsplash.com/photo-1544717305-2782549b5136?q=80&w=600&auto=format&fit=crop',
                    'https://images.unsplash.com/photo-1573496359142-b8d87734a5a2?q=80&w=600&auto=format&fit=crop'
                ],
                'ocr_data' => [
                    'Name' => 'Fatima Zahra',
                    'DOB' => '1992-08-14',
                    'Nationality' => 'Morocco',
                    'Passport No.' => 'MA9823471',
                    'Expiry' => '2030-11-20',
                ]
            ],
            [
                'id' => 102,
                'worker_name' => 'Maria Santos',
                'nationality' => 'Philippines',
                'passport_number' => 'P1234567A',
                'processed_at' => '2026-05-14 09:30',
                'status' => 'approved',
                'verification_method' => 'Auto-OCR',
                'document_images' => [
                    'https://images.unsplash.com/photo-1580489944761-15a19d654956?q=80&w=600&auto=format&fit=crop'
                ],
                'ocr_data' => [
                    'Name' => 'Maria Santos',
                    'DOB' => '1990-05-10',
                    'Nationality' => 'Philippines',
                    'Passport No.' => 'P1234567A',
                    'Expiry' => '2029-04-15',
                ]
            ],
            [
                'id' => 103,
                'worker_name' => 'Amina Diop',
                'nationality' => 'Senegal',
                'passport_number' => 'SN8765432',
                'processed_at' => '2026-05-13 14:20',
                'status' => 'approved',
                'verification_method' => 'Auto-OCR',
                'document_images' => [
                    'https://images.unsplash.com/photo-1531746020798-e6953c6e8e04?q=80&w=600&auto=format&fit=crop'
                ],
                'ocr_data' => [
                    'Name' => 'Amina Diop',
                    'DOB' => '1994-12-01',
                    'Nationality' => 'Senegal',
                    'Passport No.' => 'SN8765432',
                    'Expiry' => '2031-08-10',
                ]
            ],
            [
                'id' => 104,
                'worker_name' => 'Siti Aminah',
                'nationality' => 'Indonesia',
                'passport_number' => 'B76543210',
                'processed_at' => '2026-05-13 11:05',
                'status' => 'approved',
                'verification_method' => 'Auto-OCR',
                'document_images' => [
                    'https://images.unsplash.com/photo-1534528741775-53994a69daeb?q=80&w=600&auto=format&fit=crop'
                ],
                'ocr_data' => [
                    'Name' => 'Siti Aminah',
                    'DOB' => '1988-03-25',
                    'Nationality' => 'Indonesia',
                    'Passport No.' => 'B76543210',
                    'Expiry' => '2028-09-30',
                ]
            ],
            [
                'id' => 105,
                'worker_name' => 'Grace Omondi',
                'nationality' => 'Kenya',
                'passport_number' => 'KE5432198',
                'processed_at' => '2026-05-12 16:45',
                'status' => 'approved',
                'verification_method' => 'Auto-OCR',
                'document_images' => [
                    'https://images.unsplash.com/photo-1567532939604-b6b5b0db2604?q=80&w=600&auto=format&fit=crop'
                ],
                'ocr_data' => [
                    'Name' => 'Grace Omondi',
                    'DOB' => '1995-07-19',
                    'Nationality' => 'Kenya',
                    'Passport No.' => 'KE5432198',
                    'Expiry' => '2025-02-14',
                ]
            ],
            [
                'id' => 106,
                'worker_name' => 'Leila Bekri',
                'nationality' => 'Tunisia',
                'passport_number' => 'TN4321987',
                'processed_at' => '2026-05-12 10:10',
                'status' => 'approved',
                'verification_method' => 'Auto-OCR',
                'document_images' => [
                    'https://images.unsplash.com/photo-1544005313-94ddf0286df2?q=80&w=600&auto=format&fit=crop'
                ],
                'ocr_data' => [
                    'Name' => 'Leila Bekri',
                    'DOB' => '1991-09-08',
                    'Nationality' => 'Tunisia',
                    'Passport No.' => 'TN4321987',
                    'Expiry' => '2032-01-25',
                ]
            ],
        ];

        // Filter by status
        if ($status !== 'all') {
            $filtered = array_filter($allVerifications, function ($item) use ($status) {
                return $item['status'] === $status;
            });
        } else {
            $filtered = $allVerifications;
        }

        $page = $request->input('page', 1);
        $perPage = 20;
        $total = count($filtered);
        $slice = array_slice($filtered, ($page - 1) * $perPage, $perPage);

        // Generate paginator
        $paginator = new LengthAwarePaginator($slice, $total, $perPage, $page, [
            'path' => $request->url(),
            'query' => $request->query(),
        ]);

        return Inertia::render('Admin/Workers/Verifications', [
            'verifications' => $paginator,
            'status' => $status,
        ]);
    }

    /**
     * Process verification approval or rejection.
     */
    public function verify(Request $request, $worker)
    {
        $validated = $request->validate([
            'action' => 'required|in:approve,reject',
            'rejection_reason' => 'required_if:action,reject|nullable|string',
            'ocr_overrides' => 'nullable|array',
        ]);

        $statusMsg = $validated['action'] === 'approve' ? 'approved' : 'rejected';

        return back()->with('success', "Worker verification #{$worker} has been {$statusMsg} successfully.");
    }
}
