'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('material_supplier', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
        comment: 'Reference to Material'
      },
      vendor_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'business_partner',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
        comment: 'Reference to BusinessPartner (Vendor)'
      },
      vendor_material_number: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Material number as per vendor catalog'
      },
      vendor_material_description: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: 'Material description as per vendor'
      },
      is_preferred: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false,
        comment: 'Is this the preferred vendor for this material'
      },
      lead_time_days: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: 'Lead time in days for procurement from this vendor'
      },
      minimum_order_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true,
        comment: 'Minimum order quantity from this vendor'
      },
      standard_price: {
        type: Sequelize.DECIMAL(18, 2),
        allowNull: true,
        comment: 'Standard price from this vendor'
      },
      currency_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'currency',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Currency for the standard price'
      },
      validity_from: {
        type: Sequelize.DATEONLY,
        allowNull: true,
        comment: 'Price validity start date'
      },
      validity_to: {
        type: Sequelize.DATEONLY,
        allowNull: true,
        comment: 'Price validity end date'
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE'),
        allowNull: false,
        defaultValue: 'ACTIVE'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      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 constraint for material_id and vendor_id combination
    await queryInterface.addIndex('material_supplier', ['material_id', 'vendor_id'], {
      unique: true,
      name: 'unique_material_vendor'
    });

    // Add other indexes
    await queryInterface.addIndex('material_supplier', ['material_id']);
    await queryInterface.addIndex('material_supplier', ['vendor_id']);
    await queryInterface.addIndex('material_supplier', ['is_preferred']);
  },

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

