<?php

declare(strict_types=1);

namespace CloudXM\NFe\DTOs;

use JsonSerializable;

/**
 * Federal Taxation DTO
 * 
 * Represents PIS/COFINS and retention tax data (NEW in v2 API)
 */
class FederalTaxation implements JsonSerializable
{
    public function __construct(
        public ?string $pis_cofins_cst = null,
        public ?float $pis_cofins_vbc = null,
        public ?float $pis_cofins_rate = null,
        public ?float $cofins_rate = null,
        public ?float $pis_cofins_value = null,
        public ?float $cofins_value = null,
        public ?string $pis_cofins_retention_type = null,
        public ?float $social_security_retention = null,
        public ?float $income_tax_retention = null,
        public ?float $social_contribution_retention = null
    ) {}

    /**
     * Validate federal taxation data
     * 
     * @return array Empty array if valid, array of errors if invalid
     */
    public function validate(): array
    {
        $errors = [];

        // All fields are optional
        // But if provided, validate types and ranges
        if ($this->pis_cofins_vbc !== null && $this->pis_cofins_vbc < 0) {
            $errors['pis_cofins_vbc'] = 'PIS/COFINS VBC must be non-negative';
        }

        if ($this->pis_cofins_rate !== null && ($this->pis_cofins_rate < 0 || $this->pis_cofins_rate > 100)) {
            $errors['pis_cofins_rate'] = 'PIS/COFINS rate must be between 0 and 100';
        }

        if ($this->cofins_rate !== null && ($this->cofins_rate < 0 || $this->cofins_rate > 100)) {
            $errors['cofins_rate'] = 'COFINS rate must be between 0 and 100';
        }

        if ($this->pis_cofins_value !== null && $this->pis_cofins_value < 0) {
            $errors['pis_cofins_value'] = 'PIS/COFINS value must be non-negative';
        }

        if ($this->cofins_value !== null && $this->cofins_value < 0) {
            $errors['cofins_value'] = 'COFINS value must be non-negative';
        }

        if ($this->social_security_retention !== null && $this->social_security_retention < 0) {
            $errors['social_security_retention'] = 'Social security retention must be non-negative';
        }

        if ($this->income_tax_retention !== null && $this->income_tax_retention < 0) {
            $errors['income_tax_retention'] = 'Income tax retention must be non-negative';
        }

        if ($this->social_contribution_retention !== null && $this->social_contribution_retention < 0) {
            $errors['social_contribution_retention'] = 'Social contribution retention must be non-negative';
        }

        return $errors;
    }

    /**
     * Serialize to JSON for API request
     */
    public function jsonSerialize(): array
    {
        return array_filter([
            'pis_cofins_cst' => $this->pis_cofins_cst,
            'pis_cofins_vbc' => $this->pis_cofins_vbc,
            'pis_cofins_rate' => $this->pis_cofins_rate,
            'cofins_rate' => $this->cofins_rate,
            'pis_cofins_value' => $this->pis_cofins_value,
            'cofins_value' => $this->cofins_value,
            'pis_cofins_retention_type' => $this->pis_cofins_retention_type,
            'social_security_retention' => $this->social_security_retention,
            'income_tax_retention' => $this->income_tax_retention,
            'social_contribution_retention' => $this->social_contribution_retention,
        ], fn($value) => $value !== null);
    }
}
