<?php

declare(strict_types=1);

namespace App\Filament\Resources\Producers;

use App\Filament\Resources\Producers\Pages\CreateProducer;
use App\Filament\Resources\Producers\Pages\EditProducer;
use App\Filament\Resources\Producers\Pages\ListProducers;
use App\Filament\Resources\Producers\Schemas\ProducerForm;
use App\Filament\Resources\Producers\Tables\ProducersTable;
use App\Models\Producer;
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\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use UnitEnum;

class ProducerResource extends Resource
{
    protected static ?string $model = Producer::class;

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

    protected static ?string $recordTitleAttribute = 'brand_name';

    protected static ?string $navigationLabel = 'تولیدکنندگان';

    protected static ?string $modelLabel = 'تولیدکننده';

    protected static ?string $pluralModelLabel = 'تولیدکنندگان';

    protected static string|null|UnitEnum $navigationGroup = 'کاربران';

    protected static ?int $navigationSort = 2;

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

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

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

    public static function getPages(): array
    {
        return [
            'index' => ListProducers::route('/'),
            'create' => CreateProducer::route('/create'),
            'edit' => EditProducer::route('/{record}/edit'),
        ];
    }

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

    public static function getGlobalSearchResultTitle(Model $record): string|Htmlable
    {
        return $record->brand_name ?? $record->user->name ?? 'تولیدکننده';
    }

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

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

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

