<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * Appointment segments and their resource requirements — section 7 of the design
 * document. An appointment is not one continuous block: with the old model an
 * operator is locked for 60 minutes while working 30.
 */
final class Version20260730173225 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Add segment templates and segment resource requirements';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('CREATE TABLE segment_requirements (id INT AUTO_INCREMENT NOT NULL, count SMALLINT DEFAULT 1 NOT NULL, occupancy VARCHAR(12) DEFAULT \'exclusive\' NOT NULL, constraints JSON DEFAULT NULL, segment_id INT NOT NULL, resource_type_id INT NOT NULL, skill_id INT DEFAULT NULL, INDEX IDX_6701180998EC6B7B (resource_type_id), INDEX IDX_670118095585C142 (skill_id), INDEX idx_requirement_segment (segment_id), PRIMARY KEY (id)) DEFAULT CHARACTER SET utf8mb4');
        $this->addSql('CREATE TABLE segment_templates (id INT AUTO_INCREMENT NOT NULL, uuid VARCHAR(36) NOT NULL, sequence SMALLINT NOT NULL, name VARCHAR(150) NOT NULL, duration_source VARCHAR(10) DEFAULT \'fixed\' NOT NULL, duration_minutes SMALLINT DEFAULT 0 NOT NULL, patient_present TINYINT DEFAULT 1 NOT NULL, mergeable TINYINT DEFAULT 0 NOT NULL, created_at INT NOT NULL, entity_type VARCHAR(10) NOT NULL, entity_id INT NOT NULL, service_id INT NOT NULL, UNIQUE INDEX UNIQ_2BBC29FD17F50A6 (uuid), INDEX IDX_2BBC29FED5CA9E6 (service_id), INDEX idx_segment_tenant (entity_type, entity_id), INDEX idx_segment_service_seq (service_id, sequence), PRIMARY KEY (id)) DEFAULT CHARACTER SET utf8mb4');
        $this->addSql('ALTER TABLE segment_requirements ADD CONSTRAINT FK_67011809DB296AAD FOREIGN KEY (segment_id) REFERENCES segment_templates (id) ON DELETE CASCADE');
        $this->addSql('ALTER TABLE segment_requirements ADD CONSTRAINT FK_6701180998EC6B7B FOREIGN KEY (resource_type_id) REFERENCES resource_types (id) ON DELETE RESTRICT');
        $this->addSql('ALTER TABLE segment_requirements ADD CONSTRAINT FK_670118095585C142 FOREIGN KEY (skill_id) REFERENCES skills (id) ON DELETE RESTRICT');
        $this->addSql('ALTER TABLE segment_templates ADD CONSTRAINT FK_2BBC29FED5CA9E6 FOREIGN KEY (service_id) REFERENCES service_items (id) ON DELETE CASCADE');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE segment_requirements DROP FOREIGN KEY FK_67011809DB296AAD');
        $this->addSql('ALTER TABLE segment_requirements DROP FOREIGN KEY FK_6701180998EC6B7B');
        $this->addSql('ALTER TABLE segment_requirements DROP FOREIGN KEY FK_670118095585C142');
        $this->addSql('ALTER TABLE segment_templates DROP FOREIGN KEY FK_2BBC29FED5CA9E6');
        $this->addSql('DROP TABLE segment_requirements');
        $this->addSql('DROP TABLE segment_templates');
    }
}
