<?php

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

use App\Models\Setting;
use Filament\Infolists\Components\IconEntry;
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;

class OrderProductInfolist
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([

                // ---------------------------------------------------------
                // ORDER DETAILS
                // ---------------------------------------------------------
                Section::make('Order Details')
                    ->columns(2)
                    ->components([

                        // Payment Method with Colors
                        TextEntry::make('pm_type')
                            ->label('Payment Method')
                            ->formatStateUsing(fn ($state) => ucfirst($state))
                            ->extraAttributes(fn ($state) => [
                                'class' =>
                                    'px-2 py-1 rounded text-white text-sm inline-block ' .
                                    match ($state) {
                                        'wallet' => 'bg-blue-600',
                                        'card'   => 'bg-green-600',
                                        'cash'   => 'bg-amber-600',
                                        default  => 'bg-gray-500',
                                    }
                            ]),

                        // Status With Badge Colors
                        TextEntry::make('status')
                            ->label('Status')
                            ->formatStateUsing(fn ($state) => ucfirst($state))
                            ->extraAttributes(fn ($state) => [
                                'class' =>
                                    'px-2 py-1 rounded text-white text-sm inline-block ' .
                                    match ($state) {
                                        'pending'  => 'bg-yellow-500',
                                        'paid'     => 'bg-green-600',
                                        'failed'   => 'bg-red-600',
                                        'refunded' => 'bg-purple-600',
                                        default    => 'bg-gray-500',
                                    }
                            ]),

                        TextEntry::make('quantity')
                            ->numeric(),

                        // Total Price With Currency Prefix
                        TextEntry::make('total_price')
                            ->label('Total Price')
                            ->formatStateUsing(function ($state) {
                                $currency = Setting::where('key', 'currency')->value('value')
                                    ?: Setting::where('name', 'currency')->value('value')
                                    ?: config('app.currency')
                                    ?: 'USD';

                                return $currency . ' ' . number_format((float) $state, 2);
                            }),

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

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

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


                // ---------------------------------------------------------
                // CUSTOMER
                // ---------------------------------------------------------
                Section::make('Customer')
                    ->components([
                        TextEntry::make('user.name')
                            ->label('Customer Name'),
                    ])
                    ->columnSpanFull(),


                // ---------------------------------------------------------
                // PRODUCT
                // ---------------------------------------------------------
                Section::make('Product')
                    ->components([
                        TextEntry::make('product.name')
                            ->label('Product Name'),
                    ])
                    ->columnSpanFull(),


                // ---------------------------------------------------------
                // COUPON
                // ---------------------------------------------------------
                Section::make('Coupon Information')
                    ->components([
                        TextEntry::make('coupon.code')
                            ->label('Coupon Code')
                            ->placeholder('No coupon applied'),
                    ])
                    ->columnSpanFull(),

            ]);
    }
}
