<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * Date-ranged price lists and the itemised snapshot taken when an appointment is
 * booked — section 12 and the fifth closing rule: changing a price never changes an
 * already-booked appointment.
 *
 * The existing Tariff table only carries a year, so a mid-year rate change cannot be
 * expressed. It stays as a fallback layer beneath the new lists.
 */
final class Version20260731060549 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Add date-ranged price lists and appointment price snapshots';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('CREATE TABLE price_list_items (id INT AUTO_INCREMENT NOT NULL, price_rials BIGINT NOT NULL, price_list_id INT NOT NULL, service_item_id INT NOT NULL, INDEX IDX_8C05724A5688DED7 (price_list_id), INDEX IDX_8C05724ADDEB00C2 (service_item_id), UNIQUE INDEX uniq_price_list_service (price_list_id, service_item_id), PRIMARY KEY (id)) DEFAULT CHARACTER SET utf8mb4');
        $this->addSql('CREATE TABLE price_lists (id INT AUTO_INCREMENT NOT NULL, uuid VARCHAR(36) NOT NULL, name VARCHAR(150) NOT NULL, starts_at INT NOT NULL, ends_at INT NOT NULL, active TINYINT DEFAULT 0 NOT NULL, created_at INT NOT NULL, updated_at INT NOT NULL, entity_type VARCHAR(10) NOT NULL, entity_id INT NOT NULL, address_id INT DEFAULT NULL, UNIQUE INDEX UNIQ_23EF97C5D17F50A6 (uuid), INDEX IDX_23EF97C5F5B7AF75 (address_id), INDEX idx_price_list_tenant (entity_type, entity_id, active), INDEX idx_price_list_range (starts_at, ends_at), PRIMARY KEY (id)) DEFAULT CHARACTER SET utf8mb4');
        $this->addSql('CREATE TABLE price_snapshots (id INT AUTO_INCREMENT NOT NULL, uuid VARCHAR(36) NOT NULL, base_rials BIGINT NOT NULL, items_rials BIGINT NOT NULL, discount_rials BIGINT NOT NULL, insurance_base_rials BIGINT NOT NULL, insurance_supplementary_rials BIGINT NOT NULL, tax_rials BIGINT NOT NULL, final_rials BIGINT NOT NULL, deposit_rials BIGINT NOT NULL, breakdown JSON DEFAULT NULL, computed_at INT NOT NULL, entity_type VARCHAR(10) NOT NULL, entity_id INT NOT NULL, appointment_id INT NOT NULL, UNIQUE INDEX UNIQ_CE2075C1D17F50A6 (uuid), INDEX idx_snapshot_tenant (entity_type, entity_id), UNIQUE INDEX uniq_snapshot_appointment (appointment_id), PRIMARY KEY (id)) DEFAULT CHARACTER SET utf8mb4');
        $this->addSql('ALTER TABLE price_list_items ADD CONSTRAINT FK_8C05724A5688DED7 FOREIGN KEY (price_list_id) REFERENCES price_lists (id) ON DELETE CASCADE');
        $this->addSql('ALTER TABLE price_list_items ADD CONSTRAINT FK_8C05724ADDEB00C2 FOREIGN KEY (service_item_id) REFERENCES service_items (id) ON DELETE CASCADE');
        $this->addSql('ALTER TABLE price_lists ADD CONSTRAINT FK_23EF97C5F5B7AF75 FOREIGN KEY (address_id) REFERENCES doctor_addresses (id) ON DELETE CASCADE');
        $this->addSql('ALTER TABLE price_snapshots ADD CONSTRAINT FK_CE2075C1E5B533F9 FOREIGN KEY (appointment_id) REFERENCES appointments (id) ON DELETE CASCADE');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE price_list_items DROP FOREIGN KEY FK_8C05724A5688DED7');
        $this->addSql('ALTER TABLE price_list_items DROP FOREIGN KEY FK_8C05724ADDEB00C2');
        $this->addSql('ALTER TABLE price_lists DROP FOREIGN KEY FK_23EF97C5F5B7AF75');
        $this->addSql('ALTER TABLE price_snapshots DROP FOREIGN KEY FK_CE2075C1E5B533F9');
        $this->addSql('DROP TABLE price_list_items');
        $this->addSql('DROP TABLE price_lists');
        $this->addSql('DROP TABLE price_snapshots');
    }
}
