<?php

declare(strict_types=1);

namespace App\Filament\Resources\SupportTickets;

use App\Filament\Resources\SupportTickets\Pages\ListSupportTickets;
use App\Filament\Resources\SupportTickets\Pages\ViewSupportTicket;
use App\Models\SupportTicket;
use App\Filament\Support\NavigationBadge;
use BackedEnum;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables\Table;
use UnitEnum;

class SupportTicketResource extends Resource
{
    protected static ?string $model = SupportTicket::class;

    public static function form(Schema $schema): Schema
    {
        return \App\Filament\Resources\SupportTickets\Schemas\SupportTicketForm::configure($schema);
    }

    protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-ticket';

    protected static ?string $navigationLabel = 'تیکت‌های پشتیبانی';

    protected static ?string $modelLabel = 'تیکت';

    protected static ?string $pluralModelLabel = 'تیکت‌های پشتیبانی';

    protected static string|null|UnitEnum $navigationGroup = 'پشتیبانی';

    protected static ?string $recordTitleAttribute = 'title';

    public static function table(Table $table): Table
    {
        return \App\Filament\Resources\SupportTickets\Tables\SupportTicketsTable::configure($table);
    }

    public static function getRelations(): array
    {
        return [
            \App\Filament\Resources\SupportTickets\RelationManagers\RepliesRelationManager::class,
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => ListSupportTickets::route('/'),
            'view' => ViewSupportTicket::route('/{record}'),
        ];
    }

    public static function getNavigationBadge(): ?string
    {
        return NavigationBadge::fromQuery(
            fn () => static::getModel()::query()
                ->whereIn('status', [SupportTicket::STATUS_UNREAD, SupportTicket::STATUS_OPEN])
        );
    }

    public static function getNavigationBadgeColor(): ?string
    {
        if (static::getModel()::query()->where('status', SupportTicket::STATUS_UNREAD)->exists()) {
            return 'danger';
        }

        return NavigationBadge::colorFromQuery(
            fn () => static::getModel()::query()->where('status', SupportTicket::STATUS_OPEN)
        );
    }
}
