<?php

namespace App\Data\Pagarme\Request\CustomerRequestData;

use App\Data\Pagarme\PagarmeData;
use App\Data\Pagarme\Request\CustomerRequestData\CustomerPhonesRequestData\CustomerPhonesRequestData;

final readonly class CustomerRequestData extends PagarmeData
{
    public function __construct(
        public string $name,
        public string $email,
        public string $document,
        public string $type,
        public string $documentType,
        public string $code,
        public ?CustomerAddressRequestData $address = null,
        public ?CustomerPhonesRequestData  $phones  = null,
    ) {
        self::requireFilled($this->name, 'name');
        self::requireFilled($this->email, 'email');
        self::requireFilled($this->document, 'document');
        self::requireIn($this->type, ['individual', 'company'], 'type');
        self::requireIn($this->documentType, ['CPF', 'CNPJ'], 'document_type');
        self::requireFilled($this->code, 'code');
    }

    public function toArray(): array
    {
        return $this->filterFilledRecursive([
            'name'          => $this->name,
            'email'         => $this->email,
            'document'      => $this->document,
            'type'          => $this->type,
            'document_type' => $this->documentType,
            'code'          => $this->code,
            'address'       => $this->address,
            'phones'        => $this->phones,
        ]);
    }
}
