<?php

declare(strict_types=1);

namespace App\Models;

use App\Core\Database;
use PDO;

/**
 * Pages éditoriales pour le back-office (accueil, mentions légales…).
 */
final class PageRepository
{
    /** Pages « système » garanties présentes. */
    private const SYSTEM = [
        'home'             => ['title' => 'Accueil',          'status' => 'published', 'noindex' => 0],
        'mentions-legales' => ['title' => 'Mentions légales', 'status' => 'draft',     'noindex' => 1],
    ];

    public static function ensureSystemPages(): void
    {
        $pdo = Database::connection();
        foreach (self::SYSTEM as $slug => $def) {
            $stmt = $pdo->prepare('SELECT id FROM pages WHERE slug = :slug');
            $stmt->execute(['slug' => $slug]);
            if ($stmt->fetchColumn() === false) {
                $pdo->prepare(
                    "INSERT INTO pages (slug, title, body_html, status, seo_noindex)
                     VALUES (:slug, :title, '', :status, :noindex)"
                )->execute([
                    'slug'    => $slug,
                    'title'   => $def['title'],
                    'status'  => $def['status'],
                    'noindex' => $def['noindex'],
                ]);
            }
        }
    }

    /** @return list<array<string,mixed>> */
    public static function all(): array
    {
        $pdo = Database::connection();
        return $pdo->query(
            "SELECT id, slug, title, status FROM pages ORDER BY FIELD(slug, 'home', 'mentions-legales'), id"
        )->fetchAll();
    }

    /** @return array<string,mixed>|null */
    public static function find(int $id): ?array
    {
        $pdo  = Database::connection();
        $stmt = $pdo->prepare('SELECT * FROM pages WHERE id = :id');
        $stmt->execute(['id' => $id]);
        $row = $stmt->fetch();
        return $row === false ? null : $row;
    }

    /** @return list<string> slugs publiés et indexables (hors accueil), pour le sitemap. */
    public static function publishedIndexableSlugs(): array
    {
        $pdo  = Database::connection();
        $rows = $pdo->query(
            "SELECT slug FROM pages WHERE status = 'published' AND seo_noindex = 0 AND slug <> 'home' ORDER BY slug"
        )->fetchAll(PDO::FETCH_COLUMN);
        return is_array($rows) ? $rows : [];
    }

    /** @param array<string,mixed> $data */
    public static function update(int $id, array $data): void
    {
        $pdo  = Database::connection();
        $stmt = $pdo->prepare(
            "UPDATE pages SET title = :title, body_html = :body, status = :status,
                    seo_title = :seo_title, seo_description = :seo_description, seo_noindex = :seo_noindex
             WHERE id = :id"
        );
        $stmt->execute([
            'title'           => $data['title'],
            'body'            => $data['body_html'],
            'status'          => $data['status'],
            'seo_title'       => $data['seo_title'],
            'seo_description' => $data['seo_description'],
            'seo_noindex'     => $data['seo_noindex'],
            'id'              => $id,
        ]);
    }
}
