<?php

namespace App\Filament\Pages;

use App\Exceptions\LogisticsApiException;
use App\Services\LogisticsService;
use App\Support\ApiErrorTranslator;
use Filament\Pages\Page;
use Filament\Support\Icons\Heroicon;

class Documents extends Page
{
    protected static string|\BackedEnum|null $navigationIcon = Heroicon::OutlinedDocumentText;

    protected static ?string $navigationLabel = 'Documents';

    protected static ?string $title = 'Documents';

    protected static ?int $navigationSort = 3;

    protected string $view = 'filament.pages.documents';

    public string $mode = 'read';

    // document_list
    public string $select = 'jnl,number,thirdid,date';

    public string $thirdId = '';

    public string $results = '';

    // document_detail
    public string $detailJnl = '';

    public string $detailNumber = '';

    // Document_GetStatusList
    public string $statusJnl = '';

    // Document_GetUnitPriceAndVat
    public string $priceArtId = '';

    public string $priceQty = '';

    public string $priceJnl = '';

    public string $priceThirdId = '';

    public string $priceDate = '';

    // Document_GetDueDate
    public string $payDelay = '';

    public string $dueDateInput = '';

    // Document_GetAttachListThumbnail
    public string $attachJnl = '';

    public string $attachNumber = '';

    // Document_GetPDF
    public string $pdfJnl = '';

    public string $pdfNumber = '';

    public string $pdfLayout = '';

    // document_add
    public string $addThirdId = '';

    public string $addDate = '';

    public string $addArtIds = '';

    public string $addQty = '';

    public string $addSalePrice = '';

    public string $addJnl = '';

    public string $addDiscount = '';

    public string $addVatId = '';

    public string $addVatPc = '';

    // document_mod
    public string $modNumber = '';

    public string $modJnl = '';

    public string $modThirdId = '';

    public string $modArtIds = '';

    public string $modQty = '';

    public string $modSalePrice = '';

    // Data holders
    public array $data = [];

    public array $detailData = [];

    public array $statusData = [];

    public array $priceData = [];

    public array $dueDateData = [];

    public array $attachData = [];

    public array $pdfData = [];

    public array $addResult = [];

    public array $modResult = [];

    public ?array $metadata = null;

    public ?string $errorMessage = null;

    // Tracking
    public bool $hasSearchedDocs = false;

    public bool $hasDetail = false;

    public bool $hasStatus = false;

    public bool $hasPrice = false;

    public bool $hasDueDate = false;

    public bool $hasAttach = false;

    public bool $hasPdf = false;

    public bool $hasAdded = false;

    public bool $hasModified = false;

    /**
     * @return array<int, array{name: string, type: string, required: string, description: string}>
     */
    public function paramTableDocumentList(): array
    {
        return [
            ['name' => 'select', 'type' => 'string', 'required' => 'Non', 'description' => 'Colonnes a retourner, separees par des virgules (colonnes de la table dochead). Par defaut : thirdid.'],
            ['name' => 'thirdid', 'type' => 'string', 'required' => 'Non', 'description' => 'Identifiant du tiers (custid de la table cust). Si fourni, seuls les documents de ce tiers sont retournes.'],
            ['name' => 'results', 'type' => 'string', 'required' => 'Non', 'description' => "Nombre maximum de resultats. N'a d'effet que lorsqu'un thirdid est egalement fourni. Limite par defaut : ~108."],
        ];
    }

    /**
     * @return array<int, array{name: string, type: string, required: string, description: string}>
     */
    public function paramTableDocumentDetail(): array
    {
        return [
            ['name' => 'jnl', 'type' => 'string', 'required' => 'Oui', 'description' => 'Code du journal auquel appartient le document (ex : 03VEN, 01COM).'],
            ['name' => 'number', 'type' => 'string', 'required' => 'Oui', 'description' => 'Numero du document dans le journal (ex : 25105397).'],
        ];
    }

    /**
     * @return array<int, array{name: string, type: string, required: string, description: string}>
     */
    public function paramTableStatusList(): array
    {
        return [
            ['name' => 'jnl', 'type' => 'string', 'required' => 'Oui', 'description' => 'Code du journal dont on veut connaitre les statuts (ex : 03VEN, 01COM, 02NEV).'],
        ];
    }

