<?php
namespace App\Http\Controllers\C3;

use App\Http\Controllers\Controller;
use App\Models\C3CaseManagement;
use Illuminate\Http\Request;

class CaseManagementController extends Controller
{
    public function index()
    {
        $cases = C3CaseManagement::latest()->paginate(10);
        return view('c3.case_management.index', compact('cases'));
    }

    public function create()
    {
        return view('c3.case_management.create');
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'incident_id' => 'required|string|unique:c3_case_managements,incident_id',
            'survivor_code' => 'nullable|string|max:50',
            'case_manager_code' => 'nullable|string|max:50',
            'date_of_report' => 'nullable|date',
            'date_of_incident' => 'nullable|date',
            'date_of_birth' => 'nullable|date',
            'sex' => 'nullable|in:F,M',
            'ethnicity' => 'nullable|string',
            'country_of_origin' => 'nullable|string',
            'civil_marital_status' => 'nullable|string',
            'displacement_status_at_report' => 'nullable|string',
            'person_with_disability' => 'boolean',
            'unaccompanied_or_separated_child' => 'boolean',
            'stage_of_displacement_at_incident' => 'nullable|string',
            'incident_time_of_day' => 'nullable|string',
            'incident_location' => 'nullable|string',
            'incident_area' => 'nullable|string',
            'incident_sub_area' => 'nullable|string',
            'incident_camp_town' => 'nullable|string',
            'gbv_type' => 'nullable|string',
            'harmful_traditional_practice' => 'boolean',
            'money_goods_services_exchanged' => 'boolean',
            'type_of_abduction' => 'nullable|string',
            'previously_reported_this_incident' => 'boolean',
            'previous_gbv_incidents' => 'boolean',
            'number_of_alleged_perpetrators' => 'nullable|integer',
            'alleged_perpetrator_sex' => 'nullable|in:M,F',
            'nationality_of_alleged_perpetrator' => 'nullable|string',
            'alleged_perpetrator_age_group' => 'nullable|string',
            'alleged_perpetrator_relationship' => 'nullable|string',
            'main_occupation_of_alleged_perpetrator' => 'nullable|string',
            'referred_to_you_from' => 'nullable|string',
            'safe_house_shelter' => 'nullable|string',
            'health_medical_services' => 'nullable|string',
            'psychosocial_services' => 'nullable|string',
            'wants_legal_action' => 'boolean',
            'legal_assistance_services' => 'nullable|string',
            'police_security_actor' => 'nullable|string',
            'livelihoods_program' => 'nullable|string',
            'child_protection_services' => 'nullable|string',
            'nfis' => 'nullable|string',
            'consent_given_for_information_sharing' => 'boolean',
        ]);

        C3CaseManagement::create($validated);

        return redirect()->route('c3.case-management.index')
            ->with('success', 'Case Management record created successfully.');
    }

    public function edit(C3CaseManagement $case)
    {
        return view('c3.case_management.edit', compact('case'));
    }

    public function update(Request $request, C3CaseManagement $case)
    {
        $validated = $request->validate([ /* same validation as store, remove unique rule for update */ ]);
        $case->update($validated);

        return redirect()->route('c3.case-management.index')
            ->with('success', 'Case updated successfully.');
    }

    public function destroy(C3CaseManagement $case)
    {
        $case->delete();
        return redirect()->route('c3.case-management.index')
            ->with('success', 'Case deleted successfully.');
    }
}