'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('purchase_return', {
      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'
      },
      return_number: { type: Sequelize.STRING(50), allowNull: false, unique: true },
      return_date: { type: Sequelize.DATEONLY, allowNull: false },
      vendor_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'business_partner', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      grn_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'goods_receipt_note', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      po_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'purchase_order', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      warehouse_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'storage_location', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      return_reason: { type: Sequelize.STRING(200), allowNull: true },
      remarks: { type: Sequelize.TEXT, allowNull: true },
      status: {
        type: Sequelize.ENUM('DRAFT', 'APPROVED'),
        allowNull: false,
        defaultValue: 'DRAFT'
      },
      approved_at: { type: Sequelize.DATE, allowNull: true },
      approved_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'user', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      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.createTable('purchase_return_item', {
      id: { allowNull: false, autoIncrement: true, primaryKey: true, type: Sequelize.INTEGER },
      purchase_return_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'purchase_return', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      line_number: { type: Sequelize.INTEGER, allowNull: false },
      grn_item_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'goods_receipt_note_item', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'material', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      uom_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'uom', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      received_quantity: { type: Sequelize.DECIMAL(18, 3), allowNull: false, defaultValue: 0 },
      already_returned_quantity: { type: Sequelize.DECIMAL(18, 3), allowNull: false, defaultValue: 0 },
      return_quantity: { type: Sequelize.DECIMAL(18, 3), allowNull: false },
      return_reason: { type: Sequelize.STRING(200), allowNull: true },
      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') }
    });

    await queryInterface.addIndex('purchase_return', ['company_id', 'plant_id', 'fiscal_year_id']);
    await queryInterface.addIndex('purchase_return', ['grn_id']);
    await queryInterface.addIndex('purchase_return', ['vendor_id']);
    await queryInterface.addIndex('purchase_return', ['status']);

    await queryInterface.addIndex('purchase_return_item', ['purchase_return_id']);
    await queryInterface.addIndex('purchase_return_item', ['grn_item_id']);
    await queryInterface.addIndex('purchase_return_item', ['material_id']);
  },

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

