<?php

namespace App\Filament\Resources;

use App\Filament\Resources\OfferResource\Pages;
use App\Models\Offer;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

class OfferResource extends Resource
{
    protected static ?string $model = Offer::class;

    protected static ?string $navigationIcon = 'heroicon-o-sparkles';
    protected static ?string $navigationLabel = 'Ofertas';
    protected static ?string $navigationGroup = 'Funnel TAXILANZ';
    protected static ?string $modelLabel = 'Oferta';
    protected static ?string $pluralModelLabel = 'Ofertas';
    protected static ?int $navigationSort = 1;

    public static function getNavigationBadge(): ?string
    {
        return (string) static::getModel()::count();
    }

    public static function getNavigationBadgeColor(): ?string
    {
        return 'warning';
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('uuid')->label('UUID')->required()->maxLength(36),
                Forms\Components\TextInput::make('title')->label('Título')->required()->maxLength(255),
                Forms\Components\TextInput::make('slug')->required()->maxLength(255),
                Forms\Components\TextInput::make('category')->label('Categoría')->required(),
                Forms\Components\TextInput::make('excerpt')->label('Resumen')->maxLength(255)->default(null),
                Forms\Components\Textarea::make('description')->label('Descripción')->columnSpanFull(),
                Forms\Components\TextInput::make('location_label')->label('Ubicación')->maxLength(255)->default(null),
                Forms\Components\TextInput::make('lat')->numeric()->default(null),
                Forms\Components\TextInput::make('lng')->numeric()->default(null),
                Forms\Components\TextInput::make('price_from')->label('Precio desde')->numeric()->default(null),
                Forms\Components\TextInput::make('duration_minutes')->label('Duración')->numeric()->default(null),
                Forms\Components\FileUpload::make('image_url')->label('Imagen')->image(),
                Forms\Components\TextInput::make('status')->label('Estado')->required(),
                Forms\Components\Toggle::make('is_featured')->label('Destacada')->required(),
                Forms\Components\TextInput::make('priority_score')->label('Prioridad')->required()->numeric()->default(0),
                Forms\Components\Toggle::make('available_now')->label('Disponible ahora')->required(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->defaultSort('priority_score', 'desc')
            ->columns([
                Tables\Columns\TextColumn::make('title')
                    ->label('Oferta')
                    ->searchable()
                    ->weight('semibold')
                    ->description(fn (Offer $record): string => $record->excerpt ?: ($record->location_label ?: 'Sin resumen')),
                Tables\Columns\TextColumn::make('category')
                    ->label('Categoría')
                    ->badge()
                    ->formatStateUsing(fn (?string $state): string => ucfirst($state ?: 'general'))
                    ->color('warning'),
                Tables\Columns\TextColumn::make('location_label')
                    ->label('Ubicación')
                    ->badge()
                    ->toggleable(),
                Tables\Columns\TextColumn::make('price_from')
                    ->label('Desde')
                    ->money('EUR')
                    ->sortable(),
                Tables\Columns\TextColumn::make('duration_minutes')
                    ->label('Duración')
                    ->suffix(' min')
                    ->sortable(),
                Tables\Columns\TextColumn::make('status')
                    ->label('Estado')
                    ->badge()
                    ->color(fn (?string $state): string => match ($state) {
                        'active' => 'success',
                        'draft' => 'warning',
                        'archived' => 'gray',
                        default => 'primary',
                    }),
                Tables\Columns\IconColumn::make('available_now')
                    ->label('Ahora')
                    ->boolean(),
                Tables\Columns\IconColumn::make('is_featured')
                    ->label('Destacada')
                    ->boolean(),
                Tables\Columns\TextColumn::make('priority_score')
                    ->label('Prioridad')
                    ->badge()
                    ->sortable(),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('Creada')
                    ->since()
                    ->sortable(),
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

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

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