<?php

namespace App\Http\Controllers;

use App\Models\Child;
use App\Models\PendingChange;
use App\Models\Spouse;
use App\Models\Staff;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class SelfServiceController extends Controller
{
    public function show(string $token): Response
    {
        $staff = Staff::where('self_service_token', $token)
            ->with('company')
            ->firstOrFail();

        $spouses  = Spouse::where('staff_id', $staff->id)->get();
        $children = Child::where('staff_id', $staff->id)->orderBy('date_of_birth')->get();

        return Inertia::render('SelfService/Show', [
            'staff'    => $staff,
            'spouses'  => $spouses,
            'children' => $children,
            'token'    => $token,
        ]);
    }

    public function submitContactUpdate(Request $request, string $token)
    {
        $staff = Staff::where('self_service_token', $token)->firstOrFail();

        $data = $request->validate([
            'phone'         => 'nullable|string|max:50',
            'mobile_no'     => 'nullable|string|max:50',
            'personal_email' => 'nullable|email|max:255',
            'address_line1' => 'nullable|string|max:255',
            'address_line2' => 'nullable|string|max:255',
            'city'          => 'nullable|string|max:100',
            'state'         => 'nullable|string|max:100',
            'postcode'      => 'nullable|string|max:20',
            'notes'         => 'nullable|string|max:1000',
        ]);

        $notes  = $data['notes'] ?? null;
        $fields = collect($data)->except('notes')->filter()->toArray();

        if (empty($fields)) {
            return redirect()->back()->with('error', 'No changes provided.');
        }

        PendingChange::create([
            'staff_id'     => $staff->id,
            'company_id'   => $staff->company_id,
            'change_type'  => 'update',
            'table_name'   => 'staff',
            'record_id'    => $staff->id,
            'field_changes' => $fields,
            'staff_notes'  => $notes,
            'status'       => 'pending',
        ]);

        return redirect()->back()->with('success', 'Your contact information update has been submitted for HR review.');
    }

    public function submitSpouse(Request $request, string $token)
    {
        $staff = Staff::where('self_service_token', $token)->firstOrFail();

        if (Spouse::where('staff_id', $staff->id)->count() >= 4) {
            return redirect()->back()->with('error', 'Maximum of 4 spouses allowed.');
        }

        $data = $request->validate([
            'full_name'     => 'required|string|max:255',
            'ic_no'         => 'nullable|string|max:50',
            'date_of_birth' => 'nullable|date',
            'nationality'   => 'nullable|string|max:100',
            'occupation'    => 'nullable|string|max:255',
            'phone'         => 'nullable|string|max:50',
            'marriage_date' => 'nullable|date',
            'notes'         => 'nullable|string|max:1000',
        ]);

        $notes  = $data['notes'] ?? null;
        $fields = collect($data)->except('notes')->toArray();

        PendingChange::create([
            'staff_id'     => $staff->id,
            'company_id'   => $staff->company_id,
            'change_type'  => 'create',
            'table_name'   => 'spouses',
            'record_id'    => null,
            'field_changes' => $fields,
            'staff_notes'  => $notes,
            'status'       => 'pending',
        ]);

        return redirect()->back()->with('success', 'Spouse information submitted for HR review.');
    }

    public function submitChild(Request $request, string $token)
    {
        $staff = Staff::where('self_service_token', $token)->firstOrFail();

        $data = $request->validate([
            'full_name'     => 'required|string|max:255',
            'ic_no'         => 'nullable|string|max:50',
            'date_of_birth' => 'nullable|date',
            'gender'        => 'nullable|in:male,female',
            'is_dependent'  => 'boolean',
            'is_student'    => 'boolean',
            'notes'         => 'nullable|string|max:1000',
        ]);

        $notes  = $data['notes'] ?? null;
        $fields = collect($data)->except('notes')->toArray();

        PendingChange::create([
            'staff_id'     => $staff->id,
            'company_id'   => $staff->company_id,
            'change_type'  => 'create',
            'table_name'   => 'children',
            'record_id'    => null,
            'field_changes' => $fields,
            'staff_notes'  => $notes,
            'status'       => 'pending',
        ]);

        return redirect()->back()->with('success', 'Child information submitted for HR review.');
    }
}
