<?php

declare(strict_types=1);

namespace App\Filament\Resources\TrialBookings;

use App\Filament\Resources\TrialBookings\Pages\ListTrialBookings;
use App\Models\TrialBooking;
use App\Models\User;
use BackedEnum;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use UnitEnum;

class TrialBookingResource extends Resource
{
    protected static ?string $model = TrialBooking::class;

    protected static ?string $slug = 'trial-bookings';

    protected static ?string $navigationLabel = '免費體驗預約';

    protected static string|UnitEnum|null $navigationGroup = '諮詢管理';

    protected static ?int $navigationSort = 5;

    protected static ?string $modelLabel = '免費體驗預約';

    protected static ?string $pluralModelLabel = '免費體驗預約';

    protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-clipboard-document-check';

    public static function canViewAny(): bool
    {
        $user = Auth::user();

        return $user instanceof User
            && $user->hasAnyRole(['super_admin', 'admin']);
    }

    public static function canCreate(): bool
    {
        return false;
    }

    public static function canEdit(Model $record): bool
    {
        return false;
    }

    public static function canDelete(Model $record): bool
    {
        return false;
    }

    public static function form(Schema $schema): Schema
    {
        return $schema->components([]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')
                    ->label('姓名')
                    ->searchable(),

                TextColumn::make('phone')
                    ->label('手機')
                    ->searchable(),

                TextColumn::make('goal')
                    ->label('目標')
                    ->wrap()
                    ->searchable(),

                TextColumn::make('availability')
                    ->label('可上課時段')
                    ->formatStateUsing(function ($state): string {
                        if (is_array($state)) {
                            return implode(', ', $state);
                        }

                        return (string) $state;
                    })
                    ->wrap(),

                TextColumn::make('level')
                    ->label('程度')
                    ->default('—'),

                TextColumn::make('has_experience')
                    ->label('有無經驗')
                    ->formatStateUsing(fn (?string $state): string => $state === 'yes' ? '有' : ($state === 'no' ? '無' : '—')),

                TextColumn::make('created_at')
                    ->label('送出時間')
                    ->dateTime('Y-m-d H:i')
                    ->sortable(),
            ])
            ->actions([])
            ->defaultSort('created_at', 'desc');
    }

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