<?php

namespace App\Filament\Resources\Manage\DataChanges;

use App\Enums\RoleEnum;
use App\Filament\Resources\Manage\DataChanges\Infolists\DataChangeInfolist;
use App\Filament\Resources\Manage\DataChanges\Pages\ManageDataChanges;
use App\Filament\Resources\Manage\DataChanges\Pages\ViewDataChange;
use App\Filament\Resources\Manage\DataChanges\Tables\DataChangeTable;
use App\Models\DataChangeRequest;
use BackedEnum;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
use UnitEnum;

class DataChangesResource extends Resource
{
    protected static ?string $model = DataChangeRequest::class;

    protected static string|UnitEnum|null $navigationGroup = 'Kelola';

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

    protected static ?string $navigationLabel = 'Permohonan Perubahan Data';

    protected static ?int $navigationSort = 8;

    protected static ?string $slug = 'manage/data-changes';

    public static function getNavigationBadge(): ?string
    {
        if (Auth::user()?->hasRole(RoleEnum::PERUSAHAAN)) {
            return null;
        }

        return static::getModel()::pending()->count();
    }

    public static function infolist(Schema $schema): Schema
    {
        return DataChangeInfolist::configure($schema);
    }

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

    public static function getEloquentQuery(): Builder
    {
        return parent::getEloquentQuery()
            ->when(Auth::user()?->hasRole(RoleEnum::PERUSAHAAN->value), function (Builder $q): Builder {
                return $q->where('user_id', Auth::id());
            });
    }

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