<?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;

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

    public function getTabs(): array
    {
        return [
            'active' => Tab::make()
                ->query(function ($query) {
                    return $query
                        ->whereNot('status', OrderStatus::INVOICED)
                        ->whereNot('status', ORderStatus::SHIPPED);
                })
                ->icon(OrderStatus::PRODUCTION->getIcon())
                ->badge(function () {
                    return Order::whereNot('status', OrderStatus::SHIPPED)
                        ->whereNot('status', OrderStatus::INVOICED)
                        ->count();
                }),

            'unprinted' => Tab::make()
                ->query(function ($query) {
                    return $query->where('printed', false);
                })
                ->icon(IconEnum::PRINT->value)
                ->badge(function () {
                    $count = Order::where('printed', false)->count();

                    return $count > 0 ? $count : null;
                })
                ->badgeColor('success'),

            'overdue' => Tab::make()
                ->query(function ($query) {
                    return $query->whereDate('due_date', '<=', today())
                        ->whereNot('status', OrderStatus::INVOICED)
                        ->whereNot('status', ORderStatus::SHIPPED);
                })
                ->icon(IconEnum::TAB_OVERDUE->value)
                ->badge(function () {
                    $count = Order::whereDate('due_date', '<=', today())
                        ->whereNot('status', OrderStatus::INVOICED)
                        ->whereNot('status', ORderStatus::SHIPPED)
                        ->count();

                    return $count > 0 ? $count : null;
                })
                ->badgeColor('danger'),

            'rush' => Tab::make()
                ->query(function ($query) {
                    return $query->where('rush', true)
                        ->whereNot('status', OrderStatus::INVOICED)
                        ->whereNot('status', OrderStatus::SHIPPED);
                })
                ->icon(OrderAttributes::rush->getIcon())
                ->badge(function () {
                    $count = Order::where('rush', true)
                        ->whereNot('status', OrderStatus::INVOICED)
                        ->whereNot('status', OrderStatus::SHIPPED)
                        ->count();

                    return $count > 0 ? $count : null;
                })
                ->badgeColor('warning'),

            'all' => Tab::make('All')
                ->icon(IconEnum::TAB_ALL->value),
        ];
    }
}
