<?php

namespace App\Insurance\Entity;

use App\Insurance\Enum\ServiceCategory;
use App\Insurance\Repository\TenantInsuranceCategoryCoverageRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * Per-service-category coverage percentage a tenant explicitly overrode on its own
 * contract. A missing row means "follow the central admin default", so contracts
 * are never snapshots of it.
 */
#[ORM\Entity(repositoryClass: TenantInsuranceCategoryCoverageRepository::class)]
#[ORM\Table(name: 'tenant_insurance_category_coverage')]
#[ORM\UniqueConstraint(name: 'uniq_tenant_insurance_category', columns: ['tenant_insurance_id', 'service_category'])]
class TenantInsuranceCategoryCoverage
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

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

    #[ORM\Column(name: 'service_category', type: 'string', length: 30, enumType: ServiceCategory::class)]
    private ServiceCategory $serviceCategory;

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

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

    public function __construct(int $tenantInsuranceId, ServiceCategory $serviceCategory, float $coveragePercent = 0.0)
    {
        $this->tenantInsuranceId = $tenantInsuranceId;
        $this->serviceCategory   = $serviceCategory;
        $this->coveragePercent   = (string) $coveragePercent;
        $this->updatedAt         = time();
    }

    public function getId(): ?int                          { return $this->id; }
    public function getTenantInsuranceId(): int            { return $this->tenantInsuranceId; }
    public function getServiceCategory(): ServiceCategory  { return $this->serviceCategory; }
    public function getCoveragePercent(): float            { return (float) $this->coveragePercent; }

    public function setCoveragePercent(float $v): self
    {
        $this->coveragePercent = (string) $v;
        $this->updatedAt       = time();
        return $this;
    }

    public function toArray(): array
    {
        return [
            'service_category' => $this->serviceCategory->value,
            'label'            => $this->serviceCategory->label(),
            'coverage_percent' => (float) $this->coveragePercent,
        ];
    }
}
