'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // Create Header Table
    await queryInterface.createTable('incoming_inspection_report', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'company',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
        comment: 'Company this report belongs to'
      },
      plant_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'plant',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
        comment: 'Plant where inspection is done'
      },
      fiscal_year_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'fiscal_year',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
        comment: 'Fiscal year'
      },
      iir_no: {
        type: Sequelize.STRING(50),
        allowNull: false,
        unique: true,
        comment: 'Incoming Inspection Report Number'
      },
      iir_date: {
        type: Sequelize.DATEONLY,
        allowNull: false,
        comment: 'Report date'
      },
      supplier_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'business_partner',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Supplier/Vendor'
      },
      supplier_name: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: 'Supplier name (cached for display)'
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'material',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Material/Part being inspected'
      },
      part_name: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: 'Part name'
      },
      part_no: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'Part number'
      },
      dc_no: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Delivery Challan Number'
      },
      dc_date: {
        type: Sequelize.DATEONLY,
        allowNull: true,
        comment: 'Delivery Challan Date'
      },
      po_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'purchase_order',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Reference to Purchase Order'
      },
      po_number: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'PO Number (cached)'
      },
      total_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true,
        defaultValue: 0,
        comment: 'Total quantity received'
      },
      uom: {
        type: Sequelize.STRING(20),
        allowNull: true,
        comment: 'Unit of measurement'
      },
      accepted_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true,
        defaultValue: 0,
        comment: 'Accepted quantity'
      },
      rejected_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true,
        defaultValue: 0,
        comment: 'Rejected quantity'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: 'General remarks'
      },
      inspected_by: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'Inspector name/ID'
      },
      inspected_by_user_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Inspector user reference'
      },
      approved_by: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'Approver name/ID'
      },
      approved_by_user_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Approver user reference'
      },
      inspection_status: {
        type: Sequelize.ENUM('PENDING', 'IN_PROGRESS', 'COMPLETED', 'APPROVED', 'REJECTED'),
        allowNull: false,
        defaultValue: 'PENDING',
        comment: 'Overall inspection status'
      },
      status: {
        type: Sequelize.ENUM('DRAFT', 'SUBMITTED', 'APPROVED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'DRAFT',
        comment: 'Document status'
      },
      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')
      }
    }, {
      charset: 'utf8mb4',
      collate: 'utf8mb4_unicode_ci',
      comment: 'Incoming Inspection Report Header (RI/QA/F/01)'
    });

    // Create Detail Table
    await queryInterface.createTable('incoming_inspection_report_detail', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      iir_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'incoming_inspection_report',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
        comment: 'Reference to header'
      },
      line_no: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: 'Line number/sequence'
      },
      parameter: {
        type: Sequelize.STRING(200),
        allowNull: false,
        comment: 'Inspection parameter name'
      },
      specification: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: 'Specification/requirement'
      },
      instrument: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: 'Measurement instrument used'
      },
      instrument_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'measuring_instruments',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Reference to measuring instrument'
      },
      observation_1: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Observed value 1'
      },
      observation_2: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Observed value 2'
      },
      observation_3: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Observed value 3'
      },
      observation_4: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Observed value 4'
      },
      observation_5: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Observed value 5'
      },
      observation_6: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Observed value 6'
      },
      observation_7: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Observed value 7'
      },
      observation_8: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Observed value 8'
      },
      result: {
        type: Sequelize.ENUM('OK', 'NOT_OK', 'N/A'),
        allowNull: true,
        comment: 'Test result'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: 'Line-level remarks'
      },
      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')
      }
    }, {
      charset: 'utf8mb4',
      collate: 'utf8mb4_unicode_ci',
      comment: 'Incoming Inspection Report Detail Lines'
    });

    // Create indexes for header table
    await queryInterface.addIndex('incoming_inspection_report', ['company_id'], {
      name: 'idx_iir_company_id'
    });
    await queryInterface.addIndex('incoming_inspection_report', ['plant_id'], {
      name: 'idx_iir_plant_id'
    });
    await queryInterface.addIndex('incoming_inspection_report', ['iir_no'], {
      name: 'idx_iir_no',
      unique: true
    });
    await queryInterface.addIndex('incoming_inspection_report', ['iir_date'], {
      name: 'idx_iir_date'
    });
    await queryInterface.addIndex('incoming_inspection_report', ['supplier_id'], {
      name: 'idx_iir_supplier_id'
    });
    await queryInterface.addIndex('incoming_inspection_report', ['status'], {
      name: 'idx_iir_status'
    });

    // Create indexes for detail table
    await queryInterface.addIndex('incoming_inspection_report_detail', ['iir_id'], {
      name: 'idx_iir_detail_iir_id'
    });
    await queryInterface.addIndex('incoming_inspection_report_detail', ['iir_id', 'line_no'], {
      name: 'idx_iir_detail_iir_line',
      unique: true
    });
  },

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