<?php

declare(strict_types=1);

namespace App\Filament\Resources\Experts;

use App\Filament\Resources\Experts\Pages\CreateExpert;
use App\Filament\Resources\Experts\Pages\EditExpert;
use App\Filament\Resources\Experts\Pages\ListExperts;
use App\Filament\Resources\Experts\Pages\ViewExpert;
use App\Filament\Resources\Experts\Schemas\ExpertForm;
use App\Filament\Resources\Experts\Tables\ExpertsTable;
use App\Models\Expert;
use App\Filament\Support\NavigationBadge;
use BackedEnum;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Table;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Database\Eloquent\Builder;
use UnitEnum;

class ExpertResource extends Resource
{
    protected static ?string $model = Expert::class;

    protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedUserGroup;

    protected static ?string $recordTitleAttribute = 'display_name';

    protected static ?string $navigationLabel = 'خدمات‌دهندگان';

    protected static ?string $modelLabel = 'خدمات‌دهنده';

    protected static ?string $pluralModelLabel = 'خدمات‌دهندگان';

    protected static string|null|UnitEnum $navigationGroup = 'خدمات';

    public static function form(Schema $schema): Schema
    {
        return ExpertForm::configure($schema);
    }

    public static function table(Table $table): Table
    {
        return ExpertsTable::configure($table);
    }

    public static function getRelations(): array
    {
        return [];
    }

    public static function getPages(): array
    {
        return [
            'index' => ListExperts::route('/'),
            'create' => CreateExpert::route('/create'),
            'view' => ViewExpert::route('/{record}'),
            'edit' => EditExpert::route('/{record}/edit'),
        ];
    }

    public static function getRecordRouteBindingEloquentQuery(): Builder
    {
        return parent::getRecordRouteBindingEloquentQuery()
            ->withoutGlobalScopes([SoftDeletingScope::class]);
    }

    public static function getGlobalSearchResultTitle(Model $record): string|Htmlable
    {
        return $record->display_name ?? $record->brand_name ?? 'خدمات‌دهنده';
    }

    public static function getGloballySearchableAttributes(): array
    {
        return ['brand_name', 'main_category', 'work_email', 'work_phone'];
    }

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

    public static function getNavigationBadgeColor(): ?string
    {
        return NavigationBadge::colorFromQuery(
            fn () => static::getModel()::query()->whereIn('status', [
                Expert::STATUS_PENDING,
                Expert::STATUS_NEEDS_EDIT,
            ])
        );
    }
}
