<?php

namespace App\Filament\Resources\Master\Chickens;

use App\Enums\ChickenStatus;
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\Chickens\Pages\ManageChickens;
use App\Models\Chicken;
use BackedEnum;
use Filament\Actions\BulkActionGroup;
use Filament\Forms\Components\DatePicker;
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\SelectFilter;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use UnitEnum;

class ChickenResource extends Resource
{
    protected static ?string $model = Chicken::class;

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

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

    protected static ?string $navigationLabel = 'Ayam';

    protected static ?int $navigationSort = 3;

    protected static ?string $recordTitleAttribute = 'tag_code';

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

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextInput::make('tag_code')
                    ->label('Kode Tag')
                    ->placeholder('...')
                    ->autocomplete(false)
                    ->autofocus()
                    ->required()
                    ->maxLength(20)
                    ->unique(ignoreRecord: true),

                DatePicker::make('hatch_date')
                    ->label('Tanggal Menetas')
                    ->placeholder('Pilih Tanggal')
                    ->native(false)
                    ->displayFormat('l, d F Y'),

                DatePicker::make('arrival_date')
                    ->label('Tanggal Tiba')
                    ->placeholder('Pilih Tanggal')
                    ->required()
                    ->native(false)
                    ->displayFormat('l, d F Y'),

                DatePicker::make('exit_date')
                    ->label('Tanggal Keluar/Afkir')
                    ->placeholder('Pilih Tanggal')
                    ->native(false)
                    ->displayFormat('l, d F Y'),

                Select::make('status')
                    ->label('Status')
                    ->options(ChickenStatus::class)
                    ->required()
                    ->native(false)
                    ->default(ChickenStatus::ACTIVE),
            ])
            ->columns(1);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('tag_code')
                    ->label('Kode Tag')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('hatch_date')
                    ->label('Tanggal Menetas')
                    ->date('l, d F Y')
                    ->sortable(),

                TextColumn::make('arrival_date')
                    ->label('Tanggal Tiba')
                    ->date('l, d F Y')
                    ->sortable(),

                TextColumn::make('exit_date')
                    ->label('Tanggal Keluar')
                    ->date('l, d F Y')
                    ->sortable(),

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

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

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

                DeleteAction::make(),

                ForceDeleteAction::make(),

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

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

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