<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
 * Adds a booking context (clinic_id) to weekly_schedules, date_overrides and holidays.
 *
 * NULL means the doctor's personal practice; a non-NULL value means the same doctor
 * inside that clinic. Existing rows keep clinic_id = NULL, i.e. every schedule and
 * override that exists today is interpreted as personal — the pre-change behaviour.
 *
 * Known limitation: a schedule that was in practice used for a clinic (its sessions
 * point at a clinic address) cannot be detected automatically. Move those with
 * `php bin/console app:schedule:assign-clinic`.
 *
 * weekly_schedules loses its UNIQUE(doctor_id) so a doctor can hold one schedule per
 * context. MySQL/MariaDB treat NULLs as distinct in a unique index, so
 * UNIQUE(doctor_id, clinic_id) does not by itself prevent two personal schedules —
 * WeeklyScheduleRepository::findByDoctorAndClinic() is the guard for that.
 */
final class Version20260718092903 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Add clinic_id booking context to weekly_schedules, date_overrides and holidays';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('ALTER TABLE weekly_schedules ADD clinic_id INT DEFAULT NULL');
        $this->addSql('ALTER TABLE weekly_schedules ADD CONSTRAINT FK_69C327F2CC22AD4 FOREIGN KEY (clinic_id) REFERENCES clinics (id) ON DELETE CASCADE');
        $this->addSql('CREATE INDEX IDX_69C327F2CC22AD4 ON weekly_schedules (clinic_id)');
        $this->addSql('ALTER TABLE weekly_schedules DROP INDEX idx_weekly_schedules_doctor, ADD INDEX IDX_69C327F287F4FB17 (doctor_id)');
        $this->addSql('ALTER TABLE weekly_schedules ADD clinic_key INT AS (IFNULL(clinic_id, 0)) STORED');
        $this->addSql('CREATE UNIQUE INDEX idx_weekly_schedules_doctor_clinic ON weekly_schedules (doctor_id, clinic_key)');

        $this->addSql('ALTER TABLE date_overrides ADD clinic_id INT DEFAULT NULL');
        $this->addSql('ALTER TABLE date_overrides ADD CONSTRAINT FK_F49AE94ECC22AD4 FOREIGN KEY (clinic_id) REFERENCES clinics (id) ON DELETE CASCADE');
        $this->addSql('CREATE INDEX IDX_F49AE94ECC22AD4 ON date_overrides (clinic_id)');
        $this->addSql('DROP INDEX uniq_date_override_doctor_date ON date_overrides');
        $this->addSql('ALTER TABLE date_overrides ADD clinic_key INT AS (IFNULL(clinic_id, 0)) STORED');
        $this->addSql('CREATE UNIQUE INDEX uniq_date_override_doctor_clinic_date ON date_overrides (doctor_id, clinic_key, date)');

        $this->addSql('ALTER TABLE holidays ADD clinic_id INT DEFAULT NULL');
        $this->addSql('ALTER TABLE holidays ADD CONSTRAINT FK_3A66A10CCC22AD4 FOREIGN KEY (clinic_id) REFERENCES clinics (id) ON DELETE CASCADE');
        $this->addSql('CREATE INDEX IDX_3A66A10CCC22AD4 ON holidays (clinic_id)');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE holidays DROP FOREIGN KEY FK_3A66A10CCC22AD4');
        $this->addSql('DROP INDEX IDX_3A66A10CCC22AD4 ON holidays');
        $this->addSql('ALTER TABLE holidays DROP clinic_id');

        $this->addSql('ALTER TABLE date_overrides DROP FOREIGN KEY FK_F49AE94ECC22AD4');
        $this->addSql('DROP INDEX IDX_F49AE94ECC22AD4 ON date_overrides');
        $this->addSql('DROP INDEX uniq_date_override_doctor_clinic_date ON date_overrides');
        $this->addSql('ALTER TABLE date_overrides DROP clinic_key');
        $this->addSql('ALTER TABLE date_overrides DROP clinic_id');
        $this->addSql('CREATE UNIQUE INDEX uniq_date_override_doctor_date ON date_overrides (doctor_id, date)');

        $this->addSql('ALTER TABLE weekly_schedules DROP FOREIGN KEY FK_69C327F2CC22AD4');
        $this->addSql('DROP INDEX IDX_69C327F2CC22AD4 ON weekly_schedules');
        $this->addSql('DROP INDEX idx_weekly_schedules_doctor_clinic ON weekly_schedules');
        $this->addSql('ALTER TABLE weekly_schedules DROP clinic_key');
        $this->addSql('ALTER TABLE weekly_schedules DROP clinic_id');
        $this->addSql('ALTER TABLE weekly_schedules DROP INDEX IDX_69C327F287F4FB17, ADD UNIQUE INDEX idx_weekly_schedules_doctor (doctor_id)');
    }
}
