<?php

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

use Filament\Actions\Action;
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;

class UserInfolist
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextEntry::make('name'),

                TextEntry::make('email')
                    ->label('Email address'),

                TextEntry::make('email_verified_at')
                    ->dateTime()
                    ->placeholder('-'),

                TextEntry::make('created_at')
                    ->dateTime()
                    ->placeholder('-'),

                TextEntry::make('updated_at')
                    ->dateTime()
                    ->placeholder('-'),


                /*
                |--------------------------------------------------------------------------
                | Wallet Section
                |--------------------------------------------------------------------------
                */

                Section::make('Wallet Information')
                    ->components([

                        // Show if user HAS wallet
                        TextEntry::make('wallet.balance')
                            ->label('Balance')
                            ->visible(fn ($record) => $record->wallet !== null),

                        TextEntry::make('wallet.is_active')
                            ->label('Active')
                            ->formatStateUsing(fn ($state) => $state ? 'Yes' : 'No')
                            ->visible(fn ($record) => $record->wallet !== null),

                        // Button appears if user DOES NOT have wallet
                        Action::make('createWallet')
                            ->label('Create Wallet')
                            ->visible(fn ($record) => $record->wallet === null)
                            ->action(function ($record) {
                                $record->wallet()->create([
                                    'balance' => 0,
                                    'is_active' => true,
                                ]);
                            }),

                    ])
                    ->columnSpanFull(),
            ]);
    }
}
