'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('inventory_goods_receipt', {
      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'
      },
      movement_number: {
        type: Sequelize.STRING(50),
        allowNull: false
      },
      movement_date: {
        type: Sequelize.DATEONLY,
        allowNull: false
      },
      movement_type: {
        type: Sequelize.STRING(10),
        allowNull: false,
        defaultValue: '101'
      },
      reference_type: {
        type: Sequelize.ENUM('PRODUCTION_ORDER', 'RETURN', 'ADJUSTMENT', 'TRANSFER', 'OTHER'),
        allowNull: true
      },
      reference_number: {
        type: Sequelize.STRING(100),
        allowNull: true
      },
      storage_location: {
        type: Sequelize.STRING(10),
        allowNull: true
      },
      status: {
        type: Sequelize.ENUM('DRAFT', 'POSTED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'DRAFT'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      posted_at: {
        type: Sequelize.DATE,
        allowNull: true
      },
      posted_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        }
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        }
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        }
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });

    await queryInterface.addIndex('inventory_goods_receipt', ['company_id', 'plant_id', 'fiscal_year_id']);
    await queryInterface.addIndex('inventory_goods_receipt', ['movement_number'], { unique: true });
    await queryInterface.addIndex('inventory_goods_receipt', ['movement_date']);
    await queryInterface.addIndex('inventory_goods_receipt', ['status']);
  },

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

