<?php

namespace App\Filament\Resources\Visitors;

use App\Filament\Resources\Visitors\Pages\ManageVisitors;
use App\Models\Visitor;
use BackedEnum;
use Filament\Forms\Components\DatePicker;
use Filament\Resources\Resource;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use UnitEnum;

class VisitorResource extends Resource
{
    protected static ?string $model = Visitor::class;

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

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

    protected static ?string $navigationLabel = 'Pengunjung';

    protected static ?int $navigationSort = 4;

    protected static ?string $slug = 'monitoring/visitors';

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('date')
                    ->label('Tanggal & Waktu')
                    ->date('l, d F Y')
                    ->sortable(),

                TextColumn::make('route')
                    ->label('Halaman')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('method')
                    ->label('Method')
                    ->badge()
                    ->color(fn (string $state): string => match ($state) {
                        'GET' => 'success',
                        'POST' => 'warning',
                        default => 'gray',
                    }),

                TextColumn::make('status')
                    ->label('Status')
                    ->badge()
                    ->color(fn (int $state): string => $state >= 200 && $state < 300 ? 'success' : 'danger'),

                TextColumn::make('ip')
                    ->label('IP Address')
                    ->searchable(),

                TextColumn::make('counter')
                    ->label('Hits')
                    ->numeric()
                    ->sortable(),

                TextColumn::make('user.name')
                    ->label('User')
                    ->placeholder('Tamu'),
            ])
            ->defaultSort('date', 'desc')
            ->filters([
                SelectFilter::make('method')
                    ->options([
                        'GET' => 'GET',
                        'POST' => 'POST',
                    ])
                    ->native(false),

                Filter::make('date')
                    ->schema([
                        DatePicker::make('from')
                            ->label('Dari')
                            ->placeholder('Pilih Tanggal')
                            ->displayFormat('l, d F Y')
                            ->native(false),

                        DatePicker::make('until')
                            ->label('Sampai')
                            ->placeholder('Pilih Tanggal')
                            ->displayFormat('l, d F Y')
                            ->native(false),
                    ])
                    ->query(function (Builder $query, array $data): Builder {
                        return $query
                            ->when($data['from'], fn (Builder $q): Builder => $q->whereDate('date', '>=', $data['from']))
                            ->when($data['until'], fn (Builder $q): Builder => $q->whereDate('date', '<=', $data['until']));
                    }),
            ])
            ->emptyStateIcon(Heroicon::ChartBar)
            ->emptyStateDescription('Setelah Anda membubat data pertama, maka akan muncul disini.')
            ->defaultSort('date', 'desc')
            ->deferFilters(false);
    }

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