<?php

declare(strict_types=1);

namespace App\Repositories;

final class TarifClientRepository extends BaseRepository
{
    public function allForClient(int $clientId): array
    {
        $sql = "
            SELECT 
                tc.*,
                a.reference,
                a.designation,
                a.prix_vente_ht AS prix_standard_ht,
                a.taux_tva
            FROM tarifs_clients tc
            INNER JOIN articles a ON a.id = tc.article_id
            WHERE tc.client_id = :client_id
              AND tc.actif = 1
              AND a.actif = 1
            ORDER BY a.designation ASC
        ";

        return $this->fetchAll($sql, ['client_id' => $clientId]);
    }

    public function find(int $id): ?array
    {
        return $this->fetchOne(
            'SELECT * FROM tarifs_clients WHERE id = :id LIMIT 1',
            ['id' => $id]
        );
    }

    public function findForClientAndArticle(int $clientId, int $articleId): ?array
    {
        return $this->fetchOne(
            'SELECT * FROM tarifs_clients WHERE client_id = :client_id AND article_id = :article_id LIMIT 1',
            ['client_id' => $clientId, 'article_id' => $articleId]
        );
    }


    public function priceForClientAndArticle(int $clientId, int $articleId): ?float
    {
        $row = $this->fetchOne(
            'SELECT prix_ht FROM tarifs_clients WHERE client_id = :client_id AND article_id = :article_id AND actif = 1 LIMIT 1',
            ['client_id' => $clientId, 'article_id' => $articleId]
        );

        return $row ? (float) $row['prix_ht'] : null;
    }

    public function allActiveForDocuments(): array
    {
        $rows = $this->fetchAll(
            'SELECT client_id, article_id, prix_ht FROM tarifs_clients WHERE actif = 1 ORDER BY client_id ASC, article_id ASC'
        );

        $map = [];
        foreach ($rows as $row) {
            $clientId = (int) ($row['client_id'] ?? 0);
            $articleId = (int) ($row['article_id'] ?? 0);
            if ($clientId > 0 && $articleId > 0) {
                $map[$clientId][$articleId] = (float) ($row['prix_ht'] ?? 0);
            }
        }

        return $map;
    }

    public function save(int $clientId, int $articleId, float $prixHt): void
    {
        $sql = "
            INSERT INTO tarifs_clients (client_id, article_id, prix_ht, actif)
            VALUES (:client_id, :article_id, :prix_ht, 1)
            ON DUPLICATE KEY UPDATE
                prix_ht = VALUES(prix_ht),
                actif = 1,
                date_modification = CURRENT_TIMESTAMP
        ";

        $this->execute($sql, [
            'client_id' => $clientId,
            'article_id' => $articleId,
            'prix_ht' => $prixHt,
        ]);
    }

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