<?php

namespace App\Filament\Resources;

use App\Exports\PurchaseHistoryExporter;
use App\Filament\Clusters\WeeklyOps\WeeklyOpsCluster;
use App\Filament\Resources\PurchaseHistoryResource\Pages;
use App\Models\PurchaseHistory;
use BackedEnum;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\ExportBulkAction;
use Filament\Actions\ViewAction;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;

class PurchaseHistoryResource extends Resource
{
    protected static ?string $model = PurchaseHistory::class;

    protected static ?string $cluster = WeeklyOpsCluster::class;

    protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-receipt-refund';

    protected static ?int $navigationSort = 20;

    public static function getNavigationGroup(): string
    {
        return __('admin.nav.commercial');
    }

    public static function form(Schema $form): Schema
    {
        return $form->schema([
            Forms\Components\Select::make('tenant_id')
                ->label(__('admin.purchase_history.fields.tenant'))
                ->relationship('tenant', 'name')
                ->searchable()
                ->preload()
                ->required(),
            Forms\Components\TextInput::make('package_id')
                ->label(__('admin.purchase_history.fields.package'))
                ->required()
                ->maxLength(255),
            Forms\Components\TextInput::make('price')
                ->label(__('admin.purchase_history.fields.price'))
                ->numeric()
                ->required(),
            Forms\Components\TextInput::make('currency')
                ->label(__('admin.purchase_history.fields.currency'))
                ->maxLength(3)
                ->default('EUR'),
            Forms\Components\TextInput::make('platform')
                ->label(__('admin.purchase_history.fields.platform'))
                ->maxLength(50)
                ->required(),
            Forms\Components\TextInput::make('transaction_id')
                ->label(__('admin.purchase_history.fields.transaction_id'))
                ->maxLength(255),
            Forms\Components\DateTimePicker::make('purchased_at')
                ->label(__('admin.purchase_history.fields.purchased_at'))
                ->required(),
        ])->columns(2);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('tenant.name')
                    ->label(__('admin.purchase_history.fields.tenant'))
                    ->sortable()
                    ->searchable(),
                Tables\Columns\TextColumn::make('package_id')
                    ->label(__('admin.purchase_history.fields.package'))
                    ->badge()
                    ->sortable()
                    ->searchable(),
                Tables\Columns\TextColumn::make('price')
                    ->label(__('admin.purchase_history.fields.price'))
                    ->formatStateUsing(fn ($state, PurchaseHistory $record): string => number_format((float) $state, 2).' '.($record->currency ?? 'EUR'))
                    ->sortable(),
                Tables\Columns\TextColumn::make('platform')
                    ->label(__('admin.purchase_history.fields.platform'))
                    ->badge()
                    ->formatStateUsing(function ($state): string {
                        $key = 'admin.purchase_history.platforms.'.(string) $state;
                        $translated = __($key);

                        return $translated === $key ? Str::headline((string) $state) : $translated;
                    })
                    ->sortable()
                    ->searchable(),
                Tables\Columns\TextColumn::make('transaction_id')
                    ->label(__('admin.purchase_history.fields.transaction_id'))
                    ->copyable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('purchased_at')
                    ->label(__('admin.purchase_history.fields.purchased_at'))
                    ->dateTime()
                    ->sortable(),
            ])
            ->filters([
                Tables\Filters\Filter::make('purchased_at')
                    ->label(__('admin.purchase_history.filters.purchased_at'))
                    ->form([
                        Forms\Components\DatePicker::make('from')->label(__('admin.common.from')),
                        Forms\Components\DatePicker::make('until')->label(__('admin.common.until')),
                    ])
                    ->query(function (Builder $query, array $data): Builder {
                        return $query
                            ->when(
                                $data['from'] ?? null,
                                fn (Builder $builder, $date): Builder => $builder->whereDate('purchased_at', '>=', $date),
                            )
                            ->when(
                                $data['until'] ?? null,
                                fn (Builder $builder, $date): Builder => $builder->whereDate('purchased_at', '<=', $date),
                            );
                    }),
                Tables\Filters\SelectFilter::make('platform')
                    ->label(__('admin.purchase_history.filters.platform'))
                    ->options([
                        'ios' => __('admin.purchase_history.platforms.ios'),
                        'android' => __('admin.purchase_history.platforms.android'),
                        'web' => __('admin.purchase_history.platforms.web'),
                        'manual' => __('admin.purchase_history.platforms.manual'),
                    ]),
                Tables\Filters\SelectFilter::make('currency')
                    ->label(__('admin.purchase_history.filters.currency'))
                    ->options([
                        'EUR' => 'EUR',
                        'USD' => 'USD',
                    ]),
                Tables\Filters\SelectFilter::make('tenant_id')
                    ->label(__('admin.purchase_history.filters.tenant'))
                    ->relationship('tenant', 'name')
                    ->searchable(),
            ])
            ->actions([
                ViewAction::make(),
            ])
            ->bulkActions([
                BulkActionGroup::make([
                    ExportBulkAction::make()
                        ->label(__('admin.purchase_history.actions.export'))
                        ->exporter(PurchaseHistoryExporter::class),
                ]),
            ]);
    }

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

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

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

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

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