<?php

namespace App\Http\Livewire;

use App\Models\ActivityLog;
use Filament\Tables;
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Tables\Contracts\HasTable;
use Livewire\Component;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;

class AuditTable extends Component implements HasTable
{
    use InteractsWithTable;

    protected function getTableQuery(): Builder
    {
        return ActivityLog::query()->with('user')->orderBy('created_at', 'desc');
    }

    protected function getTableColumns(): array
    {
        return [
            Tables\Columns\TextColumn::make('id')
                ->label('No')
                ->sortable(),
            
            Tables\Columns\BadgeColumn::make('action')
                ->label('Action')
                ->colors([
                    'success' => 'login',
                    'primary' => 'submitted',
                    'warning' => 'savedraft',
                    'info' => 'updated',
                    'danger' => 'deleted',
                    'secondary' => 'logout',
                ])
                ->formatStateUsing(fn (string $state): string => ucfirst($state))
                ->sortable(),
            
            Tables\Columns\TextColumn::make('description')
                ->label('Log')
                ->searchable()
                ->sortable()
                ->wrap(),
            
            Tables\Columns\TextColumn::make('module')
                ->label('Module')
                ->sortable()
                ->searchable(),
            
            Tables\Columns\TextColumn::make('user.name')
                ->label('User')
                ->sortable()
                ->searchable(),
            
            Tables\Columns\TextColumn::make('created_at')
                ->label('Date')
                ->date('d/m/Y')
                ->sortable(),
        ];
    }

    protected function getTableFilters(): array
    {
        return [
            Tables\Filters\SelectFilter::make('action')
                ->label('Action')
                ->options([
                    'login' => 'Login',
                    'logout' => 'Logout',
                    'savedraft' => 'Save Draft',
                    'submitted' => 'Submitted',
                    'updated' => 'Updated',
                    'deleted' => 'Deleted',
                ]),
            
            Tables\Filters\SelectFilter::make('module')
                ->label('Module')
                ->options([
                    'Login' => 'Login',
                    'Logout' => 'Logout',
                    'Invoice' => 'Invoice',
                    'Daily Distribution' => 'Daily Distribution',
                ]),
            
            Tables\Filters\SelectFilter::make('user_id')
                ->label('User')
                ->relationship('user', 'name')
                ->searchable(),
            
            // Tables\Filters\Filter::make('created_at')
            //     ->form([
            //         Tables\Filters\Components\DatePicker::make('date_from')
            //             ->label('Date From'),
            //         Tables\Filters\Components\DatePicker::make('date_until')
            //             ->label('Date Until'),
            //     ])
            //     ->query(function (Builder $query, array $data): Builder {
            //         return $query
            //             ->when(
            //                 $data['date_from'],
            //                 fn (Builder $query, $date): Builder => $query->whereDate('created_at', '>=', $date),
            //             )
            //             ->when(
            //                 $data['date_until'],
            //                 fn (Builder $query, $date): Builder => $query->whereDate('created_at', '<=', $date),
            //             );
            //     }),
        ];
    }

    protected function getTableActions(): array
    {
        return [
            // No individual actions needed
        ];
    }

    protected function getTableBulkActions(): array
    {
        return [
            Tables\Actions\BulkAction::make('delete')
                ->label('Delete selected')
                ->action(fn (Collection $records) => $records->each->delete())
                ->requiresConfirmation()
                ->modalHeading('Delete Selected Records')
                ->modalSubheading('To delete by date, the user must first filter the records by date. After filtering, they can select all filtered records and perform a bulk delete.')
                ->modalButton('Delete')
                ->color('danger')
                ->icon('heroicon-o-trash')
                ->deselectRecordsAfterCompletion(),
        ];
    }

    // protected function isTableSelectionEnabled(): bool
    // {
    //     return true;
    // }

    public function render(): View
    {
        return view('livewire.audit-table');
    }
}