'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('vendor_quotation_request', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        allowNull: false
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'company',
          key: 'id'
        }
      },
      plant_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'plant',
          key: 'id'
        }
      },
      fiscal_year_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'fiscal_year',
          key: 'id'
        }
      },
      rfq_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'request_for_quotation',
          key: 'id'
        }
      },
      vendor_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'business_partner',
          key: 'id'
        }
      },
      vqr_number: {
        type: Sequelize.STRING(50),
        allowNull: false
      },
      vqr_date: {
        type: Sequelize.DATEONLY,
        allowNull: false
      },
      valid_until: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      currency_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'currency',
          key: 'id'
        }
      },
      payment_terms: {
        type: Sequelize.STRING(200),
        allowNull: true
      },
      delivery_terms: {
        type: Sequelize.STRING(200),
        allowNull: true
      },
      status: {
        type: Sequelize.ENUM('DRAFT', 'SENT', 'RESPONDED', 'CLOSED'),
        allowNull: false,
        defaultValue: 'DRAFT'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      total_amount: {
        type: Sequelize.DECIMAL(18, 2),
        allowNull: false,
        defaultValue: 0
      },
      sent_at: {
        type: Sequelize.DATE,
        allowNull: true
      },
      sent_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'
        }
      },
      createdAt: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updatedAt: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    await queryInterface.addIndex('vendor_quotation_request', ['company_id', 'vqr_number'], {
      unique: true,
      name: 'vendor_quotation_request_company_number_unique'
    });
    await queryInterface.addIndex('vendor_quotation_request', ['rfq_id']);
    await queryInterface.addIndex('vendor_quotation_request', ['vendor_id']);
    await queryInterface.addIndex('vendor_quotation_request', ['status']);
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('vendor_quotation_request');
  }
};

