<?php

namespace App\Controllers\Admin;

use App\Core\Upload;
use App\Models\FasilitasModel;

class FasilitasController extends AdminController
{
    public function index(): void
    {
        $this->adminView('fasilitas/index', [
            'daftarFasilitas' => (new FasilitasModel())->all(),
        ]);
    }

    public function create(): void
    {
        $this->adminView('fasilitas/form', ['fasilitas' => null]);
    }

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

        $uploadError = null;
        $gambar = Upload::image($_FILES['gambar'] ?? [], 'fasilitas', $uploadError);

        if ($uploadError) {
            $this->setFlash('error', $uploadError);
            $this->redirect('admin/fasilitas/create');
            return;
        }

        (new FasilitasModel())->insert([
            'nama'      => trim($_POST['nama'] ?? ''),
            'deskripsi' => trim($_POST['deskripsi'] ?? ''),
            'icon'      => trim($_POST['icon'] ?? ''),
            'urutan'    => (int) ($_POST['urutan'] ?? 0),
            'gambar'    => $gambar,
        ]);

        $this->setFlash('success', 'Fasilitas berhasil ditambahkan.');
        $this->redirect('admin/fasilitas');
    }

    public function edit(string $id): void
    {
        $fasilitas = (new FasilitasModel())->find((int) $id);

        if (!$fasilitas) {
            $this->setFlash('error', 'Fasilitas tidak ditemukan.');
            $this->redirect('admin/fasilitas');
            return;
        }

        $this->adminView('fasilitas/form', ['fasilitas' => $fasilitas]);
    }

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

        $model = new FasilitasModel();
        $existing = $model->find((int) $id);

        if (!$existing) {
            $this->setFlash('error', 'Fasilitas tidak ditemukan.');
            $this->redirect('admin/fasilitas');
            return;
        }

        $uploadError = null;
        $gambar = Upload::image($_FILES['gambar'] ?? [], 'fasilitas', $uploadError);

        if ($uploadError) {
            $this->setFlash('error', $uploadError);
            $this->redirect('admin/fasilitas/' . $id . '/edit');
            return;
        }

        $data = [
            'nama'      => trim($_POST['nama'] ?? ''),
            'deskripsi' => trim($_POST['deskripsi'] ?? ''),
            'icon'      => trim($_POST['icon'] ?? ''),
            'urutan'    => (int) ($_POST['urutan'] ?? 0),
        ];

        if ($gambar) {
            Upload::delete('fasilitas', $existing['gambar'] ?? null);
            $data['gambar'] = $gambar;
        }

        $model->update((int) $id, $data);

        $this->setFlash('success', 'Fasilitas berhasil diperbarui.');
        $this->redirect('admin/fasilitas');
    }

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

        $model = new FasilitasModel();
        $fasilitas = $model->find((int) $id);

        if ($fasilitas) {
            Upload::delete('fasilitas', $fasilitas['gambar'] ?? null);
            $model->delete((int) $id);
        }

        $this->setFlash('success', 'Fasilitas berhasil dihapus.');
        $this->redirect('admin/fasilitas');
    }
}
