<?php

namespace App\Controllers\Admin;

use App\Models\KategoriBeritaModel;
use App\Core\Slug;

class KategoriBeritaController extends AdminController
{
    public function index(): void
    {
        $this->adminView('kategori/index', [
            'kategori' => (new KategoriBeritaModel())->all(),
        ]);
    }

    public function store(): void
    {
        $this->verifyCsrf();

        $nama = trim($_POST['nama'] ?? '');

        if ($nama !== '') {
            (new KategoriBeritaModel())->insert([
                'nama' => $nama,
                'slug' => Slug::make($nama),
            ]);
            $this->setFlash('success', 'Kategori berhasil ditambahkan.');
        }

        $this->redirect('admin/kategori-berita');
    }

    public function destroy(string $id): void
    {
        $this->verifyCsrf();

        (new KategoriBeritaModel())->delete((int) $id);

        $this->setFlash('success', 'Kategori berhasil dihapus. Berita dengan kategori ini jadi tanpa kategori.');
        $this->redirect('admin/kategori-berita');
    }
}
