<?php

class Ouvrage {

    private $Id_ouvrage;
    private $numero;
    private $titre;
    private $etat;
    private $img;
    private $Id_type;
    private $collection;
    private $code_barre;

    public function __construct($Id_ouvrage, $numero, $titre, $etat, $img, $Id_type, $collection, $code_barre) {
        $this->Id_ouvrage = $Id_ouvrage;
        $this->numero = $numero;
        $this->titre = $titre;
        $this->etat = $etat;
        $this->img = $img;
        $this->Id_type = $Id_type;
        $this->collection = $collection;
        $this->code_barre = $code_barre;
    }


    public static function getAllOuvrages() {
        $db = Bdd::getInstance();
        $pdo = $db->getConnection();  
        try {
            $stmt = $pdo->query("SELECT * FROM ouvrage");
            $ouvrages = array();

            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $ouvrage = new Ouvrage(
                    $row['Id_ouvrage'],
                    $row['numero'],
                    $row['titre'],
                    $row['etat'],
                    $row['img'],
                    Type::getTypeById($row['Id_type']),
                    $row['collection'],
                    $row['code_barre']
                );
                $ouvrages[] = $ouvrage;
            }

            return $ouvrages;
        } catch (PDOException $e) {
            echo "Erreur: " . $e->getMessage();
        }
    }

    public function getId_ouvrage() {
        return $this->Id_ouvrage;
    }

    public function setId_ouvrage($Id_ouvrage) {
        $this->Id_ouvrage = $Id_ouvrage;
    }

    public function getNumero() {
        return $this->numero;
    }

    public function setNumero($numero) {
        $this->numero = $numero;
    }

    public function getTitre() {
        return $this->titre;
    }

    public function setTitre($titre) {
        $this->titre = $titre;
    }

    public function getEtat() {
        return $this->etat;
    }

    public function setEtat($etat) {
        $this->etat = $etat;
    }

    public function getImg() {
        return $this->img;
    }

    public function setImg($img) {
        $this->img = $img;
    }

    public function getId_type() {
        return $this->Id_type;
    }

    public function setId_type($Id_type) {
        $this->Id_type = $Id_type;
    }
    public function getcollection() {
        return $this->collection;
    }

    public function setcollection($collection) {
        $this->collection = $collection;
    }
    // --- UTILITAIRES QR/SCAN SANS COMPOSER ---
    // ⚠️ Mets ce secret dans une variable d'environnement en prod.
    private const QR_SECRET = 'change-moi-par-un-secret-long-et-aleatoire';

    // Si tu as déjà un BASE_URL global, tu peux le retirer d'ici et utiliser ta conf.
    private const BASE_URL = 'https://ton-domaine.tld'; // ex: https://monapp.fr

    public static function signId(int $idOuvrage): string {
        return hash_hmac('sha256', (string)$idOuvrage, self::QR_SECRET);
    }

    public static function verifySignature(int $idOuvrage, string $signature): bool {
        $calc = self::signId($idOuvrage);
        return hash_equals($calc, $signature);
    }

    /** URL de scan sécurisée (utilisée par le QR) */
    public static function scanUrl(int $idOuvrage): string {
        $sig = self::signId($idOuvrage);
        return self::BASE_URL . '/sindex.php?uc=scan&id=' . $idOuvrage . '&sig=' . $sig;
    }

    /** Le livre est-il dispo ? (nécessite Emprunt::getActiveByOuvrage) */
    public static function isDisponible(int $idOuvrage): bool {
        return (Emprunt::getActiveByOuvrage($idOuvrage) === null);
    }
    public function getTypeLibelle(): string {
        $t = $this->getId_type();
        return ($t instanceof Type) ? ($t->getLibelle() ?? '-') : (string)$t;
    }
    public static function findById(int $id) {
        $db = Bdd::getInstance()->getConnection();
        $stmt = $db->prepare("SELECT * FROM ouvrage WHERE Id_ouvrage = :id LIMIT 1");
        $stmt->execute([':id' => $id]);
        if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            return new Ouvrage(
                $row['Id_ouvrage'],
                $row['numero'],
                $row['titre'],
                $row['etat'],
                $row['img'],
                Type::getTypeById($row['Id_type']),
                $row['collection'],
                $row['code_barre']
            );
        }
        return null;
    }


}
