<?php

declare(strict_types=1);

namespace App\Filament\Components\Form;

use App\Models\Payment;
use Filament\Forms\Components\Repeater;
use Morilog\Jalali\Jalalian;

class PaymentsRepeater extends Repeater
{
    public static function create(): static
    {
        return parent::make('payments')
            ->label('پرداخت‌ها')
            ->relationship()
            ->schema([
                TransactionIdTextInput::create(),
                GatewayTextInput::create(),
            ])
            ->columns(2)
            ->collapsed()
            ->itemLabel(function (array $state): ?string {
                if (! $state['id']) {
                    return null;
                }

                $payment = Payment::find($state['id']);
                if (! $payment) {
                    return null;
                }

                $status = Payment::VALID_STATUSES[$payment->getOriginal('status')] ?? null;
                $createdAt = Jalalian::fromCarbon($payment->created_at)->format('H:i Y/m/d');
                $updatedAt = Jalalian::fromCarbon($payment->updated_at)->format('H:i Y/m/d');

                return "{$status} | {$createdAt} | {$updatedAt}";
            })
            ->columnSpan(12);
    }
}
