<?php

namespace App\Filament\Resources\SaleResource\Pages;

use App\Enums\PaymentStatus;
use App\Filament\Resources\SaleResource;
use App\Models\Sale;
use Filament\Actions;
use Filament\Resources\Components\Tab;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Database\Eloquent\Builder;

class ListSales extends ListRecords
{
    protected static string $resource = SaleResource::class;

    public function getTabs(): array
    {
        return [
            'todos' => Tab::make('Todos'),

            'por_pagar' => Tab::make('Por cobrar')
                ->modifyQueryUsing(fn (Builder $query) => $query->whereIn('payment_status', [
                    PaymentStatus::Unpaid->value,
                    PaymentStatus::Partial->value,
                ]))
                ->badge(fn () => Sale::whereIn('payment_status', [
                    PaymentStatus::Unpaid->value,
                    PaymentStatus::Partial->value,
                ])->count())
                ->badgeColor('danger'),

            'pagado' => Tab::make('Pagados')
                ->modifyQueryUsing(fn (Builder $query) => $query->where('payment_status', PaymentStatus::Paid->value))
                ->badgeColor('success'),

            'este_mes' => Tab::make('Este mes')
                ->modifyQueryUsing(fn (Builder $query) => $query
                    ->whereMonth('sale_date', now()->month)
                    ->whereYear('sale_date', now()->year)),

            'mes_anterior' => Tab::make('Mes anterior')
                ->modifyQueryUsing(fn (Builder $query) => $query
                    ->whereMonth('sale_date', now()->subMonth()->month)
                    ->whereYear('sale_date', now()->subMonth()->year)),
        ];
    }

    protected function getHeaderActions(): array
    {
        return [
            Actions\CreateAction::make(),
            Actions\Action::make('export_excel')
                ->label('Exportar Excel')
                ->icon('heroicon-o-arrow-down-tray')
                ->color('success')
                ->url(route('export.sales'))
                ->openUrlInNewTab(),
            Actions\Action::make('export_cxc')
                ->label('Exportar CxC')
                ->icon('heroicon-o-document-chart-bar')
                ->color('warning')
                ->url(route('export.cxc'))
                ->openUrlInNewTab(),
        ];
    }
}
