<?php

namespace App\Filament\Admin\Resources\OrderResource\Pages;

use App\Enums\IconEnum;
use App\Enums\OrderAttributes;
use App\Enums\OrderStatus;
use App\Filament\Admin\Resources\OrderResource;
use App\Models\Order;
use Filament\Actions;
use Filament\Resources\Components\Tab;
use Filament\Resources\Pages\ListRecords;

class ListOrders extends ListRecords
{
    protected static string $resource = OrderResource::class;

    private function excludeStatuses($query): mixed
    {
        return $query
            ->whereNot('status', OrderStatus::READY_FOR_INVOICE)
            ->whereNot('status', OrderStatus::INVOICED)
            ->whereNot('status', OrderStatus::SHIPPED);
    }

    private function getBadgeCount(callable $queryCallback): ?int
    {
        $count = Order::query()->when(true, $queryCallback)
            ->whereNot('status', OrderStatus::READY_FOR_INVOICE)
            ->whereNot('status', OrderStatus::INVOICED)
            ->whereNot('status', OrderStatus::SHIPPED)
            ->count();

        return $count > 0 ? $count : null;
    }

    protected function getHeaderActions(): array
    {
        return [
            Actions\CreateAction::make()
                ->icon(IconEnum::NEW->value),
        ];
    }

    public function getTabs(): array
    {
        return [
            'all' => Tab::make('All')
                ->icon(IconEnum::TAB_ALL->value),

            'active' => Tab::make()
                ->query(fn ($query) => $this->excludeStatuses($query))
                ->icon(OrderStatus::PRODUCTION->getIcon())
                ->badge(fn () => $this->getBadgeCount(fn ($query) => $this->excludeStatuses($query))),

            'unprinted' => Tab::make()
                ->query(fn ($query) => $this->excludeStatuses($query)->where('printed', false))
                ->icon(IconEnum::PRINT->value)
                ->badge(fn () => $this->getBadgeCount(fn ($query) => $query->where('printed', false)))
                ->badgeColor('success'),

            'overdue' => Tab::make()
                ->query(fn ($query) => $this->excludeStatuses($query)->whereDate('due_date', '<=', today()))
                ->icon(IconEnum::TAB_OVERDUE->value)
                ->badge(fn () => $this->getBadgeCount(fn ($query) => $query->whereDate('due_date', '<=', today())))
                ->badgeColor('danger'),

            'rush' => Tab::make()
                ->query(fn ($query) => $this->excludeStatuses($query)->where('rush', true))
                ->icon(OrderAttributes::rush->getIcon())
                ->badge(fn () => $this->getBadgeCount(fn ($query) => $query->where('rush', true)))
                ->badgeColor('warning'),

            'ready_for_invoice' => Tab::make()
                ->query(fn ($query) => $query->where('status', OrderStatus::READY_FOR_INVOICE))
                ->icon(OrderStatus::READY_FOR_INVOICE->getIcon())
                ->badge(fn () => $this->getBadgeCount(fn ($query) => $query->where('status', OrderStatus::READY_FOR_INVOICE)))
                ->badgeColor(OrderStatus::READY_FOR_INVOICE->getColor()),
        ];
    }
}
