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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
const prisma = require('../config/database');

/**
 * Phase 1: Continuous Proximity & Detection (moved to services/synergyMonitor.js)
 */

/**
 * Phase 2: Dispatcher Orchestration - Accept Synergy
 * Dispatcher approves the opportunity and initiates handshake
 */
async function dispatcherAcceptSynergy(req, res) {
    try {
        const { opportunityId, dispatcherId } = req.body;
        const io = req.app.get('io');

        // Validate opportunity
        const opportunity = await prisma.absorptionOpportunity.findUnique({
            where: { id: opportunityId },
            include: {
                route1: {
                    include: {
                        truck: { include: { owner: true } },
                        deliveries: true
                    }
                },
                route2: {
                    include: {
                        truck: { include: { owner: true } },
                        deliveries: true
                    }
                },
                nearestHub: true
            }
        });

        if (!opportunity) {
            return res.status(404).json({
                success: false,
                message: 'Opportunity not found'
            });
        }

        if (opportunity.status !== 'PENDING') {
            return res.status(400).json({
                success: false,
                message: 'Opportunity already processed'
            });
        }

        // Update opportunity status
        await prisma.absorptionOpportunity.update({
            where: { id: opportunityId },
            data: {
                status: 'BOTH_ACCEPTED',
                acceptedByRoute1At: new Date(),
                acceptedByRoute2At: new Date()
            }
        });

        // Create AbsorptionTransfer record
        const transfer = await prisma.absorptionTransfer.create({
            data: {
                absorptionOpportunityId: opportunityId,
                exporterDriverId: opportunity.route2.truck.owner.id, // Truck B exports
                importerDriverId: opportunity.route1.truck.owner.id, // Truck A imports
                hubId: opportunity.nearestHubId,
                deliveryIdsToTransfer: opportunity.eligibleDeliveryIds,
                spaceAvailableExporter: opportunity.truck2SpaceAvailable,
                spaceAvailableImporter: opportunity.truck1SpaceAvailable,
                distanceSavedKm: opportunity.totalDistanceSaved,
                carbonSavedKg: opportunity.potentialCarbonSaved,
                status: 'PENDING'
            }
        });

        // Emit HANDSHAKE_REQUIRED to both drivers
        const handshakeData = {
            transferId: transfer.id,
            hubLat: opportunity.nearestHub.latitude,
            hubLng: opportunity.nearestHub.longitude,
            hubName: opportunity.nearestHub.name,
            hubAddress: opportunity.nearestHub.address
        };

        // Emit to Truck A driver (Importer)
        io.to(`driver_${opportunity.route1.truck.owner.id}`).emit('HANDSHAKE_REQUIRED', {
            ...handshakeData,
            role: 'IMPORTER',
            counterpartTruck: opportunity.route2.truck.licensePlate,
            counterpartDriver: opportunity.route2.truck.owner.name
        });

        // Emit to Truck B driver (Exporter)
        io.to(`driver_${opportunity.route2.truck.owner.id}`).emit('HANDSHAKE_REQUIRED', {
            ...handshakeData,
            role: 'EXPORTER',
            counterpartTruck: opportunity.route1.truck.licensePlate,
            counterpartDriver: opportunity.route1.truck.owner.name
        });

        res.status(200).json({
            success: true,
            message: 'Synergy accepted, drivers notified',
            data: {
                transferId: transfer.id,
                hubCoordinates: {
                    lat: opportunity.nearestHub.latitude,
                    lng: opportunity.nearestHub.longitude
                }
            }
        });
    } catch (error) {
        console.error('Dispatcher accept error:', error);
        res.status(500).json({
            success: false,
            message: error.message || 'Failed to accept synergy'
        });
    }
}

/**
 * Phase 3: Generate QR Code (Exporter - Truck B)
 */
