'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('number_format', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'company',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      module_name: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: 'Module name: PURCHASE_ORDER, SALES_ORDER, CUSTOMER_QUOTATION, etc.'
      },
      transaction_type: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Specific transaction type within module (optional)'
      },
      format_prefix: {
        type: Sequelize.STRING(20),
        allowNull: false,
        comment: 'Prefix for the number (e.g., "RI-PUR")'
      },
      include_fiscal_year: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true,
        comment: 'Whether to include fiscal year in the number'
      },
      fiscal_year_format: {
        type: Sequelize.STRING(10),
        allowNull: true,
        comment: 'Fiscal year format: YYYY-YY (e.g., 2025-26) or YYYY (e.g., 2025)'
      },
      separator: {
        type: Sequelize.STRING(5),
        allowNull: false,
        defaultValue: '/',
        comment: 'Separator between prefix, fiscal year, and sequence (e.g., "/" or "-")'
      },
      sequence_length: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 5,
        comment: 'Length of sequence number (e.g., 5 for 00001)'
      },
      sequence_start: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 1,
        comment: 'Starting sequence number'
      },
      current_sequence: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: 'Current sequence number (auto-incremented)'
      },
      reset_sequence_annually: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true,
        comment: 'Whether to reset sequence at the start of each fiscal year'
      },
      is_active: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true
      },
      description: {
        type: Sequelize.STRING(500),
        allowNull: true
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      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')
      }
    });

    // Add unique index
    await queryInterface.addIndex('number_format', {
      fields: ['company_id', 'module_name', 'transaction_type'],
      unique: true,
      name: 'number_format_company_module_type_unique'
    });
  },

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

