<?php

namespace App\Controllers\Admin;

use App\Core\Upload;
use App\Models\AgendaModel;

class AgendaController extends AdminController
{
    public function index(): void
    {
        $this->adminView('agenda/index', [
            'daftarAgenda' => (new AgendaModel())->getAllAdmin(),
        ]);
    }

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

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

        [$data, $uploadError] = $this->prepareData();

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

        (new AgendaModel())->insert($data);

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

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

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

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

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

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

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

        [$data, $uploadError] = $this->prepareData();

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

        if (!empty($data['gambar']) && !empty($existing['gambar'])) {
            Upload::delete('agenda', $existing['gambar']);
        }

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

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

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

        $model = new AgendaModel();
        $agenda = $model->find((int) $id);

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

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

    private function prepareData(): array
    {
        $uploadError = null;
        $gambar = Upload::image($_FILES['gambar'] ?? [], 'agenda', $uploadError);

        $data = [
            'judul'           => trim($_POST['judul'] ?? ''),
            'deskripsi'       => trim($_POST['deskripsi'] ?? ''),
            'lokasi'          => trim($_POST['lokasi'] ?? ''),
            'tanggal_mulai'   => !empty($_POST['tanggal_mulai']) ? str_replace('T', ' ', $_POST['tanggal_mulai']) . ':00' : date('Y-m-d H:i:s'),
            'tanggal_selesai' => !empty($_POST['tanggal_selesai']) ? str_replace('T', ' ', $_POST['tanggal_selesai']) . ':00' : null,
        ];

        if ($gambar) {
            $data['gambar'] = $gambar;
        }

        return [$data, $uploadError];
    }
}
