'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('material_return_note', {
      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'
      },
      mrn_number: {
        type: Sequelize.STRING(50),
        allowNull: false,
        unique: true
      },
      return_date: {
        type: Sequelize.DATEONLY,
        allowNull: false
      },
      production_order_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'production_order',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      material_issue_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'material_issue',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Link to original Material Issue Note'
      },
      returned_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        comment: 'User who returned the materials (from production)'
      },
      received_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        comment: 'User who received the materials (stores)'
      },
      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('material_return_note', ['company_id']);
    await queryInterface.addIndex('material_return_note', ['plant_id']);
    await queryInterface.addIndex('material_return_note', ['fiscal_year_id']);
    await queryInterface.addIndex('material_return_note', ['production_order_id']);
    await queryInterface.addIndex('material_return_note', ['material_issue_id']);
    await queryInterface.addIndex('material_return_note', ['status']);
    await queryInterface.addIndex('material_return_note', ['mrn_number'], { unique: true });

    await queryInterface.createTable('material_return_note_item', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      mrn_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material_return_note',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      material_issue_item_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'material_issue_item',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Link to original Material Issue Item'
      },
      production_order_component_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'production_order_component',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      line_number: {
        type: Sequelize.INTEGER,
        allowNull: false
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      description: {
        type: Sequelize.STRING(500),
        allowNull: true
      },
      batch_number: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Batch number for batch tracking'
      },
      lot_number: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Lot number for lot tracking'
      },
      returned_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false
      },
      uom_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'uom',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      return_reason: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: 'Reason for return (excess, rejected, damaged, etc.)'
      },
      storage_location: {
        type: Sequelize.STRING(10),
        allowNull: true
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });

    await queryInterface.addIndex('material_return_note_item', ['mrn_id']);
    await queryInterface.addIndex('material_return_note_item', ['material_id']);
    await queryInterface.addIndex('material_return_note_item', ['material_issue_item_id']);
    await queryInterface.addIndex('material_return_note_item', ['production_order_component_id']);
  },

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

