<?php

namespace App\Filament\Resources\ResellerOrders\Pages;

use App\Filament\Resources\ResellerOrders\ResellerOrderResource;
use App\Filament\Resources\ResellerOrders\Widgets\ResellerOrderStats;
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 ListResellerOrders extends ListRecords
{
    protected static string $resource = ResellerOrderResource::class;

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

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

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

    public function getTabs(): array
    {
        return [
            'all' => Tab::make('All Orders')
                ->icon('heroicon-o-shopping-cart')
                ->badge(Order::where('is_reseller_order', true)->count()),

            'pending' => Tab::make('New')
                ->icon('heroicon-o-sparkles')
                ->modifyQueryUsing(fn (Builder $query) => $query->where('status', 'pending'))
                ->badge(Order::where('is_reseller_order', true)->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(Order::where('is_reseller_order', true)->where('status', 'processing')->count() ?: null)
                ->badgeColor('warning'),

            'shipped' => Tab::make('Shipped')
                ->icon('heroicon-o-truck')
                ->modifyQueryUsing(fn (Builder $query) => $query->where('status', 'shipped'))
                ->badge(Order::where('is_reseller_order', true)->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(Order::where('is_reseller_order', true)->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(Order::where('is_reseller_order', true)->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(Order::where('is_reseller_order', true)->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(Order::where('is_reseller_order', true)->where('is_exchange', true)->count() ?: null)
                ->badgeColor('info'),
        ];
    }
}
