'use strict';

/**
 * Migration: Drop transitions table
 *
 * This table was designed for storing transition video metadata but was never used.
 * Transition video URLs are stored directly in tour_pages.ui_schema_json as transitionVideoUrl.
 */
module.exports = {
  async up(queryInterface, _Sequelize) {
    // Verify the table is empty before dropping
    const [results] = await queryInterface.sequelize.query(
      'SELECT COUNT(*) as count FROM transitions',
    );
    const count = parseInt(results[0].count, 10);

    if (count > 0) {
      throw new Error(
        `Cannot drop transitions table: it contains ${count} records. Please migrate or delete them first.`,
      );
    }

    await queryInterface.dropTable('transitions');
    console.log('Dropped transitions table (was empty)');
  },

  async down(queryInterface, Sequelize) {
    // Recreate the transitions table
    await queryInterface.createTable('transitions', {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
      },
      projectId: {
        type: Sequelize.UUID,
        allowNull: false,
        references: {
          model: 'projects',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      environment: {
        type: Sequelize.STRING,
        allowNull: false,
        defaultValue: 'dev',
      },
      name: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      video_url: {
        type: Sequelize.TEXT,
        allowNull: true,
      },
      duration_ms: {
        type: Sequelize.INTEGER,
        allowNull: true,
      },
      source_key: {
        type: Sequelize.UUID,
        allowNull: true,
      },
      createdAt: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      updatedAt: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      deletedAt: {
        type: Sequelize.DATE,
        allowNull: true,
      },
      createdById: {
        type: Sequelize.UUID,
        allowNull: true,
        references: {
          model: 'users',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
      },
      updatedById: {
        type: Sequelize.UUID,
        allowNull: true,
        references: {
          model: 'users',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
      },
      importHash: {
        type: Sequelize.STRING(255),
        allowNull: true,
        unique: true,
      },
    });
  },
};
