<?php

namespace App\Filament\Resources\Manage\EggCollections;

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\Manage\EggCollections\Pages\ManageEggCollections;
use App\Models\EggCollection;
use BackedEnum;
use Filament\Actions\BulkActionGroup;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Textarea;
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 UnitEnum;

class EggCollectionResource extends Resource
{
    protected static ?string $model = EggCollection::class;

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

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

    protected static ?string $navigationLabel = 'Produksi Telur';

    protected static ?int $navigationSort = 2;

    protected static ?string $recordTitleAttribute = 'production_date';

    protected static ?string $slug = 'manage/egg-collections';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Hidden::make('production_date')
                    ->default(now()),

                TextInput::make('total_eggs')
                    ->label('Jumlah Telur')
                    ->placeholder('0')
                    ->autocomplete(false)
                    ->required()
                    ->currencyMask(thousandSeparator: '.', decimalSeparator: ',', precision: 0)
                    ->dehydrateStateUsing(fn ($state) => (int) str()->replace(',', '', (string) ($state ?? 0))),

                TextInput::make('total_broken_eggs')
                    ->label('Jumlah Telur Pecah')
                    ->placeholder('0')
                    ->autocomplete(false)
                    ->required()
                    ->currencyMask(thousandSeparator: '.', decimalSeparator: ',', precision: 0)
                    ->dehydrateStateUsing(fn ($state) => (int) str()->replace(',', '', (string) ($state ?? 0))),

                Textarea::make('notes')
                    ->label('Catatan')
                    ->placeholder('...')
                    ->columnSpanFull(),
            ])
            ->columns(1);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('production_date')
                    ->label('Tanggal Produksi')
                    ->dateTime('l, d F Y H:i')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('total_eggs')
                    ->label('Total Telur')
                    ->numeric()
                    ->searchable()
                    ->sortable(),

                TextColumn::make('total_broken_eggs')
                    ->label('Total Telur Pecah')
                    ->numeric()
                    ->searchable()
                    ->sortable(),

                TextColumn::make('notes')
                    ->label('Catatan')
                    ->searchable()
                    ->wrap()
                    ->toggleable(),

                ...TimestampColumns::make(),
            ])
            ->filters([
                TrashedFilter::make()
                    ->native(false)
                    ->visible(fn (): bool => auth()->user()->hasRole(RoleEnum::DEVELOPER)),
            ])
            ->recordActions([
                EditAction::make()
                    ->label('Ubah')
                    ->modalWidth(Width::Large)
                    ->modalHeading(fn (EggCollection $record): string => 'Ubah '.$record->production_date->translatedFormat('l, d F Y H:i')),

                DeleteAction::make(),

                ForceDeleteAction::make(),

                RestoreAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    ...DefaultBulkActions::make('Produksi Telur'),
                ]),
            ])
            ->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' => ManageEggCollections::route('/'),
        ];
    }
}
