'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('inventory_stock_transfer', {
      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'
      },
      from_plant_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'plant',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      to_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'
      },
      transfer_number: {
        type: Sequelize.STRING(50),
        allowNull: false
      },
      transfer_date: {
        type: Sequelize.DATEONLY,
        allowNull: false
      },
      movement_type: {
        type: Sequelize.STRING(10),
        allowNull: false,
        defaultValue: '301'
      },
      from_storage_location: {
        type: Sequelize.STRING(10),
        allowNull: true
      },
      to_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_stock_transfer', ['company_id', 'fiscal_year_id']);
    await queryInterface.addIndex('inventory_stock_transfer', ['transfer_number'], { unique: true });
    await queryInterface.addIndex('inventory_stock_transfer', ['transfer_date']);
    await queryInterface.addIndex('inventory_stock_transfer', ['status']);
  },

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

