<?php
declare(strict_types=1);

namespace App\Core\Events;

final class PaymentCompleted extends Event
{
    public function __construct(
        public readonly int $paymentId,
        public readonly int $receiptId,
        public readonly string $receiptNumber,
        public readonly int $memberId,
        public readonly string $paymentType,
        public readonly string $amount,
        public readonly string $method,
    ) {}

    public static function eventName(): string
    {
        return 'payment.completed';
    }

    public function toArray(): array
    {
        return [
            'payment_id' => $this->paymentId,
            'receipt_id' => $this->receiptId,
            'receipt_number' => $this->receiptNumber,
            'member_id' => $this->memberId,
            'payment_type' => $this->paymentType,
            'type' => $this->paymentType,
            'amount' => $this->amount,
            'method' => $this->method,
        ];
    }

    public static function from(array $data): static
    {
        return new self(
            paymentId: (int) ($data['payment_id'] ?? 0),
            receiptId: (int) ($data['receipt_id'] ?? 0),
            receiptNumber: (string) ($data['receipt_number'] ?? ''),
            memberId: (int) ($data['member_id'] ?? 0),
            paymentType: (string) ($data['payment_type'] ?? $data['type'] ?? ''),
            amount: (string) ($data['amount'] ?? '0.00'),
            method: (string) ($data['method'] ?? 'cash'),
        );
    }
}