    /**
     * @return array<int, array{name: string, type: string, required: string, description: string}>
     */
    public function paramTableUnitPriceAndVat(): array
    {
        return [
            ['name' => 'ARTID', 'type' => 'string', 'required' => 'Oui', 'description' => "Identifiant de l'article (champ artid de la table art)."],
            ['name' => 'QTY', 'type' => 'string', 'required' => 'Oui', 'description' => 'Quantite demandee. Doit etre au format string (ex : "2", pas 2).'],
            ['name' => 'JNL', 'type' => 'string', 'required' => 'Oui', 'description' => 'Code du journal (ex : 03VEN).'],
            ['name' => 'THIRDID', 'type' => 'string', 'required' => 'Oui', 'description' => 'Identifiant du tiers (custid de la table cust).'],
            ['name' => 'DATE', 'type' => 'string', 'required' => 'Oui', 'description' => 'Date de reference. Format obligatoire : YYYY-MM-DD.'],
        ];
    }

    /**
     * @return array<int, array{name: string, type: string, required: string, description: string}>
     */
    public function paramTableDueDate(): array
    {
        return [
            ['name' => 'paydelay', 'type' => 'string', 'required' => 'Oui', 'description' => 'Code de delai de paiement. Valeurs via codes_list PAYDELAY (ex : 30, 30F, 60, 60F, 90F).'],
            ['name' => 'date', 'type' => 'string', 'required' => 'Oui', 'description' => 'Date de depart pour le calcul. Format obligatoire : YYYY-MM-DD.'],
        ];
    }

    /**
     * @return array<int, array{name: string, type: string, required: string, description: string}>
     */
    public function paramTableAttachListThumbnail(): array
    {
        return [
            ['name' => 'JNL', 'type' => 'string', 'required' => 'Oui', 'description' => 'Code du journal du document (ex : 03VEN).'],
            ['name' => 'NUMBER', 'type' => 'string', 'required' => 'Oui', 'description' => 'Numero du document (ex : 25105397).'],
        ];
    }

    /**
     * @return array<int, array{name: string, type: string, required: string, description: string}>
     */
    public function paramTableGetPdf(): array
    {
        return [
            ['name' => 'JNL', 'type' => 'string', 'required' => 'Oui', 'description' => 'Code du journal (ex : 03VEN).'],
            ['name' => 'NUMBER', 'type' => 'string', 'required' => 'Oui', 'description' => 'Numero du document (ex : 25105397).'],
            ['name' => 'LAYOUT', 'type' => 'string', 'required' => 'Oui', 'description' => 'Identifiant numerique de la mise en page (format string). Valeur "1" recommandee par defaut.'],
        ];
    }

    /**
     * @return array<int, array{name: string, type: string, required: string, description: string}>
     */
    public function paramTableDocumentAdd(): array
    {
        return [
            ['name' => 'ThirdId', 'type' => 'string', 'required' => 'Oui', 'description' => 'Identifiant du tiers (custid de la table cust).'],
            ['name' => 'Date', 'type' => 'string', 'required' => 'Oui', 'description' => "Date d'encodage du document (ex : 2026-02-20)."],
            ['name' => 'JNL', 'type' => 'string', 'required' => 'Oui', 'description' => 'Code du journal (ex : VEN).'],
            ['name' => 'Artid', 'type' => 'array', 'required' => 'Oui', 'description' => "Tableau d'identifiants d'articles. Chaque element correspond a une ligne du document."],
            ['name' => 'Qty', 'type' => 'array', 'required' => 'Oui', 'description' => 'Tableau de quantites (format string). Correspondance positionnelle avec Artid.'],
            ['name' => 'Saleprice', 'type' => 'array', 'required' => 'Oui', 'description' => 'Tableau des prix de vente unitaires (format string). Correspondance positionnelle avec Artid.'],
            ['name' => 'Discount', 'type' => 'array', 'required' => 'Non', 'description' => 'Tableau des reductions de prix. Correspondance positionnelle avec Artid.'],
            ['name' => 'Vatid', 'type' => 'array', 'required' => 'Non', 'description' => 'Tableau des identifiants de code TVA. Correspondance positionnelle avec Artid.'],
            ['name' => 'Vatpc', 'type' => 'array', 'required' => 'Non', 'description' => 'Tableau des pourcentages de TVA. Correspondance positionnelle avec Artid.'],
        ];
    }

