<?php

namespace App\Filament\Pages;

use App\Models\ProfitCenter;
use App\Models\ProfitCenterSupplierSchedule;
use App\Models\Supplier;
use App\Services\NextDeliveryDateCalculatorService;
use Filament\Pages\Page;

class CalendarTest extends Page
{
    protected static string | \BackedEnum | null $navigationIcon = 'heroicon-o-calendar';

    protected string $view = 'filament.pages.calendar-test';

    protected static ?string $title = 'Naptár Teszt';

    protected static bool $shouldRegisterNavigation = false;

    public ?int $supplierId = null;

    public ?int $profitCenterId = null;

    public string $selectedDate = '';

    public string $selectedTime = '';

    public function mount(): void
    {
        $now = now();
        $this->selectedDate = $now->format('Y-m-d');
        $this->selectedTime = $now->format('H:i');

        $first = ProfitCenterSupplierSchedule::first();

        if ($first) {
            $this->supplierId = $first->supplier_id;
            $this->profitCenterId = $first->profit_center_id;
        }
    }

    public function getSupplier(): ?Supplier
    {
        if (!$this->supplierId) {
            return null;
        }

        return Supplier::find($this->supplierId);
    }

    public function getNextDeliveryDate(): ?string
    {
        if (! $this->supplierId || ! $this->profitCenterId || ! $this->selectedDate || ! $this->selectedTime) {
            return null;
        }

        $dt = \Illuminate\Support\Carbon::parse("{$this->selectedDate} {$this->selectedTime}");

        $result = app(NextDeliveryDateCalculatorService::class)->calculate(
            supplierId: $this->supplierId,
            profitCenterId: $this->profitCenterId,
            orderDateTime: $dt,
        );

        return $result?->translatedFormat('Y. F j., l');
    }

    public function getNextDeliveryDateRaw(): ?string
    {
        if (! $this->supplierId || ! $this->profitCenterId || ! $this->selectedDate || ! $this->selectedTime) {
            return null;
        }

        $dt = \Illuminate\Support\Carbon::parse("{$this->selectedDate} {$this->selectedTime}");

        $result = app(NextDeliveryDateCalculatorService::class)->calculate(
            supplierId: $this->supplierId,
            profitCenterId: $this->profitCenterId,
            orderDateTime: $dt,
        );

        return $result?->format('Y-m-d');
    }

    public function getSuppliers(): array
    {
        return Supplier::orderBy('name')->pluck('name', 'id')->toArray();
    }

    public function getProfitCenters(): array
    {
        if (!$this->supplierId) {
            return ProfitCenter::orderBy('name')->pluck('name', 'id')->toArray();
        }

        $pcIds = ProfitCenterSupplierSchedule::where('supplier_id', $this->supplierId)
            ->pluck('profit_center_id');

        return ProfitCenter::whereIn('id', $pcIds)->orderBy('name')->pluck('name', 'id')->toArray();
    }

    public function updatedSupplierId(): void
    {
        $this->profitCenterId = null;

        $first = ProfitCenterSupplierSchedule::where('supplier_id', $this->supplierId)->first();

        if ($first) {
            $this->profitCenterId = $first->profit_center_id;
        }

        $this->dispatch('calendar-filter-changed', supplierId: $this->supplierId, profitCenterId: $this->profitCenterId);
        $this->dispatchNextDeliveryDateChanged();
    }

    public function updatedProfitCenterId(): void
    {
        $this->dispatch('calendar-filter-changed', supplierId: $this->supplierId, profitCenterId: $this->profitCenterId);
        $this->dispatchNextDeliveryDateChanged();
    }

    public function updatedSelectedDate(): void
    {
        $this->dispatchNextDeliveryDateChanged();
    }

    public function updatedSelectedTime(): void
    {
        $this->dispatchNextDeliveryDateChanged();
    }

    protected function dispatchNextDeliveryDateChanged(): void
    {
        $this->dispatch('next-delivery-date-changed', date: $this->getNextDeliveryDateRaw());
    }

}
