'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('nesting_stores_request', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false
      },
      plant_id: {
        type: Sequelize.INTEGER,
        allowNull: false
      },
      fiscal_year_id: {
        type: Sequelize.INTEGER,
        allowNull: true
      },
      production_order_id: {
        type: Sequelize.INTEGER,
        allowNull: false
      },
      material_request_id: {
        type: Sequelize.INTEGER,
        allowNull: true
      },
      customer_po_id: {
        type: Sequelize.INTEGER,
        allowNull: true
      },
      customer_po_number: {
        type: Sequelize.STRING(100),
        allowNull: true
      },
      customer_name: {
        type: Sequelize.STRING(255),
        allowNull: true
      },
      production_order_number: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      order_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true
      },
      released_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true
      },
      material_request_number: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      required_date: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true
      },
      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')
      }
    });

    await queryInterface.addIndex('nesting_stores_request', ['company_id', 'production_order_id'], {
      name: 'idx_nsr_company_po'
    });
    await queryInterface.addIndex('nesting_stores_request', ['material_request_id'], {
      name: 'idx_nsr_material_request'
    });

    await queryInterface.createTable('nesting_stores_request_line', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      nesting_stores_request_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'nesting_stores_request', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      sheet_group_key: {
        type: Sequelize.STRING(200),
        allowNull: true
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: false
      },
      thickness: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true
      },
      grade_short: {
        type: Sequelize.STRING(120),
        allowNull: true
      },
      max_length: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true
      },
      max_width: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true
      },
      height: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true
      },
      dimension_unit: {
        type: Sequelize.STRING(20),
        allowNull: true,
        defaultValue: 'mm'
      },
      nesting_confirmed_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      purpose_text: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      warehouse_code: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      available_at_store_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true
      },
      shortage_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true
      },
      nesting_raw_material_text: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      line_status: {
        type: Sequelize.ENUM('PENDING', 'COMPLETED'),
        allowNull: false,
        defaultValue: 'PENDING'
      },
      completed_at: {
        type: Sequelize.DATE,
        allowNull: true
      },
      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')
      }
    });

    await queryInterface.addIndex('nesting_stores_request_line', ['nesting_stores_request_id'], {
      name: 'idx_nsrl_request'
    });
    await queryInterface.addIndex('nesting_stores_request_line', ['material_id'], {
      name: 'idx_nsrl_material'
    });
  },

  down: async (queryInterface) => {
    await queryInterface.dropTable('nesting_stores_request_line');
    await queryInterface.dropTable('nesting_stores_request');
  }
};
