<?php

declare(strict_types=1);

class MyvetshopVeterinaire extends ObjectModel
{
    public const TABLE = 'myvetshop_veterinaire';

    /**
     * @var int
     */
    public $id_myvetshop_clinique;

    /**
     * @var string
     */
    public $lastname;

    /**
     * @var string
     */
    public $firstname;

    /**
     * @var string
     */
    public $url_teleconsultation;

    /**
     * @var array{primary: string, table: string}
     */
    public static $definition = [
        'table' => self::TABLE,
        'primary' => 'id_myvetshop_veterinaire',
        'fields' => [
            'id_myvetshop_clinique' => ['type' => self::TYPE_INT, 'required' => true, 'validate' => 'isInt'],
            'lastname' => ['type' => self::TYPE_STRING, 'required' => false, 'size' => 55],
            'firstname' => ['type' => self::TYPE_STRING, 'required' => false, 'size' => 55],
            'url_teleconsultation' => ['type' => self::TYPE_STRING, 'required' => false, 'size' => 100],
        ],
    ];

    public function getClinique(): Clinique
    {
        return new Clinique($this->id_myvetshop_clinique);
    }

    /**
     * @return array{nom: string, prenom: string, url_rdv: string}
     */
    public function toArray(): array
    {
        return [
            'nom' => $this->lastname,
            'prenom' => $this->firstname,
            'url_rdv' => $this->url_teleconsultation,
        ];
    }

    /**
     * @return MyvetshopVeterinaire[]
     */
    public static function getByCliniqueId(int $cliniqueId): array
    {
        $query = 'SELECT id_myvetshop_veterinaire'
            . ' FROM ' . _DB_PREFIX_ . self::$definition['table']
            . ' WHERE id_myvetshop_clinique=' . $cliniqueId;

        $rows = Db::getInstance()->executeS($query);
        if (!is_array($rows)) {
            $rows = [];
        }

        $list = \array_column($rows, 'id_myvetshop_veterinaire');

        return \array_map(fn ($veterinaireId) => new self($veterinaireId), $list);
    }

    public static function install(): bool
    {
        $query = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . static::TABLE . '` (
              `id_myvetshop_veterinaire` INT(11) NOT NULL AUTO_INCREMENT,
              `id_myvetshop_clinique` INT(11) NOT NULL,
              `lastname` varchar(55) DEFAULT NULL,
              `firstname` varchar(55) DEFAULT NULL,
              `url_teleconsultation` varchar(100) DEFAULT NULL,
               PRIMARY KEY (`id_myvetshop_veterinaire`),
               INDEX (`id_myvetshop_clinique`)
            );';

        return Db::getInstance()->execute($query);
    }
}
