<?php

namespace App\Http\Controllers;

use App\Models\Department;
use App\Models\KeyResultArea;
use App\Models\KpiLibrary;
use Illuminate\Http\Request;
use Inertia\Inertia;

class KpiLibraryController extends Controller
{
    public function index(Request $request)
    {
        $kpis = KpiLibrary::with(['keyResultArea:id,name', 'department:id,name'])
            ->when($request->search, fn($q, $s) => $q->where('name', 'like', "%{$s}%"))
            ->when($request->kra, fn($q, $k) => $q->where('key_result_area_id', $k))
            ->when($request->department, fn($q, $d) => $q->where('department_id', $d))
            ->orderBy('name')
            ->paginate(20)
            ->withQueryString();

        return Inertia::render('KPILibrary/Index', [
            'kpis' => $kpis,
            'filters' => $request->only('search', 'kra', 'department'),
            'kras' => KeyResultArea::active()->select('id', 'name')->orderBy('name')->get(),
            'departments' => Department::active()->select('id', 'name')->orderBy('name')->get(),
        ]);
    }

    public function create()
    {
        return Inertia::render('KPILibrary/Form', [
            'kras' => KeyResultArea::active()->select('id', 'name')->orderBy('name')->get(),
            'departments' => Department::active()->select('id', 'name')->orderBy('name')->get(),
        ]);
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
            'unit_of_measurement' => 'nullable|string|max:100',
            'key_result_area_id' => 'nullable|exists:key_result_areas,id',
            'department_id' => 'nullable|exists:departments,id',
            'is_active' => 'boolean',
        ]);

        KpiLibrary::create($validated);

        return redirect()->route('kpi-library.index')->with('success', 'KPI created successfully.');
    }

    public function edit(KpiLibrary $kpiLibrary)
    {
        return Inertia::render('KPILibrary/Form', [
            'kpi' => $kpiLibrary,
            'kras' => KeyResultArea::active()->select('id', 'name')->orderBy('name')->get(),
            'departments' => Department::active()->select('id', 'name')->orderBy('name')->get(),
        ]);
    }

    public function update(Request $request, KpiLibrary $kpiLibrary)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
            'unit_of_measurement' => 'nullable|string|max:100',
            'key_result_area_id' => 'nullable|exists:key_result_areas,id',
            'department_id' => 'nullable|exists:departments,id',
            'is_active' => 'boolean',
        ]);

        $kpiLibrary->update($validated);

        return redirect()->route('kpi-library.index')->with('success', 'KPI updated successfully.');
    }

    public function destroy(KpiLibrary $kpiLibrary)
    {
        $kpiLibrary->delete();
        return redirect()->route('kpi-library.index')->with('success', 'KPI deleted successfully.');
    }
}
