<?php

namespace App\Filament\Resources\GiftVoucherResource\Pages;

use App\Filament\Resources\GiftVoucherResource;
use App\Services\Audit\SuperAdminAuditLogger;
use App\Services\GiftVouchers\GiftVoucherService;
use Filament\Actions\Action;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Support\Str;

class ListGiftVouchers extends ListRecords
{
    protected static string $resource = GiftVoucherResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Action::make('issue')
                ->label('Gutschein ausstellen')
                ->icon('heroicon-o-plus')
                ->form([
                    TextInput::make('amount')
                        ->label('Betrag')
                        ->numeric()
                        ->required(),
                    TextInput::make('currency')
                        ->label('Währung')
                        ->default('EUR')
                        ->maxLength(3)
                        ->required(),
                    TextInput::make('purchaser_email')->label('E-Mail Käufer')->required(),
                    TextInput::make('recipient_email')->label('E-Mail Empfänger'),
                    TextInput::make('recipient_name')->label('Name Empfänger'),
                    TextInput::make('message')
                        ->label('Nachricht')
                        ->maxLength(500),
                    TextInput::make('code')
                        ->label('Code (optional)')
                        ->placeholder('GIFT-'.Str::upper(Str::random(8))),
                    Placeholder::make('code_hint')
                        ->label('')
                        ->content('Wenn kein Code eingetragen wird, erzeugen wir automatisch einen eindeutigen Gutscheincode.'),
                ])
                ->action(function (array $data, GiftVoucherService $service): void {
                    $payload = [
                        'id' => null,
                        'metadata' => [
                            'type' => 'gift_voucher',
                            'purchaser_email' => $data['purchaser_email'],
                            'recipient_email' => $data['recipient_email'] ?? null,
                            'recipient_name' => $data['recipient_name'] ?? null,
                            'message' => $data['message'] ?? null,
                            'gift_code' => $data['code'] ?? null,
                        ],
                        'currency_code' => $data['currency'] ?? 'EUR',
                        'totals' => [
                            'grand_total' => [
                                'amount' => (float) $data['amount'],
                            ],
                        ],
                    ];

                    $voucher = $service->issueFromPaddle($payload);

                    app(SuperAdminAuditLogger::class)->recordModelMutation(
                        'issued',
                        $voucher,
                        SuperAdminAuditLogger::fieldsMetadata([
                            'amount',
                            'currency',
                            'status',
                            'expires_at',
                            'coupon_id',
                        ]),
                        source: static::class
                    );
                })
                ->modalHeading('Geschenkgutschein ausstellen'),
        ];
    }
}
