<?php

namespace App\Filament\Widgets;

use App\Models\Attendance;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as BaseWidget;

class RecentActivityWidget extends BaseWidget
{
    protected static ?string $heading = 'Kehadiran Hari Ini';

    protected static ?int $sort = 4;

    protected int|string|array $columnSpan = [
        'md' => 2,
        'xl' => 2,
    ];

    protected static ?string $pollingInterval = '30s';

    public function table(Table $table): Table
    {
        return $table
            ->query(
                Attendance::query()
                    ->with(['user', 'office'])
                    ->whereDate('date', today())
                    // Hide super_admin
                    ->whereHas('user', fn ($q) => $q->whereDoesntHave('roles', fn ($r) => $r->where('name', 'super_admin')))
                    ->latest('check_in_time')
            )
            ->columns([
                Tables\Columns\ImageColumn::make('user.avatar')
                    ->label('')
                    ->circular()
                    ->size(40)
                    ->defaultImageUrl(fn($record) => 'https://ui-avatars.com/api/?name=' . urlencode($record->user->name ?? 'U') . '&background=1565C0&color=fff&size=100'),

                Tables\Columns\TextColumn::make('user.name')
                    ->label('Nama')
                    ->weight('bold')
                    ->searchable()
                    ->sortable(),

                Tables\Columns\TextColumn::make('user.department')
                    ->label('Departemen')
                    ->color('gray')
                    ->placeholder('-'),

                Tables\Columns\TextColumn::make('check_in_time')
                    ->label('Masuk')
                    ->time('H:i')
                    ->icon('heroicon-o-arrow-right-circle')
                    ->iconColor('success')
                    ->sortable(),

                Tables\Columns\TextColumn::make('check_out_time')
                    ->label('Pulang')
                    ->time('H:i')
                    ->icon('heroicon-o-arrow-left-circle')
                    ->iconColor('danger')
                    ->placeholder('--:--')
                    ->sortable(),

                Tables\Columns\TextColumn::make('type')
                    ->label('Tipe')
                    ->badge()
                    ->formatStateUsing(fn(string $state): string => match ($state) {
                        'office' => 'Kantor',
                        'wfh' => 'WFH',
                        'business_trip' => 'Dinas',
                        default => $state,
                    })
                    ->color(fn(string $state): string => match ($state) {
                        'office' => 'primary',
                        'wfh' => 'info',
                        'business_trip' => 'warning',
                        default => 'gray',
                    }),

                Tables\Columns\TextColumn::make('status')
                    ->label('Status')
                    ->badge()
                    ->formatStateUsing(fn(string $state): string => match ($state) {
                        'present' => 'Hadir',
                        'late' => 'Terlambat',
                        'early_leave' => 'Pulang Awal',
                        'incomplete' => 'Belum Checkout',
                        default => $state,
                    })
                    ->color(fn(string $state): string => match ($state) {
                        'present' => 'success',
                        'late' => 'warning',
                        'early_leave' => 'info',
                        'incomplete' => 'gray',
                        default => 'gray',
                    }),

                Tables\Columns\TextColumn::make('office.name')
                    ->label('Lokasi')
                    ->placeholder('-')
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->defaultPaginationPageOption(10)
            ->emptyStateHeading('Belum ada kehadiran hari ini')
            ->emptyStateDescription('Data kehadiran akan muncul setelah karyawan melakukan check-in.')
            ->emptyStateIcon('heroicon-o-clock')
            ->striped();
    }
}
