<?php

namespace App\Http\Controllers\Component4;

use App\Http\Controllers\Controller;
use App\Models\Component4\C4StakeholderEngagement;
use Illuminate\Http\Request;
use Illuminate\View\View;

class C4StakeholdersEngagementController extends Controller
{
    public function index(Request $request): View
    {
        $query = C4StakeholderEngagement::query();

        if ($request->filled('state')) {
            $query->where('state_province', $request->state);
        }
        if ($request->filled('gender')) {
            $query->where('gender', $request->gender);
        }
        if ($request->filled('component')) {
            $query->where('component_of_engagement', $request->component);
        }

        $engagements = $query->latest('date_of_engagement')->paginate(15);

        // Quick stats for the page
        $stats = [
            'total' => C4StakeholderEngagement::count(),
            'total_male' => C4StakeholderEngagement::where('gender', 'male')->count(),
            'total_female' => C4StakeholderEngagement::where('gender', 'female')->count(),
            'by_component' => C4StakeholderEngagement::selectRaw('component_of_engagement, COUNT(*) as count')
                ->groupBy('component_of_engagement')->pluck('count', 'component_of_engagement'),
            'by_category' => [
                'project_launch' => C4StakeholderEngagement::where('project_launch', true)->count(),
                'learning_event' => C4StakeholderEngagement::where('learning_event', true)->count(),
                'partners_forum' => C4StakeholderEngagement::where('partners_forum', true)->count(),
                'orientation' => C4StakeholderEngagement::where('orientation', true)->count(),
                'validation_meetings' => C4StakeholderEngagement::where('validation_meetings', true)->count(),
                'consultations' => C4StakeholderEngagement::where('consultations', true)->count(),
                'others' => C4StakeholderEngagement::where('others', true)->count(),
            ]
        ];

        return view('component4.stakeholdersengagement.index', compact('engagements', 'stats'));
    }

    public function create(): View
    {
        return view('component4.stakeholdersengagement.create');
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'organization' => 'nullable|string|max:255',
            'designation' => 'nullable|string|max:255',
            'gender' => 'nullable|in:male,female,other',
            'state_province' => 'nullable|string|max:100',
            'date_of_engagement' => 'required|date',
            'component_of_engagement' => 'nullable|string|max:100',
            'project_launch' => 'boolean|nullable',
            'learning_event' => 'boolean|nullable',
            'partners_forum' => 'boolean|nullable',
            'orientation' => 'boolean|nullable',
            'validation_meetings' => 'boolean|nullable',
            'consultations' => 'boolean|nullable',
            'others' => 'boolean|nullable',
            'comment_remark' => 'nullable|string',
        ]);

        C4StakeholderEngagement::create($validated);

        return redirect()->route('c4.stakeholders.index')
            ->with('success', 'Stakeholder engagement registered successfully!');
    }

    //edit
    public function edit(C4StakeholderEngagement $engagement): View
    {
        return view('component4.stakeholdersengagement.edit', compact('engagement'));
    }

    //show
    public function show(C4StakeholderEngagement $engagement): View
    {
        return view('component4.stakeholdersengagement.show', compact('engagement'));
    }

    // You can add report() method later if you want a dedicated route
}