    /**
     * @return array<int, array{name: string, type: string, required: string, description: string}>
     */
    public function paramTableDocumentMod(): array
    {
        return [
            ['name' => 'number', 'type' => 'string', 'required' => 'Oui', 'description' => 'Identifiant du document a modifier (ex : 2026/0001).'],
            ['name' => 'JNL', 'type' => 'string', 'required' => 'Oui', 'description' => 'Code du journal lie au document (ex : VEN).'],
            ['name' => 'Thirdid', 'type' => 'string', 'required' => 'Non', 'description' => 'Nouvel identifiant du tiers (si modification du tiers associe).'],
            ['name' => 'Artid', 'type' => 'array', 'required' => 'Non', 'description' => "Tableau d'identifiants d'articles (remplace les lignes existantes)."],
            ['name' => 'Qty', 'type' => 'array', 'required' => 'Non', 'description' => 'Tableau de quantites. Correspondance positionnelle avec Artid.'],
            ['name' => 'Saleprice', 'type' => 'array', 'required' => 'Non', 'description' => 'Tableau des prix de vente unitaires. Correspondance positionnelle avec Artid.'],
        ];
    }

    public function searchDocuments(): void
    {
        $this->errorMessage = null;
        $this->hasSearchedDocs = true;

        try {
            $service = app(LogisticsService::class);

            $params = array_filter([
                'select' => $this->select,
                'thirdid' => $this->thirdId,
                'results' => $this->results,
            ]);

            $response = $service->documentList($params);

            $this->data = $response['data'] ?? [];
            $this->metadata = $response['metadata'] ?? null;
            $this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
        } catch (LogisticsApiException $e) {
            $this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
            $this->data = [];
        } catch (\Throwable $e) {
            $this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
            $this->data = [];
        }
    }

    public function getDocumentDetail(): void
    {
        $this->errorMessage = null;

        if (blank($this->detailJnl) || blank($this->detailNumber)) {
            $this->errorMessage = 'Les champs code journal (jnl) et numero de document (number) sont obligatoires.';

            return;
        }

        $this->hasDetail = true;

        try {
            $service = app(LogisticsService::class);
            $response = $service->documentDetail($this->detailJnl, $this->detailNumber);

            $this->detailData = $response['data'] ?? [];
            $this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
        } catch (LogisticsApiException $e) {
            $this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
            $this->detailData = [];
        } catch (\Throwable $e) {
            $this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
            $this->detailData = [];
        }
    }

    public function getStatusList(): void
    {
        $this->errorMessage = null;

        if (blank($this->statusJnl)) {
            $this->errorMessage = 'Le champ code journal (jnl) est obligatoire.';

            return;
        }

        $this->hasStatus = true;

        try {
            $service = app(LogisticsService::class);
            $response = $service->documentGetStatusList($this->statusJnl);

            $this->statusData = $response['data'] ?? [];
            $this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
        } catch (LogisticsApiException $e) {
            $this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
            $this->statusData = [];
        } catch (\Throwable $e) {
            $this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
            $this->statusData = [];
        }
    }

    public function getUnitPriceAndVat(): void
    {
        $this->errorMessage = null;

        if (blank($this->priceArtId) || blank($this->priceQty) || blank($this->priceJnl) || blank($this->priceThirdId) || blank($this->priceDate)) {
            $this->errorMessage = 'Tous les champs sont obligatoires : ARTID, QTY (format string), JNL, THIRDID et DATE.';

            return;
        }

        $this->hasPrice = true;

        try {
            $service = app(LogisticsService::class);
            $response = $service->documentGetUnitPriceAndVat([
                'ARTID' => $this->priceArtId,
                'QTY' => $this->priceQty,
                'JNL' => $this->priceJnl,
                'THIRDID' => $this->priceThirdId,
                'DATE' => $this->priceDate,
            ]);

            $this->priceData = $response['data'] ?? [];
            $this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
        } catch (LogisticsApiException $e) {
            $this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
            $this->priceData = [];
        } catch (\Throwable $e) {
            $this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
            $this->priceData = [];
        }
    }

    public function getDueDate(): void
    {
        $this->errorMessage = null;

        if (blank($this->payDelay) || blank($this->dueDateInput)) {
            $this->errorMessage = 'Les champs delai de paiement (paydelay) et date de depart (date) sont obligatoires.';

            return;
        }

        $this->hasDueDate = true;

        try {
            $service = app(LogisticsService::class);
            $response = $service->documentGetDueDate($this->payDelay, $this->dueDateInput);

            $this->dueDateData = $response['data'] ?? [];
            $this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
        } catch (LogisticsApiException $e) {
            $this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
            $this->dueDateData = [];
        } catch (\Throwable $e) {
            $this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
            $this->dueDateData = [];
        }
    }

