<?php

class ReservationRequest
{
    public static function getAll(): array
    {
        return Database::get()->query(
            'SELECT r.*, s.name AS subnet_name
             FROM app_reservation_requests r
             LEFT JOIN app_subnets s ON r.subnet_id = s.id
             ORDER BY r.created_at ASC'
        )->fetchAll();
    }

    public static function getById(int $id): ?array
    {
        $stmt = Database::get()->prepare(
            'SELECT r.*, s.name AS subnet_name
             FROM app_reservation_requests r
             LEFT JOIN app_subnets s ON r.subnet_id = s.id
             WHERE r.id = ?'
        );
        $stmt->execute([$id]);
        $row = $stmt->fetch();
        return $row ?: null;
    }

    public static function add(array $data): int
    {
        $db = Database::get();
        $db->prepare(
            'INSERT INTO app_reservation_requests
             (requester_name, requester_email, mac_address, subnet_id, description)
             VALUES (?, ?, ?, ?, ?)'
        )->execute([
            trim($data['requester_name']),
            trim($data['requester_email']),
            trim($data['mac_address']),
            (int)$data['subnet_id'],
            trim($data['description'] ?? '') ?: null,
        ]);
        return (int)$db->lastInsertId();
    }

    public static function delete(int $id): void
    {
        Database::get()->prepare(
            'DELETE FROM app_reservation_requests WHERE id = ?'
        )->execute([$id]);
    }

    public static function count(): int
    {
        return (int)Database::get()
            ->query('SELECT COUNT(*) FROM app_reservation_requests')
            ->fetchColumn();
    }

    public static function validate(array $data): array
    {
        $errors = [];

        $name = trim($data['requester_name'] ?? '');
        if ($name === '') {
            $errors[] = 'Le nom est obligatoire.';
        } elseif (mb_strlen($name) > 100) {
            $errors[] = 'Le nom ne doit pas dépasser 100 caractères.';
        }

        $email = trim($data['requester_email'] ?? '');
        if ($email === '') {
            $errors[] = 'L\'adresse email est obligatoire.';
        } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $errors[] = 'L\'adresse email est invalide.';
        }

        $mac = trim($data['mac_address'] ?? '');
        if ($mac === '') {
            $errors[] = 'L\'adresse MAC est obligatoire.';
        } elseif (!isValidMac($mac)) {
            $errors[] = 'L\'adresse MAC est invalide (format attendu : xx:xx:xx:xx:xx:xx).';
        }

        $subnetId = (int)($data['subnet_id'] ?? 0);
        if ($subnetId <= 0) {
            $errors[] = 'Veuillez sélectionner un réseau.';
        } else {
            // Vérifier que le subnet existe et est public
            $stmt = Database::get()->prepare(
                'SELECT id FROM app_subnets WHERE id = ? AND is_public = 1'
            );
            $stmt->execute([$subnetId]);
            if (!$stmt->fetch()) {
                $errors[] = 'Le réseau sélectionné est invalide.';
            }
        }

        return $errors;
    }
}
