'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('material_request', {
      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'
      },
      request_number: {
        type: Sequelize.STRING(50),
        allowNull: false,
        unique: true
      },
      request_date: {
        type: Sequelize.DATEONLY,
        allowNull: false
      },
      production_order_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'production_order',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      status: {
        type: Sequelize.ENUM('DRAFT', 'SENT_TO_STORES', 'REVIEWED', 'SENT_TO_PURCHASE', 'PARTIALLY_ISSUED', 'COMPLETED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'DRAFT'
      },
      sent_to_stores_at: {
        type: Sequelize.DATE,
        allowNull: true
      },
      sent_to_stores_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        }
      },
      reviewed_at: {
        type: Sequelize.DATE,
        allowNull: true
      },
      reviewed_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        }
      },
      sent_to_purchase_at: {
        type: Sequelize.DATE,
        allowNull: true
      },
      sent_to_purchase_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        }
      },
      purchase_requisition_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'purchase_requisition',
          key: 'id'
        }
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      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_request', ['company_id']);
    await queryInterface.addIndex('material_request', ['plant_id']);
    await queryInterface.addIndex('material_request', ['production_order_id']);
    await queryInterface.addIndex('material_request', ['status']);
    await queryInterface.addIndex('material_request', ['request_number']);

    await queryInterface.createTable('material_request_item', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      request_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material_request',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      production_order_component_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'production_order_component',
          key: 'id'
        }
      },
      line_number: {
        type: Sequelize.INTEGER,
        allowNull: false
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material',
          key: 'id'
        }
      },
      description: {
        type: Sequelize.STRING(500),
        allowNull: true
      },
      required_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false
      },
      uom_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'uom',
          key: 'id'
        }
      },
      available_stock: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      shortfall_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      issued_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      pending_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      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_request_item', ['request_id']);
    await queryInterface.addIndex('material_request_item', ['material_id']);
    await queryInterface.addIndex('material_request_item', ['production_order_component_id']);
  },

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

