<?php

namespace App\Modules\Pilkades\Http\Controllers\AdminDesa;

use App\Http\Controllers\Controller;
use App\Modules\Pilkades\Models\Candidate;
use App\Modules\Pilkades\Models\CandidateProgram;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class CandidateProgramController extends Controller
{
    public function store(Request $request, Candidate $candidate): RedirectResponse
    {
        $this->authorize('update', $candidate);

        $data = $request->validate([
            'judul_program' => ['required', 'string', 'max:255'],
            'deskripsi' => ['nullable', 'string'],
            'urutan' => ['nullable', 'integer'],
        ]);

        $candidate->programs()->create($data);

        return back()->with('success', 'Program kerja berhasil ditambahkan.');
    }

    public function destroy(CandidateProgram $program): RedirectResponse
    {
        $this->authorize('update', $program->candidate);

        $program->delete();

        return back()->with('success', 'Program kerja berhasil dihapus.');
    }
}
