<?php

declare(strict_types=1);

namespace App\Services\Analytics;

final class ArticleAnalytics extends BaseAnalytics
{
    public function get(int $articleId): array
    {
        return [
            'ca' => $this->getCa($articleId),
            'quantite_vendue' => $this->getQuantiteVendue($articleId),
            'nb_factures' => $this->getNbFactures($articleId),
            'derniere_vente' => $this->getDerniereVente($articleId),
            'prix_moyen_vente' => $this->getPrixMoyenVente($articleId),
            'marge_totale' => $this->getMargeTotale($articleId),

            'stock_actuel' => 0,
            'stock_mini' => 0,
            'stock_valeur' => 0,
            'pmp' => 0,

            'nb_commandes' => 0,
            'dernier_achat' => null,
            'dernier_fournisseur' => null,
            'dernier_prix_achat' => 0,
        ];
    }

    private function getCa(int $articleId): float
    {
        return (float) $this->scalar("
            SELECT COALESCE(SUM(dl.total_ht), 0)
            FROM documents_lignes dl
            INNER JOIN documents d ON d.id = dl.document_id
            WHERE dl.article_id = :article_id
              AND d.type_document = 'FACTURE'
        ", ['article_id' => $articleId]);
    }

    private function getQuantiteVendue(int $articleId): float
    {
        return (float) $this->scalar("
            SELECT COALESCE(SUM(dl.quantite), 0)
            FROM documents_lignes dl
            INNER JOIN documents d ON d.id = dl.document_id
            WHERE dl.article_id = :article_id
              AND d.type_document = 'FACTURE'
        ", ['article_id' => $articleId]);
    }

    private function getNbFactures(int $articleId): int
    {
        return (int) $this->scalar("
            SELECT COUNT(DISTINCT d.id)
            FROM documents_lignes dl
            INNER JOIN documents d ON d.id = dl.document_id
            WHERE dl.article_id = :article_id
              AND d.type_document = 'FACTURE'
        ", ['article_id' => $articleId]);
    }

    private function getDerniereVente(int $articleId): ?string
    {
        $value = $this->scalar("
            SELECT MAX(d.date_document)
            FROM documents_lignes dl
            INNER JOIN documents d ON d.id = dl.document_id
            WHERE dl.article_id = :article_id
              AND d.type_document = 'FACTURE'
        ", ['article_id' => $articleId]);

        return $value ? (string) $value : null;
    }

    private function getPrixMoyenVente(int $articleId): float
    {
        $qty = $this->getQuantiteVendue($articleId);

        if ($qty <= 0) {
            return 0;
        }

        return $this->getCa($articleId) / $qty;
    }
private function getMargeTotale(int $articleId): float
{
    return (float) $this->scalar("
        SELECT COALESCE(SUM(dl.marge_ht), 0)
        FROM documents_lignes dl
        INNER JOIN documents d ON d.id = dl.document_id
        WHERE dl.article_id = :article_id
          AND d.type_document = 'FACTURE'
    ", [
        'article_id' => $articleId
    ]);
}
}