<?php

namespace App\Filament\Resources\Manage\EggCollections\Tables;

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 Filament\Actions\BulkActionGroup;
use Filament\Actions\ViewAction;
use Filament\Infolists\Components\RepeatableEntry;
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Section;
use Filament\Support\Enums\Width;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;

class EggCollectionsTable
{
    public static function configure(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('notes')
                    ->label('Catatan')
                    ->searchable()
                    ->wrap()
                    ->toggleable(),

                ...TimestampColumns::make(),
            ])
            ->filters([
                TrashedFilter::make()
                    ->native(false),
            ])
            ->recordActions([
                EditAction::make()
                    ->label('Ubah'),

                ViewAction::make()
                    ->label('Detail')
                    ->color('gray')
                    ->modalHeading('Detail Produksi Telur')
                    ->modalWidth(Width::Large)
                    ->schema([
                        Section::make('Ringkasan')
                            ->schema([
                                TextEntry::make('production_date')
                                    ->label('Waktu Produksi')
                                    ->dateTime('l, d F Y H:i'),

                                TextEntry::make('total_eggs')
                                    ->label('Total Telur'),

                                TextEntry::make('notes')
                                    ->label('Catatan')
                                    ->placeholder('-')
                                    ->columnSpanFull(),

                                RepeatableEntry::make('items')
                                    ->label('Daftar Ayam')
                                    ->schema([
                                        TextEntry::make('chicken.tag_code')
                                            ->label('Kode Ayam')
                                            ->placeholder('-'),

                                        TextEntry::make('eggs_count')
                                            ->label('Jumlah Telur'),
                                    ])
                                    ->columns(2),
                            ]),
                    ]),

                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);
    }
}
