<?php

require_once __DIR__ . '/../config/config.php';

/**
 * Autoloader sederhana (PSR-4-ish) untuk namespace App\
 */
spl_autoload_register(function (string $class): void {
    $prefix = 'App\\';

    if (str_starts_with($class, $prefix)) {
        $relative = substr($class, strlen($prefix));
        $file     = BASE_PATH . '/app/' . str_replace('\\', '/', $relative) . '.php';

        if (file_exists($file)) {
            require $file;
        }
    }
});

use App\Core\Router;

$router = new Router();

// ==== Rute publik ====
$router->get('/', 'HomeController@index');

// ==== Panel Admin ====
$router->get('admin/login', 'Admin\\AuthController@loginForm');
$router->post('admin/login', 'Admin\\AuthController@login');
$router->get('admin/logout', 'Admin\\AuthController@logout');

$router->get('admin', 'Admin\\DashboardController@index');

$router->get('admin/menu', 'Admin\\MenuController@index');
$router->get('admin/menu/create', 'Admin\\MenuController@create');
$router->post('admin/menu', 'Admin\\MenuController@store');
$router->get('admin/menu/{id}/edit', 'Admin\\MenuController@edit');
$router->post('admin/menu/{id}', 'Admin\\MenuController@update');
$router->post('admin/menu/{id}/delete', 'Admin\\MenuController@destroy');

$router->get('admin/berita', 'Admin\\BeritaController@index');
$router->get('admin/berita/create', 'Admin\\BeritaController@create');
$router->post('admin/berita', 'Admin\\BeritaController@store');
$router->get('admin/berita/{id}/edit', 'Admin\\BeritaController@edit');
$router->post('admin/berita/{id}', 'Admin\\BeritaController@update');
$router->post('admin/berita/{id}/delete', 'Admin\\BeritaController@destroy');

$router->get('admin/kategori-berita', 'Admin\\KategoriBeritaController@index');
$router->post('admin/kategori-berita', 'Admin\\KategoriBeritaController@store');
$router->post('admin/kategori-berita/{id}/delete', 'Admin\\KategoriBeritaController@destroy');

$router->get('admin/identitas', 'Admin\\IdentitasController@edit');
$router->post('admin/identitas', 'Admin\\IdentitasController@update');

$router->get('admin/halaman', 'Admin\\HalamanController@index');
$router->get('admin/halaman/create', 'Admin\\HalamanController@create');
$router->post('admin/halaman', 'Admin\\HalamanController@store');
$router->get('admin/halaman/{id}/edit', 'Admin\\HalamanController@edit');
$router->post('admin/halaman/{id}', 'Admin\\HalamanController@update');
$router->post('admin/halaman/{id}/delete', 'Admin\\HalamanController@destroy');

$router->get('admin/agenda', 'Admin\\AgendaController@index');
$router->get('admin/agenda/create', 'Admin\\AgendaController@create');
$router->post('admin/agenda', 'Admin\\AgendaController@store');
$router->get('admin/agenda/{id}/edit', 'Admin\\AgendaController@edit');
$router->post('admin/agenda/{id}', 'Admin\\AgendaController@update');
$router->post('admin/agenda/{id}/delete', 'Admin\\AgendaController@destroy');

$router->get('admin/galeri-foto', 'Admin\\GaleriFotoController@index');
$router->get('admin/galeri-foto/create', 'Admin\\GaleriFotoController@create');
$router->post('admin/galeri-foto', 'Admin\\GaleriFotoController@store');
$router->get('admin/galeri-foto/{id}/edit', 'Admin\\GaleriFotoController@edit');
$router->post('admin/galeri-foto/{id}', 'Admin\\GaleriFotoController@update');
$router->post('admin/galeri-foto/{id}/delete', 'Admin\\GaleriFotoController@destroy');
$router->post('admin/galeri-foto/{id}/foto', 'Admin\\GaleriFotoController@tambahFoto');
$router->post('admin/galeri-foto/{id}/foto/{fotoId}/delete', 'Admin\\GaleriFotoController@hapusFoto');

$router->get('admin/galeri-video', 'Admin\\GaleriVideoController@index');
$router->get('admin/galeri-video/create', 'Admin\\GaleriVideoController@create');
$router->post('admin/galeri-video', 'Admin\\GaleriVideoController@store');
$router->get('admin/galeri-video/{id}/edit', 'Admin\\GaleriVideoController@edit');
$router->post('admin/galeri-video/{id}', 'Admin\\GaleriVideoController@update');
$router->post('admin/galeri-video/{id}/delete', 'Admin\\GaleriVideoController@destroy');

$router->get('admin/fasilitas', 'Admin\\FasilitasController@index');
$router->get('admin/fasilitas/create', 'Admin\\FasilitasController@create');
$router->post('admin/fasilitas', 'Admin\\FasilitasController@store');
$router->get('admin/fasilitas/{id}/edit', 'Admin\\FasilitasController@edit');
$router->post('admin/fasilitas/{id}', 'Admin\\FasilitasController@update');
$router->post('admin/fasilitas/{id}/delete', 'Admin\\FasilitasController@destroy');

$router->get('admin/widget', 'Admin\\WidgetController@index');
$router->get('admin/widget/create', 'Admin\\WidgetController@create');
$router->post('admin/widget', 'Admin\\WidgetController@store');
$router->get('admin/widget/{id}/edit', 'Admin\\WidgetController@edit');
$router->post('admin/widget/{id}', 'Admin\\WidgetController@update');
$router->post('admin/widget/{id}/delete', 'Admin\\WidgetController@destroy');

$router->get('admin/footer', 'Admin\\FooterController@index');
$router->get('admin/footer/create', 'Admin\\FooterController@create');
$router->post('admin/footer', 'Admin\\FooterController@store');
$router->post('admin/footer/warna-latar', 'Admin\\FooterController@updateWarnaLatar');
$router->get('admin/footer/{id}/edit', 'Admin\\FooterController@edit');
$router->post('admin/footer/{id}', 'Admin\\FooterController@update');
$router->post('admin/footer/{id}/delete', 'Admin\\FooterController@destroy');
$router->get('admin/footer/{id}/tautan', 'Admin\\FooterController@tautan');
$router->post('admin/footer/{id}/tautan/tambah', 'Admin\\FooterController@tambahTautan');
$router->post('admin/footer/{id}/tautan/{linkId}/delete', 'Admin\\FooterController@hapusTautan');

// ==== Rute publik: Halaman Statis ====
$router->get('halaman/{slug}', 'HalamanController@show');

// ==== Rute publik: Berita ====
$router->get('berita', 'BeritaController@index');
$router->get('berita/{slug}', 'BeritaController@show');

// ==== Rute publik: Agenda ====
$router->get('agenda', 'AgendaController@index');

// ==== Rute publik: Galeri ====
$router->get('galeri-foto', 'GaleriController@foto');
$router->get('galeri-foto/{id}', 'GaleriController@fotoShow');
$router->get('galeri-video', 'GaleriController@video');
$router->get('fasilitas', 'FasilitasController@index');

$router->dispatch($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']);
