<?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 Divers extends Page
{
    protected static string|\BackedEnum|null $navigationIcon = Heroicon::OutlinedWrenchScrewdriver;

    protected static ?string $navigationLabel = 'Divers';

    protected static ?string $title = 'Divers';

    protected static ?int $navigationSort = 6;

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

    public string $mode = 'read';

    // getserialnumber
    public array $serialData = [];

    // codes_list
    public string $code = '';

    public array $codesData = [];

    // custom_geninv_updatestock
    public string $stkArtId = '';

    public string $stkId = '';

    public string $stkQty = '';

    public string $stkToCheck = '';

    public string $stkToCheckDetail = '';

    public string $stkMode = '';

    public array $updateStockResult = [];

    public ?string $errorMessage = null;

    // Tracking
    public bool $hasSerial = false;

    public bool $hasCodes = false;

    public bool $hasUpdatedStock = false;

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

        try {
            $service = app(LogisticsService::class);
            $response = $service->getSerialNumber();

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

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

        if (blank($this->code)) {
            $this->errorMessage = 'Le champ debut de code (code) est obligatoire.';

            return;
        }

        $this->hasCodes = true;

        try {
            $service = app(LogisticsService::class);
            $response = $service->codesList(['code' => $this->code]);

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

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

        if (blank($this->stkArtId) || blank($this->stkId) || blank($this->stkQty)) {
            $this->errorMessage = 'Les champs ARTID, STKID et QTY sont obligatoires.';

            return;
        }

        $this->hasUpdatedStock = true;

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

            $params = array_filter([
                'ARTID' => $this->stkArtId,
                'STKID' => $this->stkId,
                'QTY' => $this->stkQty,
                'TOCHECK' => $this->stkToCheck,
                'TOCHECKDETAIL' => $this->stkToCheckDetail,
                'MODE' => $this->stkMode,
            ]);

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

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