<?php

namespace App\Filament\Resources\Feed\FeedPurchases\Tables;

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 Filament\Actions\BulkActionGroup;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;

class FeedPurchasesTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('reference_number')
                    ->label('No. Referensi')
                    ->searchable()
                    ->sortable(),

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

                TextColumn::make('items_summary')
                    ->label('Item Pakan')
                    ->getStateUsing(function ($record): array {
                        return $record->items
                            ->map(function ($item) {
                                $feedName = $item->feed?->name ?? '-';
                                $feedUnit = $item->feed?->unit?->name ?? '-';
                                $quantity = number_format($item->quantity, 0, ',', '.');
                                $subtotal = $item->formatted_subtotal;

                                return sprintf(
                                    '<div class="flex items-center justify-between gap-1 p-1">
                                        <div class="flex flex-col">
                                            <span class="font-semibold text-gray-900 dark:text-gray-100">
                                                %s
                                            </span>
                                            <span class="text-xs text-gray-500">
                                                x%s %s
                                            </span>
                                        </div>

                                        <div class="text-sm whitespace-nowrap">
                                            Rp %s
                                        </div>
                                    </div>',
                                    e($feedName),
                                    $quantity,
                                    $feedUnit,
                                    $subtotal,
                                );
                            })
                            ->toArray();
                    })
                    ->listWithLineBreaks()
                    ->html(),

                TextColumn::make('total_price')
                    ->label('Total Harga')
                    ->money('IDR', decimalPlaces: 0)
                    ->sortable(),

                TextColumn::make('note')
                    ->label('Catatan')
                    ->wrap(),

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

                DeleteAction::make(),

                ForceDeleteAction::make(),

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