'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // Create Header Table
    await queryInterface.createTable('welding_final_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'
      },
      wfir_no: {
        type: Sequelize.STRING(50),
        allowNull: false,
        unique: true,
        comment: 'Welding Final Inspection Report Number'
      },
      wfir_date: {
        type: Sequelize.DATEONLY,
        allowNull: false,
        comment: 'Report date'
      },
      doc_no: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Document Number - RI/QA/F/04'
      },
      rev_no: {
        type: Sequelize.STRING(20),
        allowNull: true,
        comment: 'Revision Number'
      },
      rev_date: {
        type: Sequelize.DATEONLY,
        allowNull: true,
        comment: 'Revision Date'
      },
      model: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'Model/Product Name'
      },
      customer_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'business_partner',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Customer'
      },
      customer_name: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: 'Customer name (cached for display)'
      },
      fabricator_name: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: 'Fabricator Name'
      },
      won: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Work Order Number'
      },
      total_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true,
        defaultValue: 0,
        comment: 'Total Quantity'
      },
      sample_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true,
        defaultValue: 0,
        comment: 'Sample Quantity'
      },
      uom: {
        type: Sequelize.STRING(20),
        allowNull: true,
        comment: 'Unit of Measurement'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: 'General remarks'
      },
      fabricator_sign: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'Fabricator Signature'
      },
      fabricator_user_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Fabricator user reference'
      },
      qc_sign: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'QC Signature'
      },
      qc_user_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'QC 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: 'Welding Final Inspection Report Header (RI/QA/F/04)'
    });

    // Create Detail Table
    await queryInterface.createTable('welding_final_inspection_report_detail', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      wfir_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'welding_final_inspection_report',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
        comment: 'Reference to header'
      },
      line_no: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: 'Line number/sequence'
      },
      description: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: 'Inspection Parameter Description'
      },
      qty: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Quantity/Specification'
      },
      frame_1: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      frame_2: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      frame_3: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      frame_4: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      frame_5: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      frame_6: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      frame_7: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      frame_8: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      frame_9: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      frame_10: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      frame_11: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      frame_12: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      frame_13: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      frame_14: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      frame_15: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      frame_16: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      frame_17: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      frame_18: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      frame_19: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      frame_20: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      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: 'Welding Final Inspection Report Detail Lines'
    });

    // Create indexes for header table
    await queryInterface.addIndex('welding_final_inspection_report', ['company_id'], {
      name: 'idx_wfir_company_id'
    });
    await queryInterface.addIndex('welding_final_inspection_report', ['plant_id'], {
      name: 'idx_wfir_plant_id'
    });
    await queryInterface.addIndex('welding_final_inspection_report', ['wfir_no'], {
      name: 'idx_wfir_no',
      unique: true
    });
    await queryInterface.addIndex('welding_final_inspection_report', ['wfir_date'], {
      name: 'idx_wfir_date'
    });
    await queryInterface.addIndex('welding_final_inspection_report', ['customer_id'], {
      name: 'idx_wfir_customer_id'
    });
    await queryInterface.addIndex('welding_final_inspection_report', ['status'], {
      name: 'idx_wfir_status'
    });
    await queryInterface.addIndex('welding_final_inspection_report', ['inspection_status'], {
      name: 'idx_wfir_inspection_status'
    });

    // Create indexes for detail table
    await queryInterface.addIndex('welding_final_inspection_report_detail', ['wfir_id'], {
      name: 'idx_wfir_detail_wfir_id'
    });
    await queryInterface.addIndex('welding_final_inspection_report_detail', ['wfir_id', 'line_no'], {
      name: 'idx_wfir_detail_wfir_line',
      unique: true
    });
  },

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