<?php

namespace App\Filament\Resources;

use App\Filament\Resources\AppSettingResource\Pages;
use App\Models\AppSetting;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;

class AppSettingResource extends Resource
{
    protected static ?string $model = AppSetting::class;

    protected static ?string $navigationIcon = 'heroicon-o-cog-6-tooth';

    protected static ?string $navigationLabel = 'Ajustes de App';

    protected static ?string $modelLabel = 'Ajuste';

    protected static ?string $pluralModelLabel = 'Ajustes de App';

    protected static ?string $navigationGroup = 'Configuración de Sistema';

    public static function canCreate(): bool
    {
        return \App\Models\AppSetting::count() === 0;
    }

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

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Section::make('Ajustes del Sistema')
                    ->schema([
                        Forms\Components\Toggle::make('wallet_enabled')
                            ->label('Activar Billetera')
                            ->helperText('Habilita o deshabilita el uso de la billetera en la app (recargas, retiros, saldo).')
                            ->default(true),
                        Forms\Components\Toggle::make('direct_payment_enabled')
                            ->label('Activar Pago Directo')
                            ->helperText('Permite pagar directamente por tarjeta o SINPE al comprar boletos sin recargar la billetera.')
                            ->default(false),
                    ])
                    ->columns(2),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\IconColumn::make('wallet_enabled')
                    ->label('Billetera Activa')
                    ->boolean(),
                Tables\Columns\IconColumn::make('direct_payment_enabled')
                    ->label('Pago Directo Activo')
                    ->boolean(),
                Tables\Columns\TextColumn::make('updated_at')
                    ->label('Última Actualización')
                    ->dateTime('d/m/Y H:i'),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                //
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListAppSettings::route('/'),
            'edit' => Pages\EditAppSetting::route('/{record}/edit'),
        ];
    }
}