async function generateQRCode(req, res) {
    try {
        const { transferId, truckId } = req.body;
        const userId = req.user.id;

        // Validate transfer
        const transfer = await prisma.absorptionTransfer.findUnique({
            where: { id: transferId },
            include: {
                exporterDriver: true,
                absorptionOpportunity: {
                    include: {
                        route2: {
                            include: {
                                deliveries: {
                                    select: {
                                        id: true,
                                        packageId: true,
                                        cargoType: true,
                                        cargoWeight: true,
                                        cargoVolumeLtrs: true,
                                        pickupLocation: true,
                                        dropLocation: true
                                    }
                                }
                            }
                        }
                    }
                }
            }
        });

        if (!transfer) {
            return res.status(404).json({
                success: false,
                message: 'Transfer not found'
            });
        }

        // Verify user is the exporter
        if (transfer.exporterDriverId !== userId) {
            return res.status(403).json({
                success: false,
                message: 'Only exporter can generate QR code'
            });
        }

        // Get deliveries to transfer
        const deliveryIds = transfer.deliveryIdsToTransfer.split(',');
        const deliveries = transfer.absorptionOpportunity.route2.deliveries.filter(
            d => deliveryIds.includes(d.id)
        );

        // Calculate totals
        const totalWeight = deliveries.reduce((sum, d) => sum + d.cargoWeight, 0);
        const totalVolume = deliveries.reduce((sum, d) => sum + d.cargoVolumeLtrs, 0);

        // Generate QR payload
        const qrPayload = {
            transferId: transfer.id,
            deliveryIds: deliveries.map(d => d.id),
            totalWeight,
            totalVolume,
            packageDetails: deliveries.map(d => ({
                packageId: d.packageId,
                cargoType: d.cargoType,
                weight: d.cargoWeight,
                volume: d.cargoVolumeLtrs,
                from: d.pickupLocation,
                to: d.dropLocation
            })),
            timestamp: new Date().toISOString()
        };

        const qrData = JSON.stringify(qrPayload);

        // Store QR data in transfer
        await prisma.absorptionTransfer.update({
            where: { id: transferId },
            data: { qrCodeData: qrData }
        });

        res.status(200).json({
            success: true,
            message: 'QR code generated',
            data: { qrData }
        });
    } catch (error) {
        console.error('QR generation error:', error);
        res.status(500).json({
            success: false,
            message: error.message || 'Failed to generate QR code'
        });
    }
}

/**
 * Phase 3: Verify QR Code (Importer - Truck A)
 */
async function verifyQR(req, res) {
    try {
        const { transferId, qrData, currentLat, currentLng } = req.body;
        const userId = req.user.id;
        const io = req.app.get('io');

        // Validate transfer
        const transfer = await prisma.absorptionTransfer.findUnique({
            where: { id: transferId },
            include: {
                importerDriver: true,
                hub: true
            }
        });

        if (!transfer) {
            return res.status(404).json({
                success: false,
                message: 'Transfer not found'
            });
        }

        // Verify user is the importer
        if (transfer.importerDriverId !== userId) {
            return res.status(403).json({
                success: false,
                message: 'Only importer can verify QR code'
            });
        }

        // Parse and validate QR data
        let scannedData;
        try {
            scannedData = JSON.parse(qrData);
        } catch (e) {
            return res.status(400).json({
                success: false,
                message: 'Invalid QR code format'
            });
        }

        // Verify transfer ID matches
        if (scannedData.transferId !== transferId) {
            return res.status(400).json({
                success: false,
                message: 'QR code does not match this transfer'
            });
        }

        // Verify delivery IDs match
        const expectedIds = transfer.deliveryIdsToTransfer.split(',').sort();
        const scannedIds = scannedData.deliveryIds.sort();

        if (JSON.stringify(expectedIds) !== JSON.stringify(scannedIds)) {
            return res.status(400).json({
                success: false,
                message: 'Delivery IDs mismatch'
            });
        }

        // Generate checklist data
        const checklistData = scannedData.packageDetails.map(pkg => ({
            packageId: pkg.packageId,
            cargoType: pkg.cargoType,
            weight: pkg.weight,
            volume: pkg.volume,
            from: pkg.from,
            to: pkg.to,
            verified: false
        }));

        // Update transfer status
        await prisma.absorptionTransfer.update({
            where: { id: transferId },
            data: {
                status: 'QR_SCANNED',
                qrCodeScanned: true,
                scannedAt: new Date(),
                checklistData: checklistData
            }
        });

        // Emit QR_SCANNED event to dispatcher
        io.emit('QR_SCANNED', {
            transferId,
            location: { lat: currentLat, lng: currentLng },
            timestamp: new Date().toISOString(),
            importerDriver: transfer.importerDriver.name
        });

        res.status(200).json({
            success: true,
            message: 'QR code verified successfully',
            data: { checklistData }
        });
    } catch (error) {
        console.error('QR verification error:', error);
        res.status(500).json({
            success: false,
            message: error.message || 'Failed to verify QR code'
        });
    }
}

