'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('city', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      state_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'state',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      city_code: {
        type: Sequelize.STRING(10),
        allowNull: true
      },
      city_name: {
        type: Sequelize.STRING(100),
        allowNull: false
      },
      pincode_prefix: {
        type: Sequelize.STRING(10),
        allowNull: true,
        comment: 'Pincode prefix range (e.g., 400xxx)'
      },
      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('city', ['state_id', 'city_name'], { unique: true });
    await queryInterface.addIndex('city', ['city_code']);
    await queryInterface.addIndex('city', ['is_active']);
  },

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

