<?php

namespace App\Filament\Resources\Feed\FeedDeliveries;

use App\Enums\RoleEnum;
use App\Filament\Actions\Cheerful\DeleteAction;
use App\Filament\Actions\Cheerful\EditAction;
use App\Filament\Actions\Cheerful\ForceDeleteAction;
use App\Filament\Actions\Cheerful\RestoreAction;
use App\Filament\Actions\DefaultBulkActions;
use App\Filament\Columns\TimestampColumns;
use App\Filament\Resources\Feed\FeedDeliveries\Pages\ManageFeedDeliveries;
use App\Models\FeedDelivery;
use BackedEnum;
use Filament\Actions\BulkActionGroup;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Enums\Width;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use UnitEnum;

class FeedDeliveryResource extends Resource
{
    protected static ?string $model = FeedDelivery::class;

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

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

    protected static ?string $navigationLabel = 'Riwayat Pemberian Pakan';

    protected static ?int $navigationSort = 4;

    protected static ?string $recordTitleAttribute = 'delivered_at';

    protected static ?string $slug = 'feed/feed-deliveries';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Select::make('feed_id')
                    ->label('Pakan')
                    ->relationship('feed', 'name')
                    ->required()
                    ->searchable()
                    ->preload(),

                TextInput::make('quantity')
                    ->label('Kuantitas')
                    ->placeholder('0')
                    ->autocomplete(false)
                    ->required()
                    ->currencyMask(thousandSeparator: '.', decimalSeparator: ',', precision: 0)
                    ->dehydrateStateUsing(fn ($state) => (int) str()->replace(',', '', (string) ($state ?? 0)))
                    ->helperText('Satuan kuantitas pakan sesuai dengan satuan yang ada di menu pakan.'),
            ])
            ->columns(1);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('feed.name')
                    ->label('Pakan')
                    ->sortable()
                    ->searchable(),

                TextColumn::make('quantity')
                    ->label('Kuantitas')
                    ->numeric(decimalPlaces: 0)
                    ->suffix(fn (FeedDelivery $record) => ' '.$record->feed?->unit?->alias)
                    ->searchable(),

                TextColumn::make('delivered_at')
                    ->label('Tanggal Disalurkan')
                    ->dateTime('l, d M Y H:i')
                    ->sortable()
                    ->searchable(),

                ...TimestampColumns::make(),
            ])
            ->filters([
                TrashedFilter::make()
                    ->native(false)
                    ->visible(fn (): bool => auth()->user()->hasRole(RoleEnum::DEVELOPER)),
            ])
            ->recordActions([
                EditAction::make()
                    ->modalWidth(Width::Large),

                DeleteAction::make(),

                ForceDeleteAction::make(),

                RestoreAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    ...DefaultBulkActions::make('Jadwal Pakan'),
                ]),
            ])
            ->emptyStateIcon(Heroicon::Clock)
            ->emptyStateDescription('Setelah Anda membuat data pertama, maka akan muncul disini.')
            ->defaultSort('created_at', 'desc')
            ->deferFilters(false);
    }

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

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