<?php

namespace App\Http\Controllers\Admin;

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

class WorkerController extends Controller
{
    /**
     * Display a listing of all workers.
     */
    public function index()
    {
        // Scaffolding mock dataset for worker management
        $workers = [
            [
                'id' => 101,
                'name' => 'Maria Santos',
                'email' => 'maria.santos@example.com',
                'nationality' => 'Philippines',
                'category' => 'Childcare',
                'experience' => '5+ Years',
                'status' => 'active',
                'joined_at' => '2026-01-12',
            ],
            [
                'id' => 102,
                'name' => 'Lakshmi Sharma',
                'email' => 'l.sharma@example.com',
                'nationality' => 'India',
                'category' => 'Elderly Care',
                'experience' => '3-5 Years',
                'status' => 'active',
                'joined_at' => '2026-02-05',
            ],
            [
                'id' => 103,
                'name' => 'Siti Aminah',
                'email' => 'siti.a@example.com',
                'nationality' => 'Indonesia',
                'category' => 'Housekeeping',
                'experience' => '2 Years',
                'status' => 'inactive',
                'joined_at' => '2026-03-18',
            ],
            [
                'id' => 104,
                'name' => 'Fatima Zahra',
                'email' => 'fatima.z@example.com',
                'nationality' => 'Morocco',
                'category' => 'Cooking',
                'experience' => '8 Years',
                'status' => 'active',
                'joined_at' => '2026-04-02',
            ],
            [
                'id' => 105,
                'name' => 'Grace Omondi',
                'email' => 'grace.o@example.com',
                'nationality' => 'Kenya',
                'category' => 'General Helper',
                'experience' => '4 Years',
                'status' => 'active',
                'joined_at' => '2026-04-25',
            ],
        ];

        return Inertia::render('Admin/Workers/Index', [
            'workers' => $workers,
        ]);
    }

    /**
     * Toggle worker active/inactive status.
     */
    public function toggleStatus(Request $request, $id)
    {
        $validated = $request->validate([
            'status' => 'required|in:active,inactive,suspended,banned',
        ]);

        return back()->with('success', "Worker status has been updated to {$validated['status']}.");
    }

    /**
     * Override worker availability.
     */
    public function availabilityOverride(Request $request, $id)
    {
        $validated = $request->validate([
            'availability' => 'required|string',
        ]);

        return back()->with('success', "Worker availability has been overridden to '{$validated['availability']}' successfully.");
    }

    /**
     * Flag worker as fraudulent or suspicious.
     */
    public function flagFraud(Request $request, $id)
    {
        $validated = $request->validate([
            'is_fraud' => 'required|boolean',
            'reason' => 'nullable|string'
        ]);

        $statusStr = $validated['is_fraud'] ? 'FLAGGED FOR FRAUD' : 'UNFLAGGED';

        return back()->with('success', "Worker #{$id} has been {$statusStr}. Notes: {$validated['reason']}");
    }

    /**
     * Moderation profile updates.
     */
    public function updateProfile(Request $request, $id)
    {
        $validated = $request->validate([
            'name' => 'required|string',
            'category' => 'required|string',
            'experience' => 'required|string',
            'bio' => 'nullable|string'
        ]);

        return back()->with('success', "Worker #{$id} profile details moderated and updated successfully.");
    }

    /**
     * Verify an employer's company details.
     */
    public function verifyEmployer(Request $request, $id)
    {
        return back()->with('success', "Employer #{$id} verification status set to APPROVED.");
    }

    /**
     * Suspend employer.
     */
    public function suspendEmployer(Request $request, $id)
    {
        $validated = $request->validate([
            'reason' => 'nullable|string'
        ]);

        return back()->with('success', "Employer #{$id} has been suspended. Reason: " . ($validated['reason'] ?? 'Unspecified violation'));
    }
}
