<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20260506000000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Add device_image_history table for rotation uniqueness tracking';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('CREATE TABLE device_image_history (
            id SERIAL NOT NULL,
            device_id INT NOT NULL,
            image_id INT NOT NULL,
            served_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
            PRIMARY KEY(id)
        )');
        $this->addSql('CREATE INDEX idx_history_device_served ON device_image_history (device_id, served_at)');
        $this->addSql('ALTER TABLE device_image_history ADD CONSTRAINT fk_history_device FOREIGN KEY (device_id) REFERENCES device (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE device_image_history ADD CONSTRAINT fk_history_image FOREIGN KEY (image_id) REFERENCES image (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('COMMENT ON COLUMN device_image_history.served_at IS \'(DC2Type:datetime_immutable)\'');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE device_image_history DROP CONSTRAINT fk_history_device');
        $this->addSql('ALTER TABLE device_image_history DROP CONSTRAINT fk_history_image');
        $this->addSql('DROP TABLE device_image_history');
    }
}
