<?php

namespace App\Data\Pagarme\Transfer;

use App\Data\Pagarme\PagarmeResponseData;

final readonly class TransferResponseData extends PagarmeResponseData
{
    public function __construct(
        public ?string $id,
        public ?int    $amount,
        public ?string $type,
        public ?string $status,
        public ?int    $fee,
        public ?string $fundingDate,
        public ?string $fundingEstimatedDate,
        public ?array  $bankAccount,
        public ?string $bankResponse,
        public ?string $createdAt,
        public ?array  $metadata,
    ) {}

    //

    public static function fromArray(array $payload): static
    {
        return new self(
            id:                   static::arrString($payload, 'id'),
            amount:               static::arrInt($payload, 'amount'),
            type:                 static::arrString($payload, 'type'),
            status:               static::arrString($payload, 'status'),
            fee:                  static::arrInt($payload, 'fee'),
            fundingDate:          static::arrString($payload, 'funding_date'),
            fundingEstimatedDate: static::arrString($payload, 'funding_estimated_date'),
            bankAccount:          static::arrGet($payload, 'bank_account'),
            bankResponse:         static::arrString($payload, 'bank_response'),
            createdAt:            static::arrString($payload, 'created_at') ?? static::arrString($payload, 'date_created'),
            metadata:             static::arrGet($payload, 'metadata'),
        );
    }

    public function toArray(): array
    {
        return [
            'id'                     => $this->id,
            'amount'                 => $this->amount,
            'type'                   => $this->type,
            'status'                 => $this->status,
            'fee'                    => $this->fee,
            'funding_date'           => $this->fundingDate,
            'funding_estimated_date' => $this->fundingEstimatedDate,
            'bank_account'           => $this->bankAccount,
            'bank_response'          => $this->bankResponse,
            'created_at'             => $this->createdAt,
            'metadata'               => $this->metadata,
        ];
    }
}
