<?php

namespace App\DTOs\ProductOwner;

use Illuminate\Support\Carbon;

class ModulePurchaseResponseData
{
    public function __construct(
        public bool $success,
        public ?string $message = null,
        public ?float $amountCharged = null,
        public ?float $walletBalance = null,
        public ?string $currency = null,
        public ?string $referenceNumber = null,
        public ?float $openingBalance = null,
        public ?float $closingBalance = null,
        public ?string $transactionType = null,
        public ?string $ledgerDescription = null,
        public ?Carbon $recordedAt = null,
    ) {}

    public static function fromArray(array $data): self
    {
        $ledger = $data['ledger'] ?? null;

        return new self(
            success: (bool) ($data['success'] ?? false),
            message: $data['message'] ?? null,
            amountCharged: isset($data['amount_charged']) ? (float) $data['amount_charged'] : null,
            walletBalance: isset($data['wallet_balance']) ? (float) $data['wallet_balance'] : null,
            currency: $data['currency'] ?? null,
            referenceNumber: $ledger['reference_number'] ?? $data['reference_number'] ?? null,
            openingBalance: isset($ledger['opening_balance']) ? (float) $ledger['opening_balance'] : null,
            closingBalance: isset($ledger['closing_balance']) ? (float) $ledger['closing_balance'] : null,
            transactionType: $ledger['transaction_type'] ?? $data['transaction_type'] ?? null,
            ledgerDescription: $ledger['description'] ?? $data['description'] ?? null,
            recordedAt: isset($ledger['recorded_at']) ? Carbon::parse($ledger['recorded_at']) : null,
        );
    }
}
