<?php

declare(strict_types=1);

namespace App\Models;

use App\Core\Model;
use PDO;

final class Fournisseur extends Model
{
    public function allActive(): array
    {
        $sql = "
            SELECT *
            FROM fournisseurs
            WHERE actif = 1
            ORDER BY raison_sociale ASC
        ";

        return $this->db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
    }

    public function find(int $id): ?array
    {
        $stmt = $this->db->prepare("SELECT * FROM fournisseurs WHERE id = :id LIMIT 1");
        $stmt->execute(['id' => $id]);
        $fournisseur = $stmt->fetch(PDO::FETCH_ASSOC);

        return $fournisseur ?: null;
    }

    public function findByCode(string $code): ?array
    {
        $stmt = $this->db->prepare("SELECT * FROM fournisseurs WHERE code_fournisseur = :code LIMIT 1");
        $stmt->execute(['code' => $code]);
        $fournisseur = $stmt->fetch(PDO::FETCH_ASSOC);

        return $fournisseur ?: null;
    }
}
