<?php

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

use Filament\Actions\Action;
use Filament\Infolists\Components\IconEntry;
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Illuminate\Support\Facades\Http;

class TopUpInfolist
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                 /*
            |--------------------------------------------------------------------------
            | Payment Overview
            |--------------------------------------------------------------------------
            */
            Section::make('Payment Overview')
                ->schema([
                    Grid::make(4)->schema([

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

                        TextEntry::make('pm_type')
                            ->label('Payment Method')
                            ->badge()
                            ->color(fn (string $state) =>
                                $state === 'stripe' ? 'success' : 'gray'
                            ),

                        TextEntry::make('status')
                            ->label('Status')
                            ->badge()
                            ->color(fn (string $state) => match ($state) {
                                'pending'   => 'warning',
                                'completed' => 'success',
                                'failed'    => 'danger',
                                default     => 'gray',
                            }),

                        IconEntry::make('is_paid')
                            ->label('Paid')
                            ->boolean(),
                    ]),
                ]),

            /*
            |--------------------------------------------------------------------------
            | Customer Information
            |--------------------------------------------------------------------------
            */
            Section::make('Customer Information')
                ->schema([
                    Grid::make(2)->schema([

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

                        TextEntry::make('phone')
                            ->label('Phone Number')
                            ->copyable()
                           ,
                    ]),
                ]),

            /*
            |--------------------------------------------------------------------------
            | 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('execute_payment')
                        ->label('Execute Payment')
                        ->icon('heroicon-o-bolt')
                        ->color('primary')
                        ->requiresConfirmation()
                        ->visible(fn ($record) =>
                           $record->status === 'pending'
                        )
                        ->action(function ($record) {
                            $response = Http::asForm()
                                ->timeout(20)
                                ->post('https://top-up.api.zarsoft.net/raht-pay/api.php', [
                                    'username' => config('services.topup.username'),
                                    'password' => config('services.topup.password'),
                                    'phone'    => $record->phone,
                                    'amount'   => $record->amount,
                                ]);

                            if ($response->successful()) {
                                $record->update([
                                    'status' => 'completed',
                                    'is_paid' => true,
                                ]);
                            }
                        }),

                    Action::make('mark_completed')
                        ->label('Mark as Completed')
                        ->icon('heroicon-o-check-circle')
                        ->color('success')
                        ->requiresConfirmation()
                        ->visible(fn ($record) =>
                            ! $record->is_paid && $record->status !== 'completed'
                        )
                        ->action(function ($record) {
                            $record->update([
                                'status' => 'completed',
                                'is_paid' => true,
                            ]);
                        }),
                ])
                ->columns(2),
            ]);
    }
}