/**
 * Phase 4: Complete Handover
 */
async function completeHandover(req, res) {
    try {
        const { transferId, photos, checklistData } = req.body;
        const userId = req.user.id;
        const io = req.app.get('io');

        // Validate transfer
        const transfer = await prisma.absorptionTransfer.findUnique({
            where: { id: transferId },
            include: {
                importerDriver: true,
                exporterDriver: true,
                absorptionOpportunity: {
                    include: {
                        route1: { include: { truck: true } },
                        route2: { include: { truck: true } }
                    }
                }
            }
        });

        if (!transfer) {
            return res.status(404).json({
                success: false,
                message: 'Transfer not found'
            });
        }

        // Verify user is the importer
        if (transfer.importerDriverId !== userId) {
            return res.status(403).json({
                success: false,
                message: 'Only importer can complete handover'
            });
        }

        // Verify QR was scanned
        if (transfer.status !== 'QR_SCANNED' && transfer.status !== 'CHECKLIST_VERIFIED') {
            return res.status(400).json({
                success: false,
                message: 'QR code must be scanned first'
            });
        }

        const deliveryIds = transfer.deliveryIdsToTransfer.split(',');
        const truckA = transfer.absorptionOpportunity.route1.truck;
        const truckB = transfer.absorptionOpportunity.route2.truck;

        // Execute handover in transaction
        await prisma.$transaction(async (tx) => {
            // 1. Update photos
            await tx.absorptionTransfer.update({
                where: { id: transferId },
                data: {
                    photos: photos || [],
                    checklistData: checklistData,
                    status: 'COMPLETED',
                    completedAt: new Date()
                }
            });

            // 2. Get deliveries to calculate weight/volume
            const deliveries = await tx.delivery.findMany({
                where: { id: { in: deliveryIds } }
            });

            const totalWeight = deliveries.reduce((sum, d) => sum + d.cargoWeight, 0);
            const totalVolume = deliveries.reduce((sum, d) => sum + d.cargoVolumeLtrs, 0);

            // 3. Update E-Way Bills
            await tx.eWayBill.updateMany({
                where: {
                    driverId: transfer.exporterDriverId,
                    status: 'ACTIVE'
                },
                data: {
                    vehicleNo: truckA.licensePlate,
                    driverId: transfer.importerDriverId,
                    status: 'TRANSFERRED'
                }
            });

            // 4. Reassign deliveries from Truck B to Truck A
            await tx.delivery.updateMany({
                where: { id: { in: deliveryIds } },
                data: {
                    truckId: truckA.id,
                    driverId: transfer.importerDriverId,
                    status: 'ABSORPTION_TRANSFERRED'
                }
            });

            // 5. Update Truck A (add weight/volume)
            await tx.truck.update({
                where: { id: truckA.id },
                data: {
                    currentWeight: { increment: totalWeight },
                    currentVolume: { increment: totalVolume }
                }
            });

            // 6. Update Truck B (reduce weight/volume)
            await tx.truck.update({
                where: { id: truckB.id },
                data: {
                    currentWeight: { decrement: totalWeight },
                    currentVolume: { decrement: totalVolume }
                }
            });

            // 7. Update opportunity status
            await tx.absorptionOpportunity.update({
                where: { id: transfer.absorptionOpportunityId },
                data: { status: 'COMPLETED' }
            });
        });

        // Emit TRANSFER_COMPLETED event
        io.emit('TRANSFER_COMPLETED', {
            transferId,
            deliveryIds,
            newTruckId: truckA.id,
            newTruckPlate: truckA.licensePlate,
            importerDriver: transfer.importerDriver.name,
            exporterDriver: transfer.exporterDriver.name,
            timestamp: new Date().toISOString()
        });

        // Notify both drivers
        io.to(`driver_${transfer.importerDriverId}`).emit('TRANSFER_COMPLETED', {
            message: 'Handover completed successfully',
            role: 'IMPORTER'
        });

        io.to(`driver_${transfer.exporterDriverId}`).emit('TRANSFER_COMPLETED', {
            message: 'Handover completed successfully',
            role: 'EXPORTER'
        });

        res.status(200).json({
            success: true,
            message: 'Handover completed successfully',
            data: {
                updatedDeliveries: deliveryIds.length,
                updatedEWayBills: await prisma.eWayBill.count({
                    where: {
                        driverId: transfer.importerDriverId,
                        status: 'TRANSFERRED'
                    }
                })
            }
        });
    } catch (error) {
        console.error('Handover completion error:', error);
        res.status(500).json({
            success: false,
            message: error.message || 'Failed to complete handover'
        });
    }
}

