<?php

namespace App\Filament\Resources\Master\Feeds;

use App\Enums\FeedStatus;
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\Master\Feeds\Pages\ManageFeeds;
use App\Models\Feed;
use BackedEnum;
use Filament\Actions\BulkActionGroup;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
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\Support\RawJs;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use UnitEnum;

class FeedResource extends Resource
{
    protected static ?string $model = Feed::class;

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

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

    protected static ?string $navigationLabel = 'Pakan';

    protected static ?int $navigationSort = 4;

    protected static ?string $recordTitleAttribute = 'name';

    protected static ?string $slug = 'master/feeds';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextInput::make('name')
                    ->label('Nama Pakan')
                    ->placeholder('Pelet')
                    ->autocomplete(false)
                    ->autofocus()
                    ->required()
                    ->maxLength(50),

                Select::make('unit_id')
                    ->label('Satuan')
                    ->relationship('unit', 'name')
                    ->required()
                    ->native(false)
                    ->searchable()
                    ->preload(),

                Select::make('status')
                    ->label('Status')
                    ->options(FeedStatus::class)
                    ->required()
                    ->native(false)
                    ->default(FeedStatus::AVAILABLE),

                TextInput::make('price')
                    ->label('Harga')
                    ->placeholder('1,000,000')
                    ->autocomplete(false)
                    ->autofocus()
                    ->required()
                    ->mask(RawJs::make('$money($input)'))
                    ->prefix('Rp')
                    ->helperText('Harga per satuan yang dipilih.')
                    ->dehydrateStateUsing(fn ($state) => str_replace(',', '', $state)),

                SpatieMediaLibraryFileUpload::make('image')
                    ->label('Gambar')
                    ->disk(config('filesystems.default'))
                    ->acceptedFileTypes(['image/*'])
                    ->maxSize(1024 * 3)
                    ->collection('feeds')
                    ->customProperties(fn (): array => [
                        'feature' => 'feeds',
                        'date' => now()->toDateString(),
                    ])
                    ->image()
                    ->required(),
            ])
            ->columns(1);
    }

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

                TextColumn::make('stock')
                    ->label('Stok')
                    ->numeric(decimalPlaces: 2)
                    ->suffix(fn (Feed $record) => ' '.$record->unit->alias)
                    ->sortable(),

                TextColumn::make('price')
                    ->label('Harga')
                    ->money('IDR', decimalPlaces: 0),

                TextColumn::make('status')
                    ->label('Status')
                    ->badge()
                    ->sortable(),

                ...TimestampColumns::make(),
            ])
            ->filters([
                SelectFilter::make('status')
                    ->label('Status')
                    ->options(FeedStatus::class)
                    ->native(false),

                TrashedFilter::make()
                    ->native(false),
            ])
            ->recordActions([
                EditAction::make()
                    ->modalWidth(Width::Large),

                DeleteAction::make(),

                ForceDeleteAction::make(),

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

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

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