<?php

namespace App\Filament\Resources\Orders\Pages;

use App\Filament\Resources\Orders\OrderResource;
use App\Filament\Resources\Orders\Widgets\OrderStats;
use App\Models\Order;
use Filament\Actions\CreateAction;
use Filament\Schemas\Components\Tabs\Tab;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Database\Eloquent\Builder;

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

    public function getTitle(): string
    {
        return 'Orders';
    }

    protected function getHeaderActions(): array
    {
        return [
            CreateAction::make(),
        ];
    }

    protected function getHeaderWidgets(): array
    {
        return [
            OrderStats::class,
        ];
    }

    public function getTabs(): array
    {
        // Base scope: regular (non-reseller) orders only
        $base = fn () => Order::where('is_reseller_order', false);

        return [
            'all' => Tab::make('All Orders')
                ->icon('heroicon-o-shopping-cart')
                ->badge($base()->count()),

            'pending' => Tab::make('New')
                ->icon('heroicon-o-sparkles')
                ->modifyQueryUsing(fn (Builder $query) => $query->where('status', 'pending'))
                ->badge($base()->where('status', 'pending')->count() ?: null)
                ->badgeColor('info'),

            'processing' => Tab::make('Processing')
                ->icon('heroicon-o-arrow-path')
                ->modifyQueryUsing(fn (Builder $query) => $query->where('status', 'processing'))
                ->badge($base()->where('status', 'processing')->count() ?: null)
                ->badgeColor('warning'),

            'shipped' => Tab::make('Shipped')
                ->icon('heroicon-o-truck')
                ->modifyQueryUsing(fn (Builder $query) => $query->where('status', 'shipped'))
                ->badge($base()->where('status', 'shipped')->count() ?: null)
                ->badgeColor('info'),

            'delivered' => Tab::make('Delivered')
                ->icon('heroicon-o-check-circle')
                ->modifyQueryUsing(fn (Builder $query) => $query->where('status', 'delivered'))
                ->badge($base()->where('status', 'delivered')->count() ?: null)
                ->badgeColor('success'),

            'cancelled' => Tab::make('Cancelled')
                ->icon('heroicon-o-x-circle')
                ->modifyQueryUsing(fn (Builder $query) => $query->where('status', 'cancelled'))
                ->badge($base()->where('status', 'cancelled')->count() ?: null)
                ->badgeColor('danger'),

            'returned' => Tab::make('Returned')
                ->icon('heroicon-o-arrow-uturn-left')
                ->modifyQueryUsing(fn (Builder $query) => $query->where('status', 'returned'))
                ->badge($base()->where('status', 'returned')->count() ?: null),

            'exchanges' => Tab::make('Exchange Orders')
                ->icon('heroicon-o-arrow-path-rounded-square')
                ->modifyQueryUsing(fn (Builder $query) => $query->where('is_exchange', true))
                ->badge($base()->where('is_exchange', true)->count() ?: null)
                ->badgeColor('info'),
        ];
    }
}