/**
 * Legacy functions (kept for backward compatibility)
 */
async function searchSynergy(req, res) {
    try {
        const { truckId } = req.body;
        const truck = await prisma.truck.findUnique({ where: { id: truckId } });

        if (!truck) {
            return res.status(404).json({ message: 'Truck not found' });
        }

        res.status(200).json({
            success: true,
            message: 'Manual search deprecated. Use continuous monitoring service.'
        });
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
}

async function acceptSynergy(req, res) {
    // DEPRECATED: Driver acceptance is no longer allowed
    // Only dispatcher can accept synergy opportunities
    try {
        return res.status(403).json({
            success: false,
            message: 'Driver acceptance not allowed. Synergy opportunities are approved by dispatcher only. Please wait for dispatcher approval.',
            deprecated: true
        });
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
}

/**
 * Phase 2: Dispatcher Orchestration - Reject Synergy
 * Dispatcher rejects the opportunity
 */
async function dispatcherRejectSynergy(req, res) {
    try {
        const { opportunityId, dispatcherId, reason } = req.body;
        const io = req.app.get('io');

        // Validate opportunity
        const opportunity = await prisma.absorptionOpportunity.findUnique({
            where: { id: opportunityId },
            include: {
                route1: {
                    include: {
                        truck: { include: { owner: true } }
                    }
                },
                route2: {
                    include: {
                        truck: { include: { owner: true } }
                    }
                }
            }
        });

        if (!opportunity) {
            return res.status(404).json({
                success: false,
                message: 'Opportunity not found'
            });
        }

        if (opportunity.status !== 'PENDING') {
            return res.status(400).json({
                success: false,
                message: 'Opportunity already processed'
            });
        }

        // Update opportunity status to REJECTED
        await prisma.absorptionOpportunity.update({
            where: { id: opportunityId },
            data: {
                status: 'REJECTED',
                rejectedAt: new Date(),
                rejectionReason: reason || 'Rejected by dispatcher'
            }
        });

        // Notify both drivers
        const rejectionData = {
            opportunityId,
            reason: reason || 'Rejected by dispatcher',
            timestamp: new Date().toISOString()
        };

        io.to(`driver_${opportunity.route1.truck.owner.id}`).emit('SYNERGY_REJECTED', rejectionData);
        io.to(`driver_${opportunity.route2.truck.owner.id}`).emit('SYNERGY_REJECTED', rejectionData);

        res.status(200).json({
            success: true,
            message: 'Synergy opportunity rejected',
            data: {
                opportunityId,
                status: 'REJECTED'
            }
        });
    } catch (error) {
        console.error('Dispatcher reject error:', error);
        res.status(500).json({
            success: false,
            message: error.message || 'Failed to reject synergy'
        });
    }
}

async function handleHandshake(req, res) {
    try {
        res.status(200).json({
            success: true,
            message: 'Use new QR-based handshake flow'
        });
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
}

module.exports = {
    // Phase 2-4 functions
    dispatcherAcceptSynergy,
    dispatcherRejectSynergy,
    generateQRCode,
    verifyQR,
    completeHandover,

    // Legacy functions (deprecated)
    searchSynergy,
    acceptSynergy,
    handleHandshake
};