const fs = require('fs');
const file = 'backend/src/db/api/job_logs.js';
let content = fs.readFileSync(file, 'utf8');

if (!content.includes('setWorkersCompClass')) {
    content = content.replace(
        /await job_logs.setVehicle(\s*data.vehicle\s*||\s*null,\s*{\s*transaction,\s*})/g,
        `await job_logs.setVehicle( data.vehicle || null, {\n            transaction,\n        });\n        \n        await job_logs.setWorkersCompClass( data.workersCompClass || null, {\n            transaction,\n        });`
    );

    content = content.replace(
        /if\s*\(data.vehicle !== undefined\)\s*{\s*await job_logs.setVehicle\(\s*data.vehicle,\s*{\s*transaction\s*}\s*\);\s*}/g,
        `if (data.vehicle !== undefined) {\n            await job_logs.setVehicle(\n              data.vehicle,\n              { transaction }\n            );\n        }\n        \n        if (data.workersCompClass !== undefined) {\n            await job_logs.setWorkersCompClass(\n              data.workersCompClass,\n              { transaction }\n            );\n        }`
    );

    // Also let's update findBy to include workersCompClass
    content = content.replace(
        /output.vehicle = await job_logs.getVehicle({\s*transaction\s*})/g,
        `output.vehicle = await job_logs.getVehicle({\n            transaction\n        });\n        \n        output.workersCompClass = await job_logs.getWorkersCompClass({\n            transaction\n        });`
    );

    fs.writeFileSync(file, content);
    console.log("Patched!");
} else {
    console.log("Already patched.");
}
