<?php

namespace App\Filament\Resources\TenantResource\RelationManagers;

use App\Services\Audit\SuperAdminAuditLogger;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\ViewAction;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Collection;

class PurchasesRelationManager extends RelationManager
{
    protected static string $relationship = 'eventPurchases';

    protected static ?string $title = 'Käufe';

    public function form(Schema $form): Schema
    {
        return $form
            ->schema([
                Select::make('package_id')
                    ->label('Paket')
                    ->options([
                        'starter_pack' => 'Starter Pack (5 Events)',
                        'pro_pack' => 'Pro Pack (20 Events)',
                        'lifetime_unlimited' => 'Lifetime Unlimited',
                        'monthly_pro' => 'Pro Subscription',
                        'monthly_agency' => 'Agency Subscription',
                    ])
                    ->required(),
                TextInput::make('credits_added')
                    ->label('Credits hinzugefügt')
                    ->numeric()
                    ->required()
                    ->minValue(0),
                TextInput::make('price')
                    ->label('Preis')
                    ->numeric()
                    ->step(0.01)
                    ->prefix('€')
                    ->required(),
                Select::make('platform')
                    ->label('Plattform')
                    ->options([
                        'ios' => 'iOS',
                        'android' => 'Android',
                        'web' => 'Web',
                        'manual' => 'Manuell',
                    ])
                    ->required(),
                TextInput::make('transaction_id')
                    ->label('Transaktions-ID')
                    ->maxLength(255),
                Textarea::make('reason')
                    ->label('Beschreibung')
                    ->maxLength(65535)
                    ->columnSpanFull(),
            ])
            ->columns(2);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('package_id')
            ->columns([
                TextColumn::make('package_id')
                    ->badge()
                    ->color(fn (string $state): string => match ($state) {
                        'starter_pack' => 'info',
                        'pro_pack' => 'success',
                        'lifetime_unlimited' => 'danger',
                        'monthly_pro' => 'warning',
                        default => 'gray',
                    }),
                TextColumn::make('credits_added')
                    ->label('Credits')
                    ->badge()
                    ->color('success'),
                TextColumn::make('price')
                    ->money('EUR'),
                TextColumn::make('platform')
                    ->badge()
                    ->color(fn (string $state): string => match ($state) {
                        'ios' => 'info',
                        'android' => 'success',
                        'web' => 'warning',
                        'manual' => 'gray',
                    }),
                TextColumn::make('purchased_at')
                    ->dateTime()
                    ->sortable(),
                TextColumn::make('transaction_id')
                    ->copyable()
                    ->toggleable(),
            ])
            ->filters([
                SelectFilter::make('platform')
                    ->options(['ios' => 'iOS', 'android' => 'Android', 'web' => 'Web', 'manual' => 'Manuell']),
                SelectFilter::make('package_id')
                    ->options([
                        'starter_pack' => 'Starter Pack',
                        'pro_pack' => 'Pro Pack',
                        'lifetime_unlimited' => 'Lifetime',
                        'monthly_pro' => 'Pro Subscription',
                        'monthly_agency' => 'Agency Subscription',
                    ]),
            ])
            ->headerActions([])
            ->actions([
                ViewAction::make(),
            ])
            ->bulkActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make()
                        ->after(function (Collection $records): void {
                            $logger = app(SuperAdminAuditLogger::class);

                            foreach ($records as $record) {
                                $logger->recordModelMutation(
                                    'deleted',
                                    $record,
                                    source: static::class
                                );
                            }
                        }),
                ]),
            ]);
    }
}
