'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('state', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      country_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'country',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      state_code: {
        type: Sequelize.STRING(10),
        allowNull: false
      },
      state_name: {
        type: Sequelize.STRING(100),
        allowNull: false
      },
      gst_state_code: {
        type: Sequelize.STRING(2),
        allowNull: true,
        comment: 'GST state code (1-38 for Indian states/UTs)'
      },
      is_union_territory: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false,
        comment: 'True for Union Territories'
      },
      is_active: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    await queryInterface.addIndex('state', ['country_id', 'state_code'], { unique: true });
    await queryInterface.addIndex('state', ['gst_state_code']);
    await queryInterface.addIndex('state', ['is_active']);
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('state');
  }
};

