// migrations/XXXXXX-add-transaction-lifecycle-fields.js

'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // Get current table structure
    const transactionsTable = await queryInterface.describeTable('Transactions');
    const transactionPaymentsTable = await queryInterface.describeTable('TransactionPayments');

    // Add columns to Transactions table only if they don't exist
    if (!transactionsTable.suspendedAt) {
      await queryInterface.addColumn('Transactions', 'suspendedAt', {
        type: Sequelize.DATE,
        allowNull: true
      });
    }

    if (!transactionsTable.resumedAt) {
      await queryInterface.addColumn('Transactions', 'resumedAt', {
        type: Sequelize.DATE,
        allowNull: true
      });
    }

    if (!transactionsTable.finalizedAt) {
      await queryInterface.addColumn('Transactions', 'finalizedAt', {
        type: Sequelize.DATE,
        allowNull: true
      });
    }

    if (!transactionsTable.paidAt) {
      await queryInterface.addColumn('Transactions', 'paidAt', {
        type: Sequelize.DATE,
        allowNull: true
      });
    }

    if (!transactionsTable.completedAt) {
      await queryInterface.addColumn('Transactions', 'completedAt', {
        type: Sequelize.DATE,
        allowNull: true
      });
    }

    // Add payment-related fields to TransactionPayments table only if they don't exist
    if (!transactionPaymentsTable.cardType) {
      await queryInterface.addColumn('TransactionPayments', 'cardType', {
        type: Sequelize.STRING(50),
        allowNull: true
      });
    }

    if (!transactionPaymentsTable.last4Digits) {
      await queryInterface.addColumn('TransactionPayments', 'last4Digits', {
        type: Sequelize.STRING(4),
        allowNull: true
      });
    }

    if (!transactionPaymentsTable.authorizationCode) {
      await queryInterface.addColumn('TransactionPayments', 'authorizationCode', {
        type: Sequelize.STRING(100),
        allowNull: true
      });
    }

    // Update status enum to include all lifecycle statuses
    await queryInterface.changeColumn('Transactions', 'status', {
      type: Sequelize.ENUM(
        'open',
        'suspended',
        'pending_payment',
        'authorized',
        'paid',
        'completed',
        'voided',
        'refunded',
        'partially_refunded',
        'failed',
        'reversed'
      ),
      defaultValue: 'open',
      allowNull: false
    });
  },

  down: async (queryInterface, Sequelize) => {
    // Get current table structure
    const transactionsTable = await queryInterface.describeTable('Transactions');
    const transactionPaymentsTable = await queryInterface.describeTable('TransactionPayments');

    // Remove columns from Transactions table only if they exist
    if (transactionsTable.suspendedAt) {
      await queryInterface.removeColumn('Transactions', 'suspendedAt');
    }

    if (transactionsTable.resumedAt) {
      await queryInterface.removeColumn('Transactions', 'resumedAt');
    }

    if (transactionsTable.finalizedAt) {
      await queryInterface.removeColumn('Transactions', 'finalizedAt');
    }

    if (transactionsTable.paidAt) {
      await queryInterface.removeColumn('Transactions', 'paidAt');
    }

    if (transactionsTable.completedAt) {
      await queryInterface.removeColumn('Transactions', 'completedAt');
    }

    // Remove columns from TransactionPayments table only if they exist
    if (transactionPaymentsTable.cardType) {
      await queryInterface.removeColumn('TransactionPayments', 'cardType');
    }

    if (transactionPaymentsTable.last4Digits) {
      await queryInterface.removeColumn('TransactionPayments', 'last4Digits');
    }

    if (transactionPaymentsTable.authorizationCode) {
      await queryInterface.removeColumn('TransactionPayments', 'authorizationCode');
    }

    // Revert status enum to previous values
    await queryInterface.changeColumn('Transactions', 'status', {
      type: Sequelize.ENUM('pending', 'completed', 'voided', 'refunded'),
      defaultValue: 'pending',
      allowNull: false
    });
  }
};