<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * Resource occupancy: one row per (appointment segment × resource).
 *
 * The granularity is the point — an operator with no requirement during "waiting for
 * the cream" has no row for those minutes and stays bookable for someone else. That
 * is what returns the capacity the single-interval model wasted.
 */
final class Version20260730174147 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Add resource occupancy for multi-resource availability';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('CREATE TABLE resource_occupancy (id INT AUTO_INCREMENT NOT NULL, uuid VARCHAR(36) NOT NULL, appointment_id INT DEFAULT NULL, starts_at INT NOT NULL, ends_at INT NOT NULL, status VARCHAR(10) DEFAULT \'booked\' NOT NULL, segment_name VARCHAR(150) DEFAULT NULL, created_at INT NOT NULL, entity_type VARCHAR(10) NOT NULL, entity_id INT NOT NULL, resource_id INT NOT NULL, UNIQUE INDEX UNIQ_2DC70369D17F50A6 (uuid), INDEX IDX_2DC7036989329D25 (resource_id), INDEX idx_occupancy_resource_range (resource_id, starts_at, ends_at), INDEX idx_occupancy_tenant (entity_type, entity_id), INDEX idx_occupancy_appointment (appointment_id), PRIMARY KEY (id)) DEFAULT CHARACTER SET utf8mb4');
        $this->addSql('ALTER TABLE resource_occupancy ADD CONSTRAINT FK_2DC7036989329D25 FOREIGN KEY (resource_id) REFERENCES clinic_resources (id) ON DELETE CASCADE');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE resource_occupancy DROP FOREIGN KEY FK_2DC7036989329D25');
        $this->addSql('DROP TABLE resource_occupancy');
    }
}
