<?php

namespace App\Insurance\Entity;

use App\Doctor\Entity\Doctor;
use App\Insurance\Repository\DoctorInsuranceRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: DoctorInsuranceRepository::class)]
#[ORM\Table(name: 'doctor_insurances')]
#[ORM\UniqueConstraint(name: 'idx_doctor_insurance', columns: ['doctor_id', 'insurance_id'])]
#[ORM\Index(columns: ['insurance_id'], name: 'idx_doctor_insurance_ins')]
class DoctorInsurance
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

    #[ORM\ManyToOne(targetEntity: Doctor::class)]
    #[ORM\JoinColumn(name: 'doctor_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
    private Doctor $doctor;

    #[ORM\ManyToOne(targetEntity: Insurance::class)]
    #[ORM\JoinColumn(name: 'insurance_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
    private Insurance $insurance;

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

    public function __construct(Doctor $doctor, Insurance $insurance)
    {
        $this->doctor    = $doctor;
        $this->insurance = $insurance;
    }

    public function getId(): ?int          { return $this->id; }
    public function getDoctor(): Doctor    { return $this->doctor; }
    public function getInsurance(): Insurance { return $this->insurance; }
    public function getPrice(): ?int       { return $this->price; }

    public function setPrice(?int $v): self { $this->price = $v; return $this; }

    public function toArray(): array
    {
        return [
            'id'             => $this->id,
            'doctor_id'      => $this->doctor->getId(),
            'insurance_id'   => $this->insurance->getId(),
            'insurance_name' => $this->insurance->getName(),
            'type'           => $this->insurance->getType()->value,
            'price'          => $this->price,
        ];
    }
}
