<?php

namespace App\Filament\Resources\Coupons\Schemas;

use App\Enums\CouponStatus;
use App\Enums\CouponType;
use Filament\Infolists\Components\KeyValueEntry;
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Illuminate\Support\Str;

class CouponInfolist
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make(__('Summary'))
                    ->columns(3)
                    ->schema([
                        TextEntry::make('name')->label(__('Name')),
                        TextEntry::make('code')->label(__('Code'))->copyable(),
                        TextEntry::make('status')
                            ->label(__('Status'))
                            ->badge()
                            ->formatStateUsing(fn ($state) => static::formatEnumState($state)),
                        TextEntry::make('type')
                            ->label(__('Discount type'))
                            ->badge()
                            ->formatStateUsing(fn ($state) => static::formatEnumState($state)),
                        TextEntry::make('amount')
                            ->label(__('Amount'))
                            ->formatStateUsing(fn ($state, $record) => $record?->type?->value === 'percentage'
                                ? sprintf('%s%%', $record?->amount ?? $state)
                                : sprintf('%s %s', number_format((float) ($record?->amount ?? $state), 2), strtoupper($record?->currency ?? 'EUR'))),
                        TextEntry::make('redemptions_count')
                            ->label(__('Total redemptions'))
                            ->badge(),
                    ]),
                Section::make(__('Limits & scheduling'))
                    ->columns(2)
                    ->schema([
                        TextEntry::make('usage_limit')->label(__('Global usage limit'))->placeholder('—'),
                        TextEntry::make('per_customer_limit')->label(__('Per tenant limit'))->placeholder('—'),
                        TextEntry::make('starts_at')
                            ->label(__('Starts'))
                            ->dateTime(),
                        TextEntry::make('ends_at')
                            ->label(__('Ends'))
                            ->dateTime(),
                    ]),
                Section::make(__('Packages'))
                    ->schema([
                        TextEntry::make('packages_list')
                            ->label(__('Limited to packages'))
                            ->state(fn ($record) => $record?->packages?->pluck('name')->all() ?: null)
                            ->listWithLineBreaks()
                            ->placeholder(__('All packages')),
                    ]),
                Section::make(__('Metadata'))
                    ->schema([
                        TextEntry::make('description')->label(__('Description'))->columnSpanFull(),
                        KeyValueEntry::make('metadata')->label(__('Metadata'))->columnSpanFull(),
                    ]),
                Section::make(__('Paddle'))
                    ->columns(3)
                    ->schema([
                        TextEntry::make('paddle_discount_id')
                            ->label(__('Discount ID'))
                            ->copyable()
                            ->placeholder('—'),
                        TextEntry::make('paddle_last_synced_at')
                            ->label(__('Last synced'))
                            ->state(fn ($record) => $record?->paddle_last_synced_at?->diffForHumans() ?? '—'),
                        TextEntry::make('paddle_mode')
                            ->label(__('Mode'))
                            ->badge()
                            ->placeholder('standard'),
                    ]),
            ]);
    }

    public static function formatEnumState(mixed $state): string
    {
        if ($state instanceof CouponType || $state instanceof CouponStatus) {
            return $state->label();
        }

        if ($state instanceof \BackedEnum) {
            return Str::headline($state->value);
        }

        if (is_string($state)) {
            return Str::headline($state);
        }

        return '';
    }
}
