'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('stock_reservation', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'company',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      plant_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'plant',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      storage_location: {
        type: Sequelize.STRING(10),
        allowNull: true,
        comment: 'Storage location code - null means reservation for any location'
      },
      reserved_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0.000,
        comment: 'Quantity reserved'
      },
      issued_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0.000,
        comment: 'Quantity already issued against this reservation'
      },
      available_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0.000,
        comment: 'Available quantity = reserved_quantity - issued_quantity'
      },
      reference_type: {
        type: Sequelize.ENUM('PRODUCTION_ORDER', 'SALES_ORDER', 'MATERIAL_REQUEST', 'MANUAL'),
        allowNull: false,
        defaultValue: 'PRODUCTION_ORDER',
        comment: 'Type of document that created this reservation'
      },
      reference_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: 'ID of the document (e.g., production_order_id)'
      },
      reference_number: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Reference document number (e.g., production_order_number)'
      },
      reservation_date: {
        type: Sequelize.DATEONLY,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_DATE'),
        comment: 'Date when reservation was created'
      },
      required_date: {
        type: Sequelize.DATEONLY,
        allowNull: true,
        comment: 'Date when material is required'
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'PARTIALLY_ISSUED', 'FULLY_ISSUED', 'CANCELLED', 'EXPIRED'),
        allowNull: false,
        defaultValue: 'ACTIVE',
        comment: 'Reservation status'
      },
      cancelled_at: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: 'Date when reservation was cancelled'
      },
      cancelled_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      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: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    }, {
      engine: 'InnoDB',
      charset: 'utf8mb4',
      collate: 'utf8mb4_unicode_ci'
    });

    // Add indexes for performance
    await queryInterface.addIndex('stock_reservation', ['company_id', 'plant_id', 'material_id', 'status'], {
      name: 'idx_stock_reservation_lookup'
    });
    await queryInterface.addIndex('stock_reservation', ['reference_type', 'reference_id'], {
      name: 'idx_stock_reservation_reference'
    });
    await queryInterface.addIndex('stock_reservation', ['status', 'reservation_date'], {
      name: 'idx_stock_reservation_status_date'
    });
    await queryInterface.addIndex('stock_reservation', ['storage_location'], {
      name: 'idx_stock_reservation_location'
    });
  },

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

