<?php

declare(strict_types=1);

namespace App\Filament\Resources\Bookings\Pages;

use App\Enums\BookingStatus;
use App\Filament\Resources\Bookings\BookingResource;
use App\Models\Booking;
use Carbon\CarbonImmutable;
use Filament\Resources\Pages\ListRecords;
use Filament\Schemas\Components\Tabs\Tab;
use Illuminate\Database\Eloquent\Builder;

/**
 * The admin's day, split into the three piles it actually falls into:
 * money to verify, sessions to run, and everything the card payments settled
 * without anyone touching them.
 */
class ListBookings extends ListRecords
{
    protected static string $resource = BookingResource::class;

    public function getTabs(): array
    {
        $now = CarbonImmutable::now('UTC');

        return [
            'attention' => Tab::make('Needs a decision')
                ->icon('heroicon-m-exclamation-circle')
                ->modifyQueryUsing(fn (Builder $query) => $query
                    ->live($now)
                    ->whereIn('status', BookingStatus::needingAttention()))
                ->badge(fn () => Booking::query()
                    ->live($now)
                    ->whereIn('status', BookingStatus::needingAttention())
                    ->count() ?: null)
                ->badgeColor('warning'),

            'today' => Tab::make('Today')
                ->icon('heroicon-m-calendar')
                ->modifyQueryUsing(fn (Builder $query) => $query
                    ->whereBetween('starts_at', self::todayWindow())
                    ->whereIn('status', [
                        BookingStatus::Confirmed->value,
                        BookingStatus::Completed->value,
                    ]))
                ->badge(fn () => Booking::query()
                    ->whereBetween('starts_at', self::todayWindow())
                    ->where('status', BookingStatus::Confirmed->value)
                    ->count() ?: null),

            'upcoming' => Tab::make('Upcoming')
                ->icon('heroicon-m-arrow-trending-up')
                ->modifyQueryUsing(fn (Builder $query) => $query
                    ->where('starts_at', '>=', $now)
                    ->where('status', BookingStatus::Confirmed->value)),

            'unassigned' => Tab::make('No engineer yet')
                ->icon('heroicon-m-user-minus')
                ->modifyQueryUsing(fn (Builder $query) => $query
                    ->where('staff_required', '>', 0)
                    ->whereNull('assigned_engineer_id')
                    ->where('starts_at', '>=', $now)
                    ->where('status', BookingStatus::Confirmed->value))
                ->badge(fn () => Booking::query()
                    ->where('staff_required', '>', 0)
                    ->whereNull('assigned_engineer_id')
                    ->where('starts_at', '>=', CarbonImmutable::now('UTC'))
                    ->where('status', BookingStatus::Confirmed->value)
                    ->count() ?: null)
                ->badgeColor('warning'),

            'all' => Tab::make('Everything')
                ->icon('heroicon-m-list-bullet'),
        ];
    }

    public function getDefaultActiveTab(): string
    {
        return 'attention';
    }

    /**
     * "Today" means the studio's today, not the server's.
     *
     * @return array{0: CarbonImmutable, 1: CarbonImmutable}
     */
    private static function todayWindow(): array
    {
        $start = CarbonImmutable::now(config('studio.timezone'))->startOfDay();

        return [$start->setTimezone('UTC'), $start->addDay()->setTimezone('UTC')];
    }
}