    public function getAttachListThumbnail(): void
    {
        $this->errorMessage = null;

        if (blank($this->attachJnl) || blank($this->attachNumber)) {
            $this->errorMessage = 'Les champs code journal (JNL) et numero de document (NUMBER) sont obligatoires.';

            return;
        }

        $this->hasAttach = true;

        try {
            $service = app(LogisticsService::class);
            $response = $service->documentGetAttachListThumbnail($this->attachJnl, $this->attachNumber);

            $this->attachData = $response['data'] ?? [];
            $this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
        } catch (LogisticsApiException $e) {
            $this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
            $this->attachData = [];
        } catch (\Throwable $e) {
            $this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
            $this->attachData = [];
        }
    }

    public function getPdf(): void
    {
        $this->errorMessage = null;

        if (blank($this->pdfJnl) || blank($this->pdfNumber) || blank($this->pdfLayout)) {
            $this->errorMessage = 'Les champs code journal (JNL), numero de document (NUMBER) et mise en page (LAYOUT) sont obligatoires.';

            return;
        }

        $this->hasPdf = true;

        try {
            $service = app(LogisticsService::class);
            $response = $service->documentGetPdf($this->pdfJnl, $this->pdfNumber, $this->pdfLayout);

            $this->pdfData = $response['data'] ?? [];
            $this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
        } catch (LogisticsApiException $e) {
            $this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
            $this->pdfData = [];
        } catch (\Throwable $e) {
            $this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
            $this->pdfData = [];
        }
    }

    public function addDocument(): void
    {
        $this->errorMessage = null;

        if (blank($this->addThirdId) || blank($this->addDate) || blank($this->addArtIds) || blank($this->addQty) || blank($this->addSalePrice) || blank($this->addJnl)) {
            $this->errorMessage = 'Les champs ThirdId, Date, Artid, Qty, Saleprice et JNL sont obligatoires.';

            return;
        }

        $this->hasAdded = true;

        try {
            $service = app(LogisticsService::class);

            $params = [
                'ThirdId' => $this->addThirdId,
                'Date' => $this->addDate,
                'Artid' => $this->splitCsv($this->addArtIds),
                'Qty' => $this->splitCsv($this->addQty),
                'Saleprice' => $this->splitCsv($this->addSalePrice),
                'JNL' => $this->addJnl,
            ];

            if (filled($this->addDiscount)) {
                $params['Discount'] = $this->splitCsv($this->addDiscount);
            }
            if (filled($this->addVatId)) {
                $params['Vatid'] = $this->splitCsv($this->addVatId);
            }
            if (filled($this->addVatPc)) {
                $params['Vatpc'] = $this->splitCsv($this->addVatPc);
            }

            $response = $service->documentAdd($params);

            $this->addResult = $response['data'] ?? [];
            $this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
        } catch (LogisticsApiException $e) {
            $this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
            $this->addResult = [];
        } catch (\Throwable $e) {
            $this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
            $this->addResult = [];
        }
    }

    public function modDocument(): void
    {
        $this->errorMessage = null;

        if (blank($this->modNumber) || blank($this->modJnl)) {
            $this->errorMessage = 'Les champs numero de document (number) et code journal (JNL) sont obligatoires.';

            return;
        }

        $this->hasModified = true;

        try {
            $service = app(LogisticsService::class);

            $params = [
                'number' => $this->modNumber,
                'JNL' => $this->modJnl,
            ];

            if (filled($this->modThirdId)) {
                $params['Thirdid'] = $this->modThirdId;
            }
            if (filled($this->modArtIds)) {
                $params['Artid'] = $this->splitCsv($this->modArtIds);
            }
            if (filled($this->modQty)) {
                $params['Qty'] = $this->splitCsv($this->modQty);
            }
            if (filled($this->modSalePrice)) {
                $params['Saleprice'] = $this->splitCsv($this->modSalePrice);
            }

            $response = $service->documentMod($params);

            $this->modResult = $response['data'] ?? [];
            $this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
        } catch (LogisticsApiException $e) {
            $this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
            $this->modResult = [];
        } catch (\Throwable $e) {
            $this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
            $this->modResult = [];
        }
    }

    /**
     * @return array<int, string>
     */
    private function splitCsv(string $value): array
    {
        return array_map('trim', explode(',', $value));
    }
}
