<?php

namespace App\Http\Controllers;

use App\Models\Commitment;
use App\Services\CommitmentService;
use Illuminate\Http\Request;

class CommitmentController extends Controller
{
    public function __construct(
        private CommitmentService $commitmentService
    ) {}

    public function store(Request $request)
    {
        $validated = $request->validate([
            'beschrijving' => 'required|string',
            'eigenaar_id'  => 'required|exists:users,id',
            'deadline'     => 'required|date',
            'project_id'   => 'required|exists:projects,id',
            'prioriteit'   => 'nullable|string',
            'bron'         => 'nullable|string',
            'besluit_id'   => 'nullable|exists:besluiten,id',
        ]);

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

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

    public function update(Request $request, Commitment $commitment)
    {
        $validated = $request->validate([
            'beschrijving' => 'sometimes|required|string',
            'eigenaar_id'  => 'sometimes|required|exists:users,id',
            'deadline'     => 'sometimes|required|date',
            'prioriteit'   => 'nullable|string',
            'bron'         => 'nullable|string',
            'status'       => 'nullable|string',
        ]);

        $this->commitmentService->update($commitment, $validated);

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

    public function complete(Commitment $commitment)
    {
        $this->commitmentService->markComplete($commitment);

        return back()->with('success', 'Commitment afgerond.');
    }

    public function destroy(Commitment $commitment)
    {
        $commitment->delete();

        return back()->with('success', 'Commitment verwijderd.');
    }
}
