<?php

declare(strict_types=1);

namespace App\Repositories;

final class UniteArticleRepository extends BaseRepository
{
    public function all(): array
    {
        return $this->fetchAll(
            "SELECT *
             FROM unites_articles
             ORDER BY actif DESC, libelle ASC"
        );
    }

    public function allActive(): array
    {
        return $this->fetchAll(
            "SELECT *
             FROM unites_articles
             WHERE actif = 1
             ORDER BY libelle ASC"
        );
    }

    public function create(string $libelle): void
    {
        $this->execute(
            "INSERT INTO unites_articles (libelle, actif)
             VALUES (:libelle, 1)",
            [
                'libelle' => trim($libelle),
            ]
        );
    }

    public function update(int $id, string $libelle, int $actif): void
    {
        $this->execute(
            "UPDATE unites_articles
             SET libelle = :libelle,
                 actif = :actif
             WHERE id = :id",
            [
                'id' => $id,
                'libelle' => trim($libelle),
                'actif' => $actif,
            ]
        );
    }

    public function deactivate(int $id): void
    {
        $this->execute(
            "UPDATE unites_articles
             SET actif = 0
             WHERE id = :id",
            [
                'id' => $id,
            ]
        );
    }
}