<?php

namespace App\Insurance\Entity;

use App\Insurance\Repository\TenantInsuranceRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity(repositoryClass: TenantInsuranceRepository::class)]
#[ORM\Table(name: 'tenant_insurances')]
#[ORM\UniqueConstraint(name: 'uniq_tenant_insurance_version', columns: ['entity_type', 'entity_id', 'insurance_id', 'version'])]
#[ORM\Index(columns: ['entity_type', 'entity_id', 'is_active'], name: 'idx_tenant_insurance_active')]
class TenantInsurance
{
    public const TYPE_DOCTOR = 'doctor';
    public const TYPE_CLINIC = 'clinic';

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

    #[ORM\Column(type: 'string', length: 36, unique: true)]
    private string $uuid;

    #[ORM\Column(name: 'entity_type', type: 'string', length: 10)]
    private string $entityType;

    #[ORM\Column(name: 'entity_id', type: 'integer')]
    private int $entityId;

    #[ORM\Column(name: 'insurance_id', type: 'integer')]
    private int $insuranceId;

    #[ORM\Column(type: 'integer')]
    private int $version = 1;

    #[ORM\Column(name: 'is_active', type: 'boolean')]
    private bool $isActive = true;

    #[ORM\Column(name: 'coverage_percent', type: 'decimal', precision: 5, scale: 2)]
    private string $coveragePercent = '0.00';

    #[ORM\Column(name: 'franchise_rials', type: 'integer')]
    private int $franchiseRials = 0;

    #[ORM\Column(name: 'annual_ceiling_rials', type: 'integer', nullable: true)]
    private ?int $annualCeilingRials = null;

    #[ORM\Column(name: 'effective_from', type: 'integer')]
    private int $effectiveFrom;

    #[ORM\Column(name: 'effective_to', type: 'integer', nullable: true)]
    private ?int $effectiveTo = null;

    #[ORM\Column(name: 'created_at', type: 'integer')]
    private int $createdAt;

    #[ORM\Column(name: 'updated_at', type: 'integer')]
    private int $updatedAt;

    public function __construct(string $entityType, int $entityId, int $insuranceId, int $version = 1)
    {
        $this->uuid          = Uuid::v4()->toRfc4122();
        $this->entityType    = $entityType;
        $this->entityId      = $entityId;
        $this->insuranceId   = $insuranceId;
        $this->version       = $version;
        $this->effectiveFrom = time();
        $this->createdAt     = time();
        $this->updatedAt     = time();
    }

    public function getId(): ?int                 { return $this->id; }
    public function getUuid(): string             { return $this->uuid; }
    public function getEntityType(): string       { return $this->entityType; }
    public function getEntityId(): int            { return $this->entityId; }
    public function getInsuranceId(): int         { return $this->insuranceId; }
    public function getVersion(): int             { return $this->version; }
    public function isActive(): bool              { return $this->isActive; }
    public function getCoveragePercent(): float   { return (float) $this->coveragePercent; }
    public function getFranchiseRials(): int      { return $this->franchiseRials; }
    public function getAnnualCeilingRials(): ?int { return $this->annualCeilingRials; }
    public function getEffectiveFrom(): int       { return $this->effectiveFrom; }
    public function getEffectiveTo(): ?int        { return $this->effectiveTo; }

    public function setActive(bool $v): self              { $this->isActive = $v; $this->updatedAt = time(); return $this; }
    public function setCoveragePercent(float $v): self    { $this->coveragePercent = (string) $v; $this->updatedAt = time(); return $this; }
    public function setFranchiseRials(int $v): self       { $this->franchiseRials = $v; $this->updatedAt = time(); return $this; }
    public function setAnnualCeilingRials(?int $v): self  { $this->annualCeilingRials = $v; $this->updatedAt = time(); return $this; }
    public function setEffectiveTo(?int $v): self         { $this->effectiveTo = $v; $this->updatedAt = time(); return $this; }

    public function toArray(): array
    {
        return [
            'uuid'                 => $this->uuid,
            'entity_type'          => $this->entityType,
            'entity_id'            => $this->entityId,
            'insurance_id'         => $this->insuranceId,
            'version'              => $this->version,
            'is_active'            => $this->isActive,
            'coverage_percent'     => (float) $this->coveragePercent,
            'franchise_rials'      => $this->franchiseRials,
            'annual_ceiling_rials' => $this->annualCeilingRials,
            'effective_from'       => $this->effectiveFrom,
            'effective_to'         => $this->effectiveTo,
        ];
    }
}
