'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('country', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      country_code: {
        type: Sequelize.STRING(3),
        allowNull: false,
        unique: true,
        comment: 'ISO 3166-1 alpha-3 country code'
      },
      country_code_iso2: {
        type: Sequelize.STRING(2),
        allowNull: true,
        unique: true,
        comment: 'ISO 3166-1 alpha-2 country code'
      },
      country_name: {
        type: Sequelize.STRING(100),
        allowNull: false
      },
      phone_code: {
        type: Sequelize.STRING(5),
        allowNull: true,
        comment: 'International dialing code'
      },
      currency_code: {
        type: Sequelize.STRING(3),
        allowNull: true,
        comment: 'Default currency code for the country'
      },
      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('country', ['country_code'], { unique: true });
    await queryInterface.addIndex('country', ['country_code_iso2'], { unique: true });
    await queryInterface.addIndex('country', ['is_active']);
  },

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

