<?php

namespace App\Insurance\Entity;

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

#[ORM\Entity(repositoryClass: TenantServiceCoverageRepository::class)]
#[ORM\Table(name: 'tenant_service_coverage')]
#[ORM\UniqueConstraint(name: 'uniq_tenant_service_coverage', columns: ['tenant_insurance_id', 'service_item_id'])]
#[ORM\Index(columns: ['service_item_id'], name: 'idx_tsc_service')]
class TenantServiceCoverage
{
    #[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: 'tenant_insurance_id', type: 'integer')]
    private int $tenantInsuranceId;

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

    #[ORM\Column(type: 'boolean')]
    private bool $covered = true;

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

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

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

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

    public function __construct(int $tenantInsuranceId, int $serviceItemId)
    {
        $this->uuid              = Uuid::v4()->toRfc4122();
        $this->tenantInsuranceId = $tenantInsuranceId;
        $this->serviceItemId     = $serviceItemId;
        $this->updatedAt         = time();
    }

    public function getId(): ?int                { return $this->id; }
    public function getUuid(): string            { return $this->uuid; }
    public function getTenantInsuranceId(): int  { return $this->tenantInsuranceId; }
    public function getServiceItemId(): int      { return $this->serviceItemId; }
    public function isCovered(): bool            { return $this->covered; }
    public function getCoveragePercent(): ?float { return $this->coveragePercent !== null ? (float) $this->coveragePercent : null; }
    public function getFranchiseRials(): ?int    { return $this->franchiseRials; }
    public function getCeilingRials(): ?int      { return $this->ceilingRials; }

    public function setCovered(bool $v): self            { $this->covered = $v; $this->updatedAt = time(); return $this; }
    public function setCoveragePercent(?float $v): self  { $this->coveragePercent = $v !== null ? (string) $v : null; $this->updatedAt = time(); return $this; }
    public function setFranchiseRials(?int $v): self     { $this->franchiseRials = $v; $this->updatedAt = time(); return $this; }
    public function setCeilingRials(?int $v): self       { $this->ceilingRials = $v; $this->updatedAt = time(); return $this; }

    public function toArray(): array
    {
        return [
            'uuid'                => $this->uuid,
            'tenant_insurance_id' => $this->tenantInsuranceId,
            'service_item_id'     => $this->serviceItemId,
            'covered'             => $this->covered,
            'coverage_percent'    => $this->getCoveragePercent(),
            'franchise_rials'     => $this->franchiseRials,
            'ceiling_rials'       => $this->ceilingRials,
        ];
    }
}
