<?php

namespace App\Insurance\Entity;

use App\Insurance\Repository\EntityInsurancePricingRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: EntityInsurancePricingRepository::class)]
#[ORM\Table(name: 'entity_insurance_pricing')]
#[ORM\UniqueConstraint(name: 'uniq_entity_insurance', columns: ['entity_type', 'entity_id', 'insurance_id'])]
#[ORM\Index(columns: ['entity_type', 'entity_id'], name: 'idx_entity_pricing_owner')]
class EntityInsurancePricing
{
    public const TYPE_DOCTOR = 'doctor';
    public const TYPE_CLINIC = 'clinic';

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

    #[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', nullable: true)]
    private ?int $insuranceId = null;

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

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

    public function __construct(string $entityType, int $entityId, ?int $insuranceId, int $patientShareRials = 0)
    {
        $this->entityType        = $entityType;
        $this->entityId          = $entityId;
        $this->insuranceId       = $insuranceId;
        $this->patientShareRials = $patientShareRials;
        $this->updatedAt         = time();
    }

    public function getId(): ?int                  { return $this->id; }
    public function getEntityType(): string        { return $this->entityType; }
    public function getEntityId(): int             { return $this->entityId; }
    public function getInsuranceId(): ?int         { return $this->insuranceId; }
    public function getPatientShareRials(): int    { return $this->patientShareRials; }

    public function isFreeVisit(): bool            { return $this->insuranceId === null; }

    public function setPatientShareRials(int $v): self { $this->patientShareRials = $v; $this->updatedAt = time(); return $this; }

    public function toArray(): array
    {
        return [
            'id'                  => $this->id,
            'entity_type'         => $this->entityType,
            'entity_id'           => $this->entityId,
            'insurance_id'        => $this->insuranceId,
            'patient_share_rials' => $this->patientShareRials,
        ];
    }
}
