<?php

namespace App\Filament\Admin\Resources\WalletTransactions\Schemas;

use Filament\Actions\Action;
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Forms\Components\TextInput;
use Illuminate\Support\Facades\Http;
use Stripe\Checkout\Session;
use Stripe\Stripe;


class WalletTransactionInfolist
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                /*
                |--------------------------------------------------------------------------
                | Transaction Overview
                |--------------------------------------------------------------------------
                */
                Section::make('Transaction Overview')
                    ->schema([
                        Grid::make(3)->schema([
                            TextEntry::make('pm_type')
                                ->label('Payment Method')
                                ->badge()
                                ->color(
                                    fn(string $state) =>
                                    $state === 'stripe' ? 'success' : 'gray'
                                ),

                            TextEntry::make('type')
                                ->label('Transaction Type')
                                ->badge()
                                ->color(
                                    fn(string $state) =>
                                    $state === 'credit' ? 'success' : 'danger'
                                ),
                            TextEntry::make('status')
                                ->label('Status')
                                ->badge()
                                ->icon(fn(string $state): string => match ($state) {
                                    'completed' => 'heroicon-o-check-circle',
                                    'pending'   => 'heroicon-o-clock',
                                    'failed'    => 'heroicon-o-x-circle',
                                    default     => 'heroicon-o-question-mark-circle',
                                })
                                ->color(fn(string $state): string => match ($state) {
                                    'completed' => 'success',
                                    'pending'   => 'warning',
                                    'failed'    => 'danger',
                                    default     => 'gray',
                                }),

                            TextEntry::make('amount')
                                ->label('Amount')
                                ->numeric()
                                ->money('USD')
                                ->weight('bold'),
                        ]),
                    ]),

                /*
                |--------------------------------------------------------------------------
                | Relations
                |--------------------------------------------------------------------------
                */
                Section::make('Relations')
                    ->schema([
                        Grid::make(2)->schema([
                            TextEntry::make('wallet.user.name')
                                ->label('Wallet ID')
                                ->copyable(),

                            TextEntry::make('user.name')
                                ->label('User')
                                ->icon('heroicon-o-user'),
                        ]),
                    ]),

                /*
                |--------------------------------------------------------------------------
                | Timestamps
                |--------------------------------------------------------------------------
                */
                Section::make('Timestamps')
                    ->schema([
                        Grid::make(2)->schema([
                            TextEntry::make('created_at')
                                ->label('Created At')
                                ->dateTime()
                                ->placeholder('-'),

                            TextEntry::make('updated_at')
                                ->label('Last Updated')
                                ->dateTime()
                                ->placeholder('-'),
                        ]),
                    ]),

                /*
                |--------------------------------------------------------------------------
                | Actions
                |--------------------------------------------------------------------------
                */
                Section::make('Actions')
                    ->schema([
                        Action::make('stripe_pay')
                            ->label('Pay with Stripe')
                            ->icon('heroicon-o-credit-card')
                            ->color('success')
                            ->requiresConfirmation()
                            ->visible(
                                fn($record) =>
                                $record->pm_type === 'stripe' &&
                                    $record->status === 'pending'
                            )
                            ->action(function ($record) {

                                // 🔐 safety
                                abort_if(
                                    $record->status !== 'pending' ||
                                        $record->pm_type !== 'stripe',
                                    403
                                );

                                Stripe::setApiKey(config('services.stripe.secret'));

                                $session = Session::create([
                                    'mode' => 'payment',
                                    'payment_method_types' => ['card'],
                                    'line_items' => [[
                                        'price_data' => [
                                            'currency' => 'usd',
                                            'unit_amount' => (int) round($record->amount * 100),
                                            'product_data' => [
                                                'name' => 'Wallet Top-up',
                                            ],
                                        ],
                                        'quantity' => 1,
                                    ]],
                                    'success_url' => route('stripe.success') . '?session_id={CHECKOUT_SESSION_ID}',
                                    'cancel_url'  => route('stripe.cancel'),
                                    'metadata' => [
                                        'transaction_id' => $record->id,
                                        'user_id'        => auth()->id(),
                                        'amount'         => $record->amount,
                                    ],
                                ]);

                                return redirect()->away($session->url);
                            }),



                        Action::make('refund')
                            ->label('Refund')
                            ->icon('heroicon-o-arrow-uturn-left')
                            ->color('danger')
                            ->requiresConfirmation()
                            ->visible(
                                fn($record) =>
                                $record->pm_type === 'stripe' && $record->type === 'credit'
                            )
                            ->action(function ($record) {
                                // TODO: call Stripe refund logic
                            }),
                            
                    ])
                    ->columns(2),
            ]);
    }
}
