<?php

namespace App\Http\Controllers;

use App\Models\Thema;
use App\Services\ThemaService;
use Illuminate\Http\Request;
use Inertia\Inertia;

class ThemaController extends Controller
{
    public function __construct(
        private ThemaService $themaService
    ) {}

    public function index()
    {
        return Inertia::render('Thema/Index', [
            'themas' => $this->themaService->getAll(),
        ]);
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'naam' => 'required|string|max:255',
            'beschrijving' => 'nullable|string',
            'prioriteit' => 'nullable|string',
            'periode_start' => 'nullable|date',
            'periode_eind' => 'nullable|date|after:periode_start',
        ]);

        $this->themaService->create($validated);

        return back()->with('success', 'Thema aangemaakt.');
    }

    public function update(Request $request, Thema $thema)
    {
        $validated = $request->validate([
            'naam' => 'sometimes|string|max:255',
            'beschrijving' => 'nullable|string',
            'prioriteit' => 'nullable|string',
            'periode_start' => 'nullable|date',
            'periode_eind' => 'nullable|date',
        ]);

        $this->themaService->update($thema, $validated);

        return back()->with('success', 'Thema bijgewerkt.');
    }
}
