'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('fg_dispatch', {
      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'
      },
      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'
      },
      dispatch_no: {
        type: Sequelize.STRING(60),
        allowNull: false,
        unique: true
      },
      dispatch_date: {
        type: Sequelize.DATE,
        allowNull: false
      },
      customer_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'business_partner', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      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'
      },
      available_stock: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      dispatch_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      balance_after_dispatch: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      status: {
        type: Sequelize.ENUM('PENDING', 'COMPLETED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'COMPLETED'
      },
      remarks: {
        type: Sequelize.TEXT,
        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')
      }
    });

    await queryInterface.addIndex('fg_dispatch', ['company_id', 'plant_id', 'fiscal_year_id'], {
      name: 'idx_fg_dispatch_company_plant_fy'
    });
    await queryInterface.addIndex('fg_dispatch', ['dispatch_no'], {
      name: 'idx_fg_dispatch_dispatch_no',
      unique: true
    });
    await queryInterface.addIndex('fg_dispatch', ['dispatch_date'], {
      name: 'idx_fg_dispatch_dispatch_date'
    });
    await queryInterface.addIndex('fg_dispatch', ['item_id', 'warehouse_id'], {
      name: 'idx_fg_dispatch_item_warehouse'
    });
    await queryInterface.addIndex('fg_dispatch', ['status'], {
      name: 'idx_fg_dispatch_status'
    });
  },

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