'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('fg_stock_ledger', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      stock_ledger_no: {
        type: Sequelize.STRING(60),
        allowNull: false,
        unique: true
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'company', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      plant_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'plant', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      fiscal_year_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'fiscal_year', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      item_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'material', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      warehouse_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'storage_location', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      reference_type: {
        type: Sequelize.ENUM('FG_INWARD', 'DISPATCH', 'ADJUSTMENT'),
        allowNull: false
      },
      reference_id: {
        type: Sequelize.INTEGER,
        allowNull: true
      },
      movement_date: {
        type: Sequelize.DATE,
        allowNull: false
      },
      inward_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      outward_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      balance_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      created_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')
      }
    });

    await queryInterface.addIndex('fg_stock_ledger', ['company_id', 'plant_id', 'item_id', 'warehouse_id', 'id'], {
      name: 'idx_fg_stock_ledger_scope'
    });
    await queryInterface.addIndex('fg_stock_ledger', ['stock_ledger_no'], {
      name: 'idx_fg_stock_ledger_no',
      unique: true
    });
    await queryInterface.addIndex('fg_stock_ledger', ['movement_date'], {
      name: 'idx_fg_stock_ledger_movement_date'
    });
    await queryInterface.addIndex('fg_stock_ledger', ['reference_type', 'reference_id'], {
      name: 'idx_fg_stock_ledger_reference'
    });
  },

  async down(queryInterface) {
    await queryInterface.dropTable('fg_stock_ledger');
  }
};
