<?php

declare(strict_types=1);

/**
 * Front controller public (racine = public_html, D-17).
 * Toutes les requêtes non-fichier sont routées ici par le .htaccess.
 */

// Sous le serveur intégré PHP (dev), laisser servir les fichiers statiques existants.
if (PHP_SAPI === 'cli-server') {
    $path = parse_url((string) ($_SERVER['REQUEST_URI'] ?? '/'), PHP_URL_PATH);
    if (is_string($path) && $path !== '/' && is_file(__DIR__ . $path)) {
        return false;
    }
}

require __DIR__ . '/src/bootstrap.php';

use App\Core\Config;
use App\Core\Request;
use App\Core\Router;
use App\Core\Session;
use App\Controllers\HomeController;
use App\Controllers\GalleryController;
use App\Controllers\ContactController;
use App\Controllers\SeoController;
use App\Controllers\Admin\RedirectController as AdminRedirectController;
use App\Controllers\Admin\AuthController;
use App\Controllers\Admin\DashboardController;
use App\Controllers\Admin\GalleryController as AdminGalleryController;
use App\Controllers\Admin\ImageController as AdminImageController;
use App\Controllers\Admin\PageController as AdminPageController;
use App\Controllers\Admin\SettingsController as AdminSettingsController;
use App\Controllers\Admin\AccountController as AdminAccountController;

// Erreurs : affichées uniquement en debug
$debug = (bool) Config::get('app.debug', false);
error_reporting(E_ALL);
ini_set('display_errors', $debug ? '1' : '0');

Session::start();

// Routes
$router = new Router();
$router->get('/', [HomeController::class, 'index']);

// Back-office
$router->get('/admin', [DashboardController::class, 'index']);
$router->get('/admin/login', [AuthController::class, 'showLogin']);
$router->post('/admin/login', [AuthController::class, 'login']);
$router->post('/admin/logout', [AuthController::class, 'logout']);

// Back-office — galeries
$router->get('/admin/galleries', [AdminGalleryController::class, 'index']);
$router->get('/admin/galleries/new', [AdminGalleryController::class, 'create']);
$router->post('/admin/galleries', [AdminGalleryController::class, 'store']);
$router->post('/admin/galleries/reorder', [AdminGalleryController::class, 'reorder']);
$router->get('/admin/galleries/{id}/edit', [AdminGalleryController::class, 'edit']);
$router->post('/admin/galleries/{id}', [AdminGalleryController::class, 'update']);
$router->post('/admin/galleries/{id}/delete', [AdminGalleryController::class, 'destroy']);
$router->post('/admin/galleries/{id}/move', [AdminGalleryController::class, 'move']);
$router->post('/admin/galleries/{id}/status', [AdminGalleryController::class, 'status']);

// Back-office — images d'une galerie
$router->get('/admin/galleries/{gid}/images', [AdminImageController::class, 'index']);
$router->post('/admin/galleries/{gid}/images', [AdminImageController::class, 'upload']);
$router->post('/admin/galleries/{gid}/images/reorder', [AdminImageController::class, 'reorder']);
$router->post('/admin/galleries/{gid}/images/{id}', [AdminImageController::class, 'update']);
$router->post('/admin/galleries/{gid}/images/{id}/replace', [AdminImageController::class, 'replace']);
$router->post('/admin/galleries/{gid}/images/{id}/status', [AdminImageController::class, 'status']);
$router->post('/admin/galleries/{gid}/images/{id}/delete', [AdminImageController::class, 'destroy']);
$router->post('/admin/galleries/{gid}/images/{id}/move', [AdminImageController::class, 'move']);

// Back-office — pages
$router->get('/admin/pages', [AdminPageController::class, 'index']);
$router->post('/admin/pages/upload-image', [AdminPageController::class, 'uploadImage']);
$router->get('/admin/pages/{id}/edit', [AdminPageController::class, 'edit']);
$router->post('/admin/pages/{id}', [AdminPageController::class, 'update']);

// Back-office — paramètres + thème
$router->get('/admin/settings', [AdminSettingsController::class, 'edit']);
$router->post('/admin/settings', [AdminSettingsController::class, 'update']);
$router->post('/admin/settings/test-email', [AdminSettingsController::class, 'testEmail']);
$router->get('/admin/account', [AdminAccountController::class, 'edit']);
$router->post('/admin/account/password', [AdminAccountController::class, 'updatePassword']);

// Back-office — redirections
$router->get('/admin/redirects', [AdminRedirectController::class, 'index']);
$router->post('/admin/redirects', [AdminRedirectController::class, 'store']);
$router->post('/admin/redirects/{id}/delete', [AdminRedirectController::class, 'destroy']);

// SEO
$router->get('/sitemap.xml', [SeoController::class, 'sitemap']);
$router->get('/robots.txt', [SeoController::class, 'robots']);

// Contact public (si activé)
$router->get('/contact', [ContactController::class, 'show']);
$router->post('/contact', [ContactController::class, 'submit']);

// Galerie publique (slug) — doit rester en dernier (attrape-tout 1 segment)
$router->get('/{slug}', [GalleryController::class, 'show']);

// Dispatch
try {
    $response = $router->dispatch(Request::fromGlobals());
} catch (\Throwable $e) {
    if ($debug) {
        throw $e;
    }
    http_response_code(500);
    echo 'Une erreur est survenue.';
    exit;
}

$response->send();
