File size: 59,015 Bytes
486340a
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486340a
 
366364c
 
 
 
 
 
 
 
 
 
 
 
 
486340a
 
366364c
 
 
 
 
 
 
3790c80
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486340a
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3790c80
 
366364c
 
 
 
 
 
 
 
 
 
486340a
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486340a
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3790c80
366364c
 
 
 
 
3790c80
366364c
 
 
486340a
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486340a
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486340a
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486340a
366364c
486340a
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486340a
366364c
 
486340a
366364c
 
 
486340a
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486340a
366364c
486340a
366364c
 
 
 
486340a
 
366364c
 
3790c80
366364c
 
 
486340a
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3790c80
366364c
486340a
366364c
 
 
 
 
 
486340a
 
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486340a
366364c
 
 
 
 
486340a
366364c
 
 
 
 
486340a
 
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486340a
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486340a
366364c
 
 
 
 
 
 
 
 
 
486340a
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486340a
366364c
 
486340a
366364c
 
 
 
 
 
486340a
366364c
 
 
486340a
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486340a
366364c
486340a
366364c
 
 
 
 
 
 
 
 
 
486340a
366364c
 
 
 
 
 
095d271
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
095d271
366364c
 
 
 
 
 
 
 
 
 
3790c80
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
095d271
366364c
 
095d271
 
366364c
 
 
 
 
 
 
 
 
 
 
 
 
3790c80
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3790c80
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
095d271
 
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
095d271
366364c
 
 
 
 
 
 
 
 
095d271
366364c
 
095d271
366364c
 
 
 
 
 
 
 
 
095d271
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d221b7
3790c80
366364c
 
 
1d221b7
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d221b7
366364c
 
 
 
 
 
 
 
 
 
 
 
3790c80
1d221b7
366364c
 
 
 
 
 
 
e63b011
366364c
 
 
3790c80
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e63b011
3790c80
366364c
 
 
 
 
 
 
 
 
 
 
3790c80
e63b011
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3790c80
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3790c80
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3790c80
366364c
3790c80
366364c
 
 
 
3790c80
366364c
3790c80
366364c
3790c80
366364c
 
 
 
 
3790c80
 
366364c
 
 
 
3790c80
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3790c80
366364c
 
 
 
 
 
 
 
 
3790c80
366364c
 
 
 
3790c80
366364c
 
 
 
 
 
 
 
 
3790c80
366364c
 
 
 
 
 
 
 
 
3790c80
366364c
3790c80
366364c
3790c80
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3790c80
366364c
 
3790c80
366364c
 
 
 
 
 
 
3790c80
366364c
 
 
 
 
 
 
3790c80
366364c
 
3790c80
 
366364c
 
 
 
3790c80
366364c
 
3790c80
366364c
 
 
 
 
 
 
 
3790c80
 
366364c
 
 
 
 
 
 
 
 
 
3790c80
 
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3790c80
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
095d271
366364c
1d221b7
366364c
486340a
366364c
 
 
 
 
 
 
 
 
 
 
 
 
 
047ead5
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
const { db } = require('../config/firebase');
const { getBalance } = require('../utils/walletHelper');
const { pushToUser } = require('../utils/notificationHelper');
const { normalizeRouteLabels } = require('../utils/aiLocationHelper');
const { labelFromLocation } = require('../utils/locationLabelHelper');
const { maybeCleanupExpiredRides } = require('../utils/throttledCleanup');
const { DEAL_STATUS } = require('../constants/statuses');
const { fetchRoutePolyline, projectPointOnPolyline } = require('../utils/routeHelper');
const {
  sanitizeString,
  exceedsMaxLength,
  MAX_LOCATION,
} = require('../utils/inputSanitizer');

// ─── HELPERS ──────────────────────────────────────────────
function parseNumber(value) {
  const n = Number(value);
  return Number.isFinite(n) ? n : null;
}

function distanceKm(aLat, aLng, bLat, bLng) {
  if ([aLat, aLng, bLat, bLng].some((v) => v == null)) return null;
  const toRad = (value) => (value * Math.PI) / 180;
  const earthKm = 6371;
  const dLat = toRad(bLat - aLat);
  const dLng = toRad(bLng - aLng);
  const lat1 = toRad(aLat);
  const lat2 = toRad(bLat);
  const h =
    Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(lat1) * Math.cos(lat2) *
      Math.sin(dLng / 2) * Math.sin(dLng / 2);
  return earthKm * 2 * Math.atan2(Math.sqrt(h), Math.sqrt(1 - h));
}

function calculateRouteProximityMeters(userLat, userLng, startLat, startLng, endLat, endLng) {
  const pickupLat = parseNumber(userLat);
  const pickupLng = parseNumber(userLng);
  const routeStartLat = parseNumber(startLat);
  const routeStartLng = parseNumber(startLng);
  const routeEndLat = parseNumber(endLat);
  const routeEndLng = parseNumber(endLng);

  if ([pickupLat, pickupLng, routeStartLat, routeStartLng, routeEndLat, routeEndLng].some((v) => v == null)) {
    return null;
  }

  const toRouteDistanceKm = (pointLat, pointLng, lineStartLat, lineStartLng, lineEndLat, lineEndLng) => {
    const dx = lineEndLng - lineStartLng;
    const dy = lineEndLat - lineStartLat;
    const lengthSq = dx * dx + dy * dy;
    if (lengthSq === 0) {
      return distanceKm(pointLat, pointLng, lineStartLat, lineStartLng);
    }

    const t = ((pointLng - lineStartLng) * dx + (pointLat - lineStartLat) * dy) / lengthSq;
    const clampedT = Math.max(0, Math.min(1, t));
    const nearestLat = lineStartLat + clampedT * dy;
    const nearestLng = lineStartLng + clampedT * dx;
    return distanceKm(pointLat, pointLng, nearestLat, nearestLng);
  };

  const distanceKmToSegment = toRouteDistanceKm(
    pickupLat,
    pickupLng,
    routeStartLat,
    routeStartLng,
    routeEndLat,
    routeEndLng,
  );

  return distanceKmToSegment == null ? null : distanceKmToSegment * 1000;
}

function pointProjectionOnRoute(pointLat, pointLng, startLat, startLng, endLat, endLng) {
  const lat = parseNumber(pointLat);
  const lng = parseNumber(pointLng);
  const sLat = parseNumber(startLat);
  const sLng = parseNumber(startLng);
  const eLat = parseNumber(endLat);
  const eLng = parseNumber(endLng);

  if ([lat, lng, sLat, sLng, eLat, eLng].some((v) => v == null)) return null;

  const dx = eLng - sLng;
  const dy = eLat - sLat;
  const lengthSq = dx * dx + dy * dy;
  if (lengthSq === 0) {
    const distance = distanceKm(lat, lng, sLat, sLng);
    return { t: 0, distanceKm: distance == null ? null : distance };
  }

  const rawT = ((lng - sLng) * dx + (lat - sLat) * dy) / lengthSq;
  const t = Math.max(0, Math.min(1, rawT));
  const nearestLat = sLat + t * dy;
  const nearestLng = sLng + t * dx;
  return {
    t,
    rawT,
    distanceKm: distanceKm(lat, lng, nearestLat, nearestLng),
  };
}

function routeMatchScore(ride, search) {
  const pickupRadiusKm = search.pickupRadiusKm ?? 3;
  const destinationRadiusKm = search.destinationRadiusKm ?? 3;
  const routeRadiusKm = search.routeRadiusKm ?? 2.5;
  const pickupLat = search.pickupLat;
  const pickupLng = search.pickupLng;
  const destLat = search.destLat;
  const destLng = search.destLng;

  function projectOnRoute(lat, lng) {
    if (lat == null || lng == null) return null;
    if (Array.isArray(ride.routePolyline) && ride.routePolyline.length >= 2) {
      const hit = projectPointOnPolyline(lat, lng, ride.routePolyline);
      if (!hit) return null;
      return { t: hit.progress, distanceKm: hit.distanceMeters / 1000 };
    }
    return pointProjectionOnRoute(lat, lng, ride.startLat, ride.startLng, ride.endLat, ride.endLng);
  }

  const pickupProjection = projectOnRoute(pickupLat, pickupLng);
  const destinationProjection = projectOnRoute(destLat, destLng);
  const pickupStartKm = distanceKm(pickupLat, pickupLng, parseNumber(ride.startLat), parseNumber(ride.startLng));
  const destinationEndKm = distanceKm(destLat, destLng, parseNumber(ride.endLat), parseNumber(ride.endLng));
  const captainCurrentKm = distanceKm(pickupLat, pickupLng, parseNumber(ride.captainLat), parseNumber(ride.captainLng));

  const pickupOnRoute =
    pickupProjection?.distanceKm != null && pickupProjection.distanceKm <= routeRadiusKm;
  const destinationOnRoute =
    destinationProjection?.distanceKm != null && destinationProjection.distanceKm <= routeRadiusKm;
  const pickupNearStart = pickupStartKm != null && pickupStartKm <= pickupRadiusKm;
  const destinationNearEnd = destinationEndKm != null && destinationEndKm <= destinationRadiusKm;
  const captainNearPickup = captainCurrentKm != null && captainCurrentKm <= pickupRadiusKm;
  const directionOk =
    pickupProjection?.t == null ||
    destinationProjection?.t == null ||
    pickupProjection.t <= destinationProjection.t + 0.03;

  const routeIncludesJourney =
    directionOk &&
    (pickupOnRoute || pickupNearStart || captainNearPickup) &&
    (destinationOnRoute || destinationNearEnd);

  const pickupLabelScore = Math.max(
    levenshteinSimilarity(search.startLocation, ride.startLocation),
    levenshteinSimilarity(search.startLocation, ride.exactLocation),
  );
  const destinationLabelScore = Math.max(
    levenshteinSimilarity(search.endLocation, ride.endLocation),
    levenshteinSimilarity(search.endLocation, ride.exactDropLocation),
  );
  const textOverlap = search.startLocation && search.endLocation
    ? locationsOverlap(search.startLocation, search.endLocation, ride.startLocation, ride.endLocation)
    : false;

  let score = 0;
  if (routeIncludesJourney) score += 70;
  if (pickupNearStart) score += 18;
  if (captainNearPickup) score += 12;
  if (pickupOnRoute) score += 16;
  if (destinationNearEnd) score += 16;
  if (destinationOnRoute) score += 16;
  if (directionOk) score += 10;
  if (textOverlap) score += 18;
  score += Math.floor(Math.max(pickupLabelScore, 0) * 0.12);
  score += Math.floor(Math.max(destinationLabelScore, 0) * 0.12);

  const pickupRouteKm = pickupProjection?.distanceKm;
  const destinationRouteKm = destinationProjection?.distanceKm;
  const suitable =
    routeIncludesJourney ||
    (textOverlap && directionOk) ||
    (pickupLabelScore >= 80 && (destinationLabelScore >= 70 || destinationOnRoute || destinationNearEnd));

  return {
    suitable,
    score,
    directionOk,
    routePassesThroughPickup: pickupOnRoute,
    routePassesThroughDestination: destinationOnRoute,
    pickupRouteKm,
    destinationRouteKm,
    distanceFromPickup: pickupStartKm,
    distanceFromDestination: destinationEndKm,
    captainDistanceFromPickup: captainCurrentKm,
    pickupProgress: pickupProjection?.t ?? null,
    destinationProgress: destinationProjection?.t ?? null,
    routeIncludesJourney,
    isDirectMatch: pickupNearStart && destinationNearEnd,
    isProximityMatch: routeIncludesJourney || pickupOnRoute || destinationOnRoute,
  };
}

function buildRideRouteSummary(startLat, startLng, endLat, endLng) {
  const routeStartLat = parseNumber(startLat);
  const routeStartLng = parseNumber(startLng);
  const routeEndLat = parseNumber(endLat);
  const routeEndLng = parseNumber(endLng);

  if ([routeStartLat, routeStartLng, routeEndLat, routeEndLng].some((v) => v == null)) {
    return null;
  }

  const routeDistanceKm = distanceKm(routeStartLat, routeStartLng, routeEndLat, routeEndLng);

  return {
    routeBoundingBox: {
      north: Math.max(routeStartLat, routeEndLat),
      south: Math.min(routeStartLat, routeEndLat),
      east: Math.max(routeStartLng, routeEndLng),
      west: Math.min(routeStartLng, routeEndLng),
    },
    routeDistanceKm: routeDistanceKm == null ? null : Number(routeDistanceKm.toFixed(2)),
    routeDistanceMeters: routeDistanceKm == null ? null : Number((routeDistanceKm * 1000).toFixed(0)),
  };
}

function isValidLat(value) {
  return value != null && value >= -90 && value <= 90;
}

function isValidLng(value) {
  return value != null && value >= -180 && value <= 180;
}

function isZeroCoordinate(lat, lng) {
  return Number(lat) === 0 && Number(lng) === 0;
}

function isRideUpcoming(ride, now = new Date()) {
  const status = (ride.status || '').toString().toLowerCase();
  if (!['active', 'in_progress'].includes(status)) return false;
  const departure = new Date(ride.departureTime || '');
  return !Number.isNaN(departure.getTime()) && departure > now;
}

function formatRideDateTime(value) {
  const date = new Date(value || '');
  if (Number.isNaN(date.getTime())) return null;
  try {
    const formatter = new Intl.DateTimeFormat('en-GB', {
      timeZone: 'Asia/Karachi',
      day: 'numeric',
      month: 'long',
      year: 'numeric',
      hour: 'numeric',
      minute: '2-digit',
      hour12: true,
    });
    const parts = formatter.formatToParts(date);
    const day = parts.find(p => p.type === 'day').value;
    const month = parts.find(p => p.type === 'month').value;
    const year = parts.find(p => p.type === 'year').value;
    const hour = parts.find(p => p.type === 'hour').value;
    const minute = parts.find(p => p.type === 'minute').value;
    const dayPeriod = parts.find(p => p.type === 'dayPeriod').value.toLowerCase();

    const minutesStr = minute === '00' ? '' : `:${minute}`;
    const timeStr = `${hour}${minutesStr} ${dayPeriod}`;

    return `${day} ${month} ${year} time ${timeStr}`;
  } catch (e) {
    const day = date.getDate();
    const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    const month = months[date.getMonth()];
    const year = date.getFullYear();
    let hours = date.getHours();
    const minutes = date.getMinutes();
    const ampm = hours >= 12 ? 'pm' : 'am';
    hours = hours % 12;
    hours = hours ? hours : 12;
    const minutesStr = minutes === 0 ? '' : `:${minutes.toString().padStart(2, '0')}`;
    return `${day} ${month} ${year} time ${hours}${minutesStr} ${ampm}`;
  }
}

function locationsOverlap(queryStart, queryEnd, rideStart, rideEnd) {
  const qs = (queryStart || '').toLowerCase();
  const qe = (queryEnd || '').toLowerCase();
  const rs = (rideStart || '').toLowerCase();
  const re = (rideEnd || '').toLowerCase();

  const sequence = [
    'peshawar',
    'nowshera',
    'attock',
    'wah cantt',
    'wah',
    'taxila',
    'rawalpindi',
    'pindi',
    'islamabad',
    'isb'
  ];

  const findIndex = (loc) => {
    return sequence.findIndex(item => loc.includes(item));
  };

  const qStartIdx = findIndex(qs);
  const qEndIdx = findIndex(qe);
  const rStartIdx = findIndex(rs);
  const rEndIdx = findIndex(re);

  if (qStartIdx !== -1 && qEndIdx !== -1 && rStartIdx !== -1 && rEndIdx !== -1) {
    const queryDir = qEndIdx - qStartIdx;
    const rideDir = rEndIdx - rStartIdx;

    if ((queryDir > 0 && rideDir > 0) || (queryDir < 0 && rideDir < 0)) {
      const qMin = Math.min(qStartIdx, qEndIdx);
      const qMax = Math.max(qStartIdx, qEndIdx);
      const rMin = Math.min(rStartIdx, rEndIdx);
      const rMax = Math.max(rStartIdx, rEndIdx);

      return Math.max(qMin, rMin) <= Math.min(qMax, rMax);
    }
  }

  return rs.includes(qs) && re.includes(qe);
}

function levenshteinSimilarity(str1, str2) {
  const s1 = (str1 || '').toLowerCase().trim();
  const s2 = (str2 || '').toLowerCase().trim();

  if (s1 === s2) return 100;
  if (!s1 || !s2) return 0;

  const longer = s1.length > s2.length ? s1 : s2;
  const shorter = s1.length > s2.length ? s2 : s1;

  if (longer.includes(shorter)) return 95;

  const costs = [];
  for (let i = 0; i <= s1.length; i++) {
    let lastValue = i;
    for (let j = 0; j <= s2.length; j++) {
      if (i === 0) {
        costs[j] = j;
      } else if (j > 0) {
        let newValue = costs[j - 1];
        if (s1.charAt(i - 1) !== s2.charAt(j - 1)) {
          newValue = Math.min(Math.min(newValue, lastValue), costs[j]) + 1;
        }
        costs[j - 1] = lastValue;
        lastValue = newValue;
      }
    }
    if (i > 0) costs[s2.length] = lastValue;
  }

  const distance = costs[s2.length];
  const maxLen = Math.max(s1.length, s2.length);
  return Math.round(((maxLen - distance) / maxLen) * 100);
}

function calculateDestinationMatch(userDest, rideStart, rideEnd) {
  if (!userDest || !rideEnd) return { score: 0, passesThrough: false };

  const userDest_lower = (userDest || '').toLowerCase().trim();
  const rideEnd_lower = (rideEnd || '').toLowerCase().trim();
  const rideStart_lower = (rideStart || '').toLowerCase().trim();

  const directMatch = levenshteinSimilarity(userDest_lower, rideEnd_lower);

  const startMatch = levenshteinSimilarity(userDest_lower, rideStart_lower);
  const passesThrough = startMatch >= 80;

  const score = Math.max(directMatch, startMatch);

  return {
    score: Math.round(score),
    passesThrough: passesThrough || directMatch >= 80
  };
}

function serializeRide(id, data) {
  const ride = { id, ...data };
  const displayDateTime = formatRideDateTime(ride.departureTime);
  const routeSummary = buildRideRouteSummary(ride.startLat, ride.startLng, ride.endLat, ride.endLng);
  return {
    ...ride,
    routeLabel: `${ride.startLocation || ''} -> ${ride.endLocation || ''}`.trim(),
    departureDisplay: displayDateTime,
    displayDateTime,
    ...(routeSummary || {}),
  };
}

// ─── NOTIFICATION HELPERS ──────────────────────────────────

// βœ… FIX: Removed city filter – now route‑match only decides
async function notifyPassengersAboutRide(ride, rideId, captainCity) {
  try {
    const passengersSnap = await db
      .collection('customerRideRequests')
      .where('status', 'in', ['open', 'countered'])
      .get();

    const targets = [];
    passengersSnap.docs.forEach((doc) => {
      const reqData = doc.data();
      if (!reqData) return;

      // ❌ City filter REMOVED – no longer blocking based on text

      // Map request coordinates to search parameter format
      const search = {
        pickupLat: reqData.startLat,
        pickupLng: reqData.startLng,
        destLat: reqData.endLat,
        destLng: reqData.endLng,
        pickupRadiusKm: 3.0,
        destinationRadiusKm: 3.0,
        routeRadiusKm: 2.5,
        startLocation: reqData.startLocation || '',
        endLocation: reqData.endLocation || '',
      };

      const score = routeMatchScore(ride, search);
      if (score && score.routeIncludesJourney) {
        targets.push(reqData.customerId);
      }
    });

    const uniqueCustomerIds = [...new Set(targets)];
    await Promise.all(uniqueCustomerIds.map(customerId => pushToUser(customerId, {
      title: 'πŸš— New Ride on Your Route!',
      body: `${ride.captainName} posted a ${ride.vehicleType} ride matching your route from ${ride.startLocation} to ${ride.endLocation}`,
      type: 'new_ride',
      data: {
        rideId: rideId,
        screen: 'find-ride',
        vehicleType: ride.vehicleType,
        startLocation: ride.startLocation,
        endLocation: ride.endLocation,
        suggestedFare: String(ride.suggestedFare)
      },
    })));

    console.log(`βœ… Smart-notified ${uniqueCustomerIds.length} passengers about matching ride`);
  } catch (err) {
    console.error('Passenger notification error:', err.message);
  }
}

// βœ… FIX: Broadcast to ALL captains (no route‑filter)
async function notifyCaptainsAboutRequest(request, requestId, customerCity, vehicleType) {
  try {
    // Get ALL active captains
    let captainsQuery = db.collection('users').where('role', '==', 'captain');
    const captainsSnap = await captainsQuery.get();

    const targets = captainsSnap.docs
      .map(doc => doc.id)
      .filter(id => id); // valid ids

    const uniqueCaptainIds = [...new Set(targets)];
    await Promise.all(uniqueCaptainIds.map(captainId => pushToUser(captainId, {
      title: 'πŸ§‘ New Ride Request!',
      body: `${request.customerName} needs a ride from ${request.startLocation} to ${request.endLocation}`,
      type: 'customer_ride_request',
      data: {
        requestId: requestId,
        screen: 'captain-requests',
        customerId: request.customerId,
        startLocation: request.startLocation,
        endLocation: request.endLocation,
        preferredFare: String(request.preferredFare || 0),
      },
    })));

    console.log(`βœ… Notified ALL ${uniqueCaptainIds.length} captains about new request`);
  } catch (err) {
    console.error('Captain notification error:', err.message);
  }
}

// ─── CAPTAIN POST RIDE ────────────────────────────────────
const postRide = async (req, res) => {
  const uid = req.user ? req.user.uid : req.body.captainId;
  if (!uid) return res.status(400).json({ success: false, error: 'Captain ID is required', code: 'MISSING_CAPTAIN_ID' });

  try {
    const userDoc = await db.collection('users').doc(uid).get();
    if (!userDoc.exists) return res.status(404).json({ success: false, error: 'User profile not found.', code: 'USER_NOT_FOUND' });

    const userData = userDoc.data();
    const verificationStatus = userData.captainVerificationStatus;
    const isCaptainVerified = verificationStatus === 'verified' || verificationStatus === 'approved' || userData.isVerified === true;
    if (!isCaptainVerified) return res.status(403).json({ success: false, error: 'Your documents are under review.', code: 'NOT_VERIFIED' });

    const requiredVehicleFields = ['captainVehicleType', 'vehicleModel', 'vehicleRegistration', 'vehiclePhotoUrl'];
    const missingVehicle = requiredVehicleFields.filter(f => !userData[f] || String(userData[f]).trim() === '');
    if (missingVehicle.length > 0) return res.status(403).json({ success: false, error: 'Complete your vehicle details first.', code: 'INCOMPLETE_VEHICLE_DETAILS' });

    await getBalance(uid);

    const {
      startLocation, endLocation,
      startLat, startLng, endLat, endLng,
      departureTime, totalSeats, suggestedFare,
      rideType, vehicleType, acceptsDelivery, vehicleInfo,
      tourType, maxPassengers,
      cargoType, weightCapacity, truckSize, exactLocation, exactDropLocation,
      routeDescription,
    } = req.body;

    const rideMode = req.body.rideMode || 'share';
    const normalizedRideMode = String(rideMode).toLowerCase();
    if (!['share', 'solo'].includes(normalizedRideMode)) {
      return res.status(400).json({ success: false, error: 'rideMode must be share or solo', code: 'INVALID_RIDE_MODE' });
    }

    const normalizedRideType = String(rideType || 'random').toLowerCase();
    const isTourRide = normalizedRideType === 'tour';
    const captainVehicleType = String(userData.captainVehicleType || '').toLowerCase();
    const normalizedVehicleType = String(vehicleType || '').toLowerCase();

    const allowedVehicleTypes = ['car', 'bike', 'bus', 'truck', 'shazore', 'tour'];

    let inferredVehicleType;
    if (isTourRide || normalizedVehicleType === 'tour' || captainVehicleType === 'tour') {
      inferredVehicleType = 'tour';
    } else if (normalizedVehicleType && allowedVehicleTypes.includes(normalizedVehicleType)) {
      inferredVehicleType = normalizedVehicleType;
    } else if (captainVehicleType && allowedVehicleTypes.includes(captainVehicleType)) {
      inferredVehicleType = captainVehicleType;
    } else {
      inferredVehicleType = 'car';
    }

    if (!allowedVehicleTypes.includes(inferredVehicleType)) {
      return res.status(400).json({ success: false, error: 'vehicleType must be one of car, bike, bus, truck, shazore, tour', code: 'INVALID_VEHICLE_TYPE' });
    }

    if (captainVehicleType && !isTourRide && captainVehicleType !== 'tour' && normalizedVehicleType !== 'tour') {
      const postingType = inferredVehicleType === 'shazore' ? 'shazore' : inferredVehicleType;
      if (postingType !== captainVehicleType) {
        return res.status(403).json({ success: false, error: `You can only post ${captainVehicleType} rides`, code: 'VEHICLE_TYPE_MISMATCH' });
      }
    }

    if (
      exceedsMaxLength(startLocation, MAX_LOCATION) ||
      exceedsMaxLength(endLocation, MAX_LOCATION) ||
      exceedsMaxLength(exactLocation, MAX_LOCATION) ||
      exceedsMaxLength(exactDropLocation, MAX_LOCATION) ||
      exceedsMaxLength(routeDescription, MAX_LOCATION)
    ) {
      return res.status(400).json({
        success: false,
        error: `Location fields must be at most ${MAX_LOCATION} characters`,
        code: 'FIELD_TOO_LONG',
      });
    }

    const rawStartLocation = sanitizeString(labelFromLocation(startLocation), MAX_LOCATION);
    const rawEndLocation = sanitizeString(labelFromLocation(endLocation), MAX_LOCATION);
    const rawExactLocation = exactLocation
      ? sanitizeString(labelFromLocation(exactLocation), MAX_LOCATION)
      : '';
    const rawExactDropLocation = exactDropLocation
      ? sanitizeString(labelFromLocation(exactDropLocation), MAX_LOCATION)
      : '';
    const rawRouteDescription = routeDescription
      ? sanitizeString(routeDescription, MAX_LOCATION)
      : '';

    if (!rawStartLocation || !rawEndLocation || !departureTime || !totalSeats || !suggestedFare) {
      return res.status(400).json({ success: false, error: 'Missing required fields', code: 'MISSING_FIELDS' });
    }

    const parsedStartLat = parseNumber(startLat);
    const parsedStartLng = parseNumber(startLng);
    const parsedEndLat = parseNumber(endLat);
    const parsedEndLng = parseNumber(endLng);
    if (
      !isValidLat(parsedStartLat) ||
      !isValidLng(parsedStartLng) ||
      !isValidLat(parsedEndLat) ||
      !isValidLng(parsedEndLng) ||
      isZeroCoordinate(parsedStartLat, parsedStartLng) ||
      isZeroCoordinate(parsedEndLat, parsedEndLng)
    ) {
      return res.status(400).json({
        success: false,
        error: 'Valid map pickup and drop coordinates are required',
        code: 'MAP_COORDINATES_REQUIRED',
      });
    }

    const parsedDeparture = new Date(departureTime);
    if (Number.isNaN(parsedDeparture.getTime())) {
      return res.status(400).json({ success: false, error: 'departureTime is invalid', code: 'INVALID_DEPARTURE_TIME' });
    }
    if (parsedDeparture <= new Date()) {
      return res.status(400).json({ success: false, error: 'departureTime must be in the future', code: 'PAST_DEPARTURE_TIME' });
    }

    const parsedSeats = parseInt(totalSeats, 10);
    if (!Number.isInteger(parsedSeats) || parsedSeats < 1) {
      return res.status(400).json({ success: false, error: 'totalSeats must be at least 1', code: 'INVALID_TOTAL_SEATS' });
    }
    const captainVehicleSeats = parseInt(userData.vehicleSeats || userData.totalSeats || 0, 10);
    if (isTourRide && Number.isInteger(captainVehicleSeats) && captainVehicleSeats > 0 && parsedSeats > captainVehicleSeats) {
      return res.status(400).json({
        success: false,
        error: `Tour seats cannot exceed your registered vehicle seats (${captainVehicleSeats})`,
        code: 'TOUR_SEATS_EXCEED_VEHICLE',
      });
    }

    const parsedFare = parseNumber(suggestedFare);
    if (!parsedFare || parsedFare <= 0) {
      return res.status(400).json({ success: false, error: 'suggestedFare must be greater than 0', code: 'INVALID_FARE' });
    }
    if (parsedFare < 50) {
      return res.status(400).json({ success: false, error: 'Minimum fare is Rs 50', code: 'FARE_TOO_LOW' });
    }

    const normalizedLabels = await normalizeRouteLabels({
      startLocation: rawStartLocation,
      endLocation: rawEndLocation,
      exactPickup: rawExactLocation,
      exactDrop: rawExactDropLocation,
      city: userData.city,
    });

    const ride = {
      captainId: uid,
      captainName: userData.name || 'Anonymous',
      captainPhone: userData.phone || '',
      captainRating: userData.rating || 5.0,
      captainGender: (userData.gender || '').toString().toLowerCase() || null,
      startLocation: normalizedLabels.startLocation,
      endLocation: normalizedLabels.endLocation,
      exactLocation: rawExactLocation || null,
      exactDropLocation: rawExactDropLocation || null,
      routeDescription: rawRouteDescription || null,
      startLat: parsedStartLat,
      startLng: parsedStartLng,
      endLat: parsedEndLat,
      endLng: parsedEndLng,
      departureTime: parsedDeparture.toISOString(),
      totalSeats: parsedSeats,
      availableSeats: parsedSeats,
      full: false,
      suggestedFare: parsedFare,
      rideType: normalizedRideType,
      rideMode: normalizedRideMode,
      vehicleType: inferredVehicleType === 'shazore' ? 'truck' : inferredVehicleType,
      isShazoreRide: inferredVehicleType === 'shazore',
      isLadiesRide: ((userData.gender || '').toString().toLowerCase() === 'female'),
      acceptsDelivery: acceptsDelivery || false,
      vehicleInfo: vehicleInfo || `${userData.vehicleMake || ''} ${userData.vehicleModel || ''}`.trim() || 'Not Specified',
      vehiclePhotoUrl: userData.vehiclePhotoUrl || '',
      status: 'active',
      createdAt: new Date().toISOString(),
      updatedAt: new Date().toISOString(),
    };

    if (isTourRide) {
      const normalizedTourType = String(tourType || 'share').toLowerCase();
      if (!['share', 'solo'].includes(normalizedTourType)) {
        return res.status(400).json({ success: false, error: "tourType must be share or solo", code: 'INVALID_TOUR_TYPE' });
      }
      ride.tourType = normalizedTourType;
      if (normalizedTourType === 'share') {
        const parsedMax = parseInt(maxPassengers ?? parsedSeats, 10);
        if (!Number.isInteger(parsedMax) || parsedMax < 1) {
          ride.maxPassengers = parsedSeats;
        } else {
          ride.maxPassengers = Math.min(parsedMax, parsedSeats);
        }
      } else {
        ride.maxPassengers = 1;
      }
    }

    if (inferredVehicleType === 'truck' || inferredVehicleType === 'shazore') {
      const normalizedTruckSize = String(truckSize || '').toLowerCase();
      if (normalizedTruckSize && !['mini', 'half', 'full'].includes(normalizedTruckSize)) {
        return res.status(400).json({ success: false, error: "truckSize must be mini, half, or full", code: 'INVALID_TRUCK_SIZE' });
      }
      ride.cargoType = (cargoType || '').toString().trim() || null;
      ride.weightCapacity = weightCapacity != null ? Number(weightCapacity) : null;
      ride.truckSize = normalizedTruckSize || (inferredVehicleType === 'shazore' ? 'full' : null);
    }

    try {
      const routePolyline = await fetchRoutePolyline(
        ride.startLat, ride.startLng, ride.endLat, ride.endLng,
      );
      if (routePolyline && routePolyline.length >= 2) {
        ride.routePolyline = routePolyline;
      }
    } catch (routeErr) {
      console.error('Route polyline fetch failed (continuing without it):', routeErr.message);
    }

    const ref = await db.collection('rides').add(ride);

    return res.status(201).json({
      success: true,
      message: 'Ride posted successfully!',
      rideId: ref.id,
      ride: serializeRide(ref.id, ride)
    });
  } catch (err) {
    console.error('CRITICAL ERROR in postRide:', err);
    return res.status(500).json({ success: false, error: 'Internal Server Error: ' + err.message, code: 'POST_RIDE_ERROR' });
  }
};

// ─── CUSTOMER POST RIDE REQUEST ──────────────────────────
const postCustomerRideRequest = async (req, res) => {
  const uid = req.user ? req.user.uid : req.body.customerId;
  if (!uid) return res.status(400).json({ success: false, error: 'Customer ID is required', code: 'MISSING_CUSTOMER_ID' });

  try {
    const userDoc = await db.collection('users').doc(uid).get();
    if (!userDoc.exists) return res.status(404).json({ success: false, error: 'User profile not found.', code: 'USER_NOT_FOUND' });

    const userData = userDoc.data();

    const {
      startLocation, endLocation,
      startLat, startLng, endLat, endLng,
      departureTime, preferredFare,
      vehicleType, seats, exactLocation, exactDropLocation,
    } = req.body;

    if (!startLocation || !endLocation || !startLat || !startLng || !endLat || !endLng) {
      return res.status(400).json({ success: false, error: 'Missing required fields', code: 'MISSING_FIELDS' });
    }

    const request = {
      customerId: uid,
      customerName: userData.name || 'Anonymous',
      customerPhone: userData.phone || '',
      startLocation: sanitizeString(labelFromLocation(startLocation), MAX_LOCATION),
      endLocation: sanitizeString(labelFromLocation(endLocation), MAX_LOCATION),
      exactLocation: exactLocation ? sanitizeString(labelFromLocation(exactLocation), MAX_LOCATION) : null,
      exactDropLocation: exactDropLocation ? sanitizeString(labelFromLocation(exactDropLocation), MAX_LOCATION) : null,
      startLat: parseNumber(startLat),
      startLng: parseNumber(startLng),
      endLat: parseNumber(endLat),
      endLng: parseNumber(endLng),
      departureTime: departureTime ? new Date(departureTime).toISOString() : new Date().toISOString(),
      preferredFare: parseNumber(preferredFare) || 0,
      vehicleType: String(vehicleType || 'any').toLowerCase(),
      seats: parseInt(seats, 10) || 1,
      status: 'pending',
      isLadiesRide: ((userData.gender || '').toString().toLowerCase() === 'female'),
      createdAt: new Date().toISOString(),
      updatedAt: new Date().toISOString(),
    };

    const ref = await db.collection('customerRideRequests').add(request);

    // ─── NOTIFICATION: Customer request β†’ CAPTAINS ───
    await notifyCaptainsAboutRequest(request, ref.id, userData.city, request.vehicleType);

    return res.status(201).json({
      success: true,
      message: 'Ride request posted successfully! Captains will be notified.',
      requestId: ref.id,
      request,
    });

  } catch (err) {
    console.error('CRITICAL ERROR in postCustomerRideRequest:', err);
    return res.status(500).json({
      success: false,
      error: 'Internal Server Error: ' + err.message,
      code: 'POST_REQUEST_ERROR'
    });
  }
};

// ─── GET ACTIVE RIDES ─────────────────────────────────────
const getActiveRides = async (req, res) => {

  const { rideType, startLocation, endLocation, rideMode } = req.query;
  const pageLimit = Math.min(Math.max(parseInt(req.query.limit, 10) || 20, 1), 50);
  const afterId = (req.query.after || req.query.lastDocId || '').toString().trim();
  const hasRouteSearch = Boolean(startLocation || endLocation || req.query.lat || req.query.lng);
  const fetchLimit = hasRouteSearch ? Math.min(Math.max(pageLimit * 5, 75), 150) : pageLimit;
  
  // βœ… FIX D: Radius cap increased from 10 β†’ 100 km
  const pickupRadiusKm = Math.min(Math.max(parseNumber(req.query.pickupRadiusKm) || 3, 0.5), 100);
  const destinationRadiusKm = Math.min(Math.max(parseNumber(req.query.destinationRadiusKm) || 3, 0.5), 100);
  const routeRadiusKm = Math.min(Math.max(parseNumber(req.query.routeRadiusKm) || 2.5, 0.5), 10);
  
  try {
    await maybeCleanupExpiredRides();
    const userLat = parseNumber(req.query.lat ?? req.query.pickupLat);
    const userLng = parseNumber(req.query.lng ?? req.query.pickupLng);
    const destLat = parseNumber(req.query.destLat ?? req.query.endLat);
    const destLng = parseNumber(req.query.destLng ?? req.query.endLng);
    let requesterGender = '';
    if (req.user?.uid) {
      const requesterDoc = await db.collection('users').doc(req.user.uid).get();
      if (requesterDoc.exists) requesterGender = (requesterDoc.data().gender || '').toString().toLowerCase();
    }

    // βœ… FIX: Reduced grace period from 30 minutes to 10 minutes
    const RIDE_GRACE_PERIOD_MS = 10 * 60 * 1000; // 10 minutes
    const now = new Date().toISOString();
    const nowMs = Date.now();
    const graceFloor = new Date(nowMs - RIDE_GRACE_PERIOD_MS).toISOString();
    let query = db.collection('rides').where('status', '==', 'active').where('departureTime', '>=', graceFloor);
    if (afterId) {
      const afterDoc = await db.collection('rides').doc(afterId).get();
      if (afterDoc.exists) {
        query = query.orderBy('departureTime').startAfter(afterDoc);
      } else {
        query = query.orderBy('departureTime');
      }
    } else {
      query = query.orderBy('departureTime');
    }
    const snap = await query.limit(fetchLimit).get();
    let rides = snap.docs.map(d => serializeRide(d.id, d.data()));
    rides = rides.map((r) => ({
      ...r,
      isGracePeriod: new Date(r.departureTime || 0).getTime() < nowMs,
    }));
    const lastDoc = snap.docs.length > 0 ? snap.docs[snap.docs.length - 1] : null;

    if (requesterGender !== 'female') {
      rides = rides.filter(r => r.isLadiesRide !== true);
    }

    if (rideType) {
      const rt = String(rideType).toLowerCase();
      if (['car', 'bike', 'bus', 'truck'].includes(rt)) {
        rides = rides.filter(r => (r.vehicleType || '').toString().toLowerCase() === rt);
      } else if (rt === 'shazore') {
        rides = rides.filter(r => r.isShazoreRide === true);
      } else if (rt === 'tour') {
        rides = rides.filter(r => (r.rideType || '').toString().toLowerCase() === 'tour');
      } else if (rt === 'ladies') {
        if (requesterGender !== 'female') rides = [];
        else rides = rides.filter(r => r.isLadiesRide === true);
      } else if (rt !== 'all' && rt !== 'random') {
        rides = rides.filter(r => (r.rideType || '').toString().toLowerCase() === rt);
      }
    }

    const wantsTours = String(rideType || '').toLowerCase() === 'tour';
    if (!wantsTours) {
      rides = rides.filter((r) =>
        (r.rideType || '').toString().toLowerCase() !== 'tour' &&
        (r.vehicleType || '').toString().toLowerCase() !== 'tour'
      );
    }

    if (rideMode) {
      const rm = String(rideMode).toLowerCase();
      if (['solo', 'share'].includes(rm)) {
        rides = rides.filter(r => (r.rideMode || 'share').toString().toLowerCase() === rm);
      }
    }

    // Never expose a ride that has no bookable seat left
    rides = rides.filter((r) => r.full !== true && Number(r.availableSeats || 0) > 0);

    if (req.user?.uid) {
      try {
        const myDealsSnap = await db
          .collection('deals')
          .where('customerId', '==', req.user.uid)
          .where('status', 'in', [DEAL_STATUS.PENDING, DEAL_STATUS.CONFIRMED, DEAL_STATUS.STARTED])
          .get();
        const excludedRideIds = new Set(myDealsSnap.docs.map((d) => d.data().rideId).filter(Boolean));
        if (excludedRideIds.size > 0) {
          rides = rides.filter((r) => !excludedRideIds.has(r.id));
        }
      } catch (excludeErr) {
        console.error('Error excluding already-booked rides:', excludeErr.message);
      }
    }

    // βœ… FIX C: Pickup‑only radius filter
    const canCoordinateMatch = userLat != null && userLng != null && destLat != null && destLng != null;
    const pickupOnlyMatch = userLat != null && userLng != null && !canCoordinateMatch;
    const shouldRouteFilter = Boolean(startLocation || endLocation || canCoordinateMatch || pickupOnlyMatch);
    
    if (shouldRouteFilter) {
      rides = rides
        .map((r) => {
          const coordinateMatch = canCoordinateMatch
            ? routeMatchScore(r, {
                startLocation,
                endLocation,
                pickupLat: userLat,
                pickupLng: userLng,
                destLat,
                destLng,
                pickupRadiusKm,
                destinationRadiusKm,
                routeRadiusKm,
              })
            : null;

          // βœ… NEW: pickup-only distance and suitability
          let pickupOnlyDistance = null;
          let pickupOnlySuitable = null;
          if (pickupOnlyMatch) {
            pickupOnlyDistance = distanceKm(userLat, userLng, parseNumber(r.startLat), parseNumber(r.startLng));
            pickupOnlySuitable = pickupOnlyDistance != null && pickupOnlyDistance <= pickupRadiusKm;
          }

          const destMatch = endLocation
            ? calculateDestinationMatch(endLocation, r.startLocation, r.endLocation)
            : { score: 0, passesThrough: false };
          const labelOverlap = startLocation && endLocation
            ? locationsOverlap(startLocation, endLocation, r.startLocation, r.endLocation)
            : false;
          const pickupLabelScore = startLocation
            ? Math.max(
                levenshteinSimilarity(startLocation, r.startLocation),
                levenshteinSimilarity(startLocation, r.exactLocation),
              )
            : 0;

          const suitable = canCoordinateMatch
            ? coordinateMatch?.suitable === true
            : pickupOnlyMatch
              ? pickupOnlySuitable
              : labelOverlap ||
                (pickupLabelScore >= 80 && (!endLocation || destMatch.score >= 70 || destMatch.passesThrough));

          const relevanceScore =
            (coordinateMatch?.score || 0) +
            (labelOverlap ? 20 : 0) +
            Math.floor(pickupLabelScore * 0.15) +
            Math.floor((destMatch.score || 0) * 0.15);

          return {
            ...r,
            routeMatchScore: relevanceScore,
            destinationMatchScore: destMatch.score,
            routePassesThrough: coordinateMatch?.isProximityMatch === true || destMatch.passesThrough,
            isDirectMatch: coordinateMatch?.isDirectMatch === true,
            isProximityMatch: coordinateMatch?.isProximityMatch === true || destMatch.passesThrough,
            directionOk: coordinateMatch?.directionOk ?? null,
            pickupProgress: coordinateMatch?.pickupProgress ?? null,
            destinationProgress: coordinateMatch?.destinationProgress ?? null,
            distanceFromPickup: coordinateMatch?.distanceFromPickup ?? (userLat != null && userLng != null ? distanceKm(userLat, userLng, parseNumber(r.startLat), parseNumber(r.startLng)) : null),
            distanceFromDest: coordinateMatch?.distanceFromDestination ?? (destLat != null && destLng != null ? distanceKm(destLat, destLng, parseNumber(r.endLat), parseNumber(r.endLng)) : null),
            routeProximityKm: coordinateMatch?.pickupRouteKm ?? null,
            destinationRouteProximityKm: coordinateMatch?.destinationRouteKm ?? null,
            captainDistanceFromPickup: coordinateMatch?.captainDistanceFromPickup ?? null,
            _suitable: suitable,
          };
        })
        .filter((r) => r._suitable === true)
        .sort((a, b) => {
          if ((a.routeMatchScore || 0) !== (b.routeMatchScore || 0)) {
            return (b.routeMatchScore || 0) - (a.routeMatchScore || 0);
          }
          const aCreated = Date.parse(a.createdAt || '') || 0;
          const bCreated = Date.parse(b.createdAt || '') || 0;
          if (aCreated !== bCreated) return bCreated - aCreated;
          const aPickup = a.distanceFromPickup == null ? Number.MAX_SAFE_INTEGER : a.distanceFromPickup;
          const bPickup = b.distanceFromPickup == null ? Number.MAX_SAFE_INTEGER : b.distanceFromPickup;
          if (aPickup !== bPickup) return aPickup - bPickup;
          const aDest = a.distanceFromDest == null ? Number.MAX_SAFE_INTEGER : a.distanceFromDest;
          const bDest = b.distanceFromDest == null ? Number.MAX_SAFE_INTEGER : b.distanceFromDest;
          if (aDest !== bDest) return aDest - bDest;
          return String(a.departureTime || '').localeCompare(String(b.departureTime || ''));
        })
        .slice(0, pageLimit)
        .map((r) => {
          const { _suitable, ...cleanRide } = r;
          return {
            ...cleanRide,
            distanceFromPickup: cleanRide.distanceFromPickup == null ? null : Number(cleanRide.distanceFromPickup.toFixed(2)),
            distanceFromDest: cleanRide.distanceFromDest == null ? null : Number(cleanRide.distanceFromDest.toFixed(2)),
            distanceKm: cleanRide.distanceFromPickup == null ? null : Number(cleanRide.distanceFromPickup.toFixed(2)),
            routeProximityKm: cleanRide.routeProximityKm == null ? null : Number(cleanRide.routeProximityKm.toFixed(2)),
            destinationRouteProximityKm: cleanRide.destinationRouteProximityKm == null ? null : Number(cleanRide.destinationRouteProximityKm.toFixed(2)),
            captainDistanceFromPickup: cleanRide.captainDistanceFromPickup == null ? null : Number(cleanRide.captainDistanceFromPickup.toFixed(2)),
          };
        });
    }

    res.set('Cache-Control', 'public, max-age=10');
    return res.json({
      success: true,
      count: rides.length,
      rides,
      hasMore: snap.docs.length >= fetchLimit,
      lastDocId: lastDoc ? lastDoc.id : null,
    });
  } catch (err) {
    console.error('Error fetching rides:', err);
    return res.status(500).json({ success: false, error: err.message, code: 'GET_RIDES_ERROR' });
  }
};

// ─── UPDATE RIDE STATUS ──────────────────────────────────
const updateRideStatus = async (req, res) => {
  const { rideId } = req.params;
  const { status } = req.body;
  const uid = req.user ? req.user.uid : req.body.captainId;
  const validStatuses = ['active', 'filled', 'completed', 'cancelled'];
  if (!validStatuses.includes(status)) return res.status(400).json({ success: false, error: 'Invalid status type', code: 'INVALID_STATUS' });

  try {
    const rideRef = db.collection('rides').doc(rideId);
    const rideDoc = await rideRef.get();
    if (!rideDoc.exists) return res.status(404).json({ success: false, error: 'Ride not found', code: 'RIDE_NOT_FOUND' });
    if (rideDoc.data().captainId !== uid) return res.status(403).json({ success: false, error: 'Unauthorized', code: 'UNAUTHORIZED' });
    await rideRef.update({ status, updatedAt: new Date().toISOString() });
    return res.json({ success: true, message: `Ride marked as ${status}` });
  } catch (err) {
    return res.status(500).json({ success: false, error: err.message, code: 'UPDATE_RIDE_ERROR' });
  }
};

// ─── UPDATE RIDE ──────────────────────────────────────────
const updateRide = async (req, res) => {
  const { rideId } = req.params;
  const uid = req.user ? req.user.uid : req.body.captainId;
  if (!uid) return res.status(400).json({ success: false, error: 'Captain ID is required', code: 'MISSING_CAPTAIN_ID' });

  try {
    const rideRef = db.collection('rides').doc(rideId);
    const rideDoc = await rideRef.get();
    if (!rideDoc.exists) return res.status(404).json({ success: false, error: 'Ride not found', code: 'RIDE_NOT_FOUND' });

    const existingRide = rideDoc.data();
    if (existingRide.captainId !== uid) {
      return res.status(403).json({ success: false, error: 'Unauthorized', code: 'UNAUTHORIZED' });
    }

    const {
      startLocation,
      endLocation,
      startLat,
      startLng,
      endLat,
      endLng,
      departureTime,
      suggestedFare,
      totalSeats,
      rideType,
      rideMode,
      exactLocation,
      exactDropLocation,
      routeDescription,
    } = req.body;

    if (
      startLocation != null && exceedsMaxLength(startLocation, MAX_LOCATION) ||
      endLocation != null && exceedsMaxLength(endLocation, MAX_LOCATION) ||
      exactLocation != null && exceedsMaxLength(exactLocation, MAX_LOCATION) ||
      exactDropLocation != null && exceedsMaxLength(exactDropLocation, MAX_LOCATION) ||
      routeDescription != null && exceedsMaxLength(routeDescription, MAX_LOCATION)
    ) {
      return res.status(400).json({
        success: false,
        error: `Location fields must be at most ${MAX_LOCATION} characters`,
        code: 'FIELD_TOO_LONG',
      });
    }

    const nextStartLocation = startLocation != null ? sanitizeString(labelFromLocation(startLocation), MAX_LOCATION) : existingRide.startLocation;
    const nextEndLocation = endLocation != null ? sanitizeString(labelFromLocation(endLocation), MAX_LOCATION) : existingRide.endLocation;
    const nextExactLocation = exactLocation != null ? sanitizeString(labelFromLocation(exactLocation), MAX_LOCATION) : existingRide.exactLocation;
    const nextExactDropLocation = exactDropLocation != null ? sanitizeString(labelFromLocation(exactDropLocation), MAX_LOCATION) : existingRide.exactDropLocation;
    const nextRouteDescription = routeDescription != null
      ? sanitizeString(routeDescription, MAX_LOCATION)
      : existingRide.routeDescription;

    if (!nextStartLocation || !nextEndLocation) {
      return res.status(400).json({ success: false, error: 'startLocation and endLocation are required', code: 'MISSING_FIELDS' });
    }

    const updatePayload = {
      updatedAt: new Date().toISOString(),
      startLocation: nextStartLocation,
      endLocation: nextEndLocation,
      exactLocation: nextExactLocation || null,
      exactDropLocation: nextExactDropLocation || null,
      routeDescription: nextRouteDescription || null,
    };

    let routeCoordsChanged = false;

    if (startLat != null || startLng != null || endLat != null || endLng != null) {
      const parsedStartLat = startLat != null ? parseNumber(startLat) : existingRide.startLat;
      const parsedStartLng = startLng != null ? parseNumber(startLng) : existingRide.startLng;
      const parsedEndLat = endLat != null ? parseNumber(endLat) : existingRide.endLat;
      const parsedEndLng = endLng != null ? parseNumber(endLng) : existingRide.endLng;

      if (
        !isValidLat(parsedStartLat) ||
        !isValidLng(parsedStartLng) ||
        !isValidLat(parsedEndLat) ||
        !isValidLng(parsedEndLng) ||
        isZeroCoordinate(parsedStartLat, parsedStartLng) ||
        isZeroCoordinate(parsedEndLat, parsedEndLng)
      ) {
        return res.status(400).json({ success: false, error: 'Valid map coordinates are required', code: 'MAP_COORDINATES_REQUIRED' });
      }

      updatePayload.startLat = parsedStartLat;
      updatePayload.startLng = parsedStartLng;
      updatePayload.endLat = parsedEndLat;
      updatePayload.endLng = parsedEndLng;
      routeCoordsChanged = true;
    }

    if (routeCoordsChanged) {
      try {
        const routePolyline = await fetchRoutePolyline(
          updatePayload.startLat, updatePayload.startLng, updatePayload.endLat, updatePayload.endLng,
        );
        if (routePolyline && routePolyline.length >= 2) {
          updatePayload.routePolyline = routePolyline;
        }
      } catch (routeErr) {
        console.error('Route polyline refresh failed (continuing without it):', routeErr.message);
      }
    }

    if (departureTime != null) {
      const parsedDeparture = new Date(departureTime);
      if (Number.isNaN(parsedDeparture.getTime())) {
        return res.status(400).json({ success: false, error: 'departureTime is invalid', code: 'INVALID_DEPARTURE_TIME' });
      }
      if (parsedDeparture <= new Date()) {
        return res.status(400).json({ success: false, error: 'departureTime must be in the future', code: 'PAST_DEPARTURE_TIME' });
      }
      updatePayload.departureTime = parsedDeparture.toISOString();
    }

    if (suggestedFare != null) {
      const parsedFare = parseNumber(suggestedFare);
      if (!parsedFare || parsedFare <= 0) {
        return res.status(400).json({ success: false, error: 'suggestedFare must be greater than 0', code: 'INVALID_FARE' });
      }
      if (parsedFare < 50) {
        return res.status(400).json({ success: false, error: 'Minimum fare is Rs 50', code: 'FARE_TOO_LOW' });
      }
      updatePayload.suggestedFare = parsedFare;
    }

    if (totalSeats != null) {
      const parsedSeats = parseInt(totalSeats, 10);
      if (!Number.isInteger(parsedSeats) || parsedSeats < 1) {
        return res.status(400).json({ success: false, error: 'totalSeats must be at least 1', code: 'INVALID_TOTAL_SEATS' });
      }
      const currentAvailable = Number(existingRide.availableSeats ?? existingRide.totalSeats ?? parsedSeats);
      const reservedSeats = Math.max(
        0,
        Number(existingRide.totalSeats ?? parsedSeats) - currentAvailable,
      );
      if (parsedSeats < reservedSeats) {
        return res.status(400).json({
          success: false,
          error: `totalSeats cannot be less than ${reservedSeats} already booked seat(s)`,
          code: 'SEATS_BELOW_BOOKED_COUNT',
        });
      }
      updatePayload.totalSeats = parsedSeats;
      updatePayload.availableSeats = Math.max(0, Math.min(currentAvailable, parsedSeats));
      updatePayload.full = updatePayload.availableSeats <= 0;
      if (updatePayload.full && existingRide.status === RIDE_STATUS.ACTIVE) {
        updatePayload.status = RIDE_STATUS.FILLED;
      }
    }

    if (rideType != null) {
      updatePayload.rideType = String(rideType).toLowerCase();
    }

    if (rideMode != null) {
      const normalizedRideMode = String(rideMode).toLowerCase();
      if (!['share', 'solo'].includes(normalizedRideMode)) {
        return res.status(400).json({ success: false, error: 'rideMode must be share or solo', code: 'INVALID_RIDE_MODE' });
      }
      updatePayload.rideMode = normalizedRideMode;
    }

    await rideRef.update(updatePayload);
    const updatedDoc = await rideRef.get();
    return res.json({ success: true, message: 'Ride updated successfully', ride: serializeRide(updatedDoc.id, updatedDoc.data()) });
  } catch (err) {
    return res.status(500).json({ success: false, error: err.message, code: 'UPDATE_RIDE_ERROR' });
  }
};

// ─── DELETE RIDE ──────────────────────────────────────────
const deleteRide = async (req, res) => {
  const { rideId } = req.params;
  const uid = req.user ? req.user.uid : req.body.captainId;
  if (!uid) return res.status(400).json({ success: false, error: 'Captain ID is required', code: 'MISSING_CAPTAIN_ID' });

  try {
    const rideRef = db.collection('rides').doc(rideId);
    const rideDoc = await rideRef.get();
    if (!rideDoc.exists) return res.status(404).json({ success: false, error: 'Ride not found', code: 'RIDE_NOT_FOUND' });
    if (rideDoc.data().captainId !== uid) return res.status(403).json({ success: false, error: 'Unauthorized', code: 'UNAUTHORIZED' });

    const dealsSnap = await db.collection('deals').where('rideId', '==', rideId).get();
    const now = new Date().toISOString();
    const batch = db.batch();
    dealsSnap.docs.forEach((dealDoc) => {
      const status = (dealDoc.data().status || '').toString().toLowerCase();
      if ([DEAL_STATUS.PENDING, DEAL_STATUS.CONFIRMED, DEAL_STATUS.STARTED].includes(status)) {
        batch.update(dealDoc.ref, {
          status: DEAL_STATUS.CANCELLED,
          cancelledBy: 'captain_delete',
          updatedAt: now,
        });
      }
    });
    batch.delete(rideRef);
    await batch.commit();

    await Promise.all(
      dealsSnap.docs
        .map((dealDoc) => dealDoc.data())
        .filter((deal) => deal.customerId && [DEAL_STATUS.PENDING, DEAL_STATUS.CONFIRMED, DEAL_STATUS.STARTED].includes((deal.status || '').toString().toLowerCase()))
        .map((deal) => pushToUser(deal.customerId, {
          title: 'Ride unavailable',
          body: 'The captain deleted this ride post.',
          type: 'deal_cancelled',
          data: { rideId, screen: 'my-bookings' },
        }).catch(() => null)),
    );

    return res.json({
      success: true,
      message: 'Ride deleted successfully',
      cancelledBookings: dealsSnap.docs.length,
    });
  } catch (err) {
    return res.status(500).json({ success: false, error: err.message, code: 'DELETE_RIDE_ERROR' });
  }
};

// ─── GET MY RIDES ─────────────────────────────────────────
const getMyRides = async (req, res) => {
  const uid = req.user ? req.user.uid : req.query.captainId;
  if (!uid) return res.status(400).json({ success: false, error: 'Captain ID is required', code: 'MISSING_CAPTAIN_ID' });

  try {
    await maybeCleanupExpiredRides();
    const snap = await db.collection('rides').where('captainId', '==', uid).orderBy('createdAt', 'desc').get();
    const rides = snap.docs.map(d => serializeRide(d.id, d.data()));
    return res.json({ success: true, rides });
  } catch (err) {
    return res.status(500).json({ success: false, error: err.message, code: 'GET_MY_RIDES_ERROR' });
  }
};

// ─── GET RIDE BY ID ───────────────────────────────────────
const getRideById = async (req, res) => {
  const { rideId } = req.params;
  try {
    await maybeCleanupExpiredRides();
    const rideDoc = await db.collection('rides').doc(rideId).get();
    if (!rideDoc.exists) return res.status(404).json({ success: false, error: 'Ride not found', code: 'RIDE_NOT_FOUND' });
    res.set('Cache-Control', 'public, max-age=10');
    return res.json({ success: true, ride: serializeRide(rideDoc.id, rideDoc.data()) });
  } catch (err) {
    return res.status(500).json({ success: false, error: err.message, code: 'GET_RIDE_BY_ID_ERROR' });
  }
};

// ─── UPDATE RIDE LOCATION ─────────────────────────────────
const updateRideLocation = async (req, res) => {
  const { rideId } = req.params;
  const { lat, lng } = req.body;
  const uid = req.user ? req.user.uid : req.body.captainId;
  if (lat === undefined || lng === undefined) return res.status(400).json({ success: false, error: 'Latitude and Longitude are required', code: 'MISSING_COORDINATES' });

  try {
    const rideRef = db.collection('rides').doc(rideId);
    const rideDoc = await rideRef.get();
    if (!rideDoc.exists) return res.status(404).json({ success: false, error: 'Ride not found', code: 'RIDE_NOT_FOUND' });
    if (rideDoc.data().captainId !== uid) return res.status(403).json({ success: false, error: 'Unauthorized', code: 'UNAUTHORIZED' });
    await rideRef.update({ captainLat: parseFloat(lat), captainLng: parseFloat(lng), updatedAt: new Date().toISOString() });
    return res.json({ success: true, message: 'Location updated successfully' });
  } catch (err) {
    return res.status(500).json({ success: false, error: err.message, code: 'UPDATE_LOCATION_ERROR' });
  }
};

// ─── GET CAPTAIN STATS ────────────────────────────────────
const getCaptainStats = async (req, res) => {
  try {
    const captainId = req.user.uid;
    const now = new Date();

    const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate());
    const startOfWeek = new Date(startOfToday);
    startOfWeek.setDate(startOfToday.getDate() - startOfToday.getDay());
    const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);

    const ridesSnap = await db
      .collection('rides')
      .where('captainId', '==', captainId)
      .where('status', '==', 'completed')
      .get();

    const completedDealsSnap = await db
      .collection('deals')
      .where('captainId', '==', captainId)
      .where('status', '==', DEAL_STATUS.COMPLETED)
      .get();

    const txSnap = await db
      .collection('transactions')
      .where('walletId', '==', captainId)
      .get();

    const earningTypes = new Set(['ride_earning', 'earning', 'commission_credit', 'topup']);
    const deductionTypes = new Set(['commission_deduction', 'commission']);

    function calcStats(filterFn) {
      let ridesCompleted = 0;
      let earningsRs = 0;

      ridesSnap.docs.forEach((doc) => {
        const data = doc.data();
        const completedAt = new Date(data.completedAt || data.updatedAt || data.createdAt || 0);
        if (filterFn(completedAt)) ridesCompleted++;
      });

      txSnap.docs.forEach((doc) => {
        const tx = doc.data();
        const createdAt = new Date(tx.createdAt || 0);
        if (!filterFn(createdAt)) return;
        const amt = Math.abs(Number(tx.amount || 0));
        if (earningTypes.has(tx.type)) earningsRs += amt;
        else if (deductionTypes.has(tx.type)) earningsRs -= amt;
      });

      return { ridesCompleted, earningsRs: Math.max(0, Math.round(earningsRs)) };
    }

    const today    = calcStats((d) => d >= startOfToday);
    const week     = calcStats((d) => d >= startOfWeek);
    const month    = calcStats((d) => d >= startOfMonth);
    const lifetime = calcStats(() => true);

    const ratedDeals = completedDealsSnap.docs
      .map((doc) => doc.data())
      .filter((deal) => deal.rating != null);
    const reviewCount = ratedDeals.length;
    const ratingSum = ratedDeals.reduce((sum, deal) => sum + Number(deal.rating || 0), 0);
    const averageRating = reviewCount > 0 ? Number((ratingSum / reviewCount).toFixed(2)) : 0;
    const totalCompletedRides = completedDealsSnap.size || lifetime.ridesCompleted;

    await db.collection('users').doc(captainId).set(
      {
        rating: averageRating,
        averageRating,
        reviewCount,
        totalReviews: reviewCount,
        completedRides: totalCompletedRides,
        totalRides: Math.max(totalCompletedRides, lifetime.ridesCompleted),
        updatedAt: new Date().toISOString(),
      },
      { merge: true },
    );

    return res.json({
      success: true,
      today,
      week,
      month,
      lifetime,
      profile: {
        averageRating,
        reviewCount,
        totalCompletedRides,
      },
    });
  } catch (err) {
    return res.status(500).json({ success: false, error: err.message, code: 'CAPTAIN_STATS_ERROR' });
  }
};

// ─── EXPORTS ──────────────────────────────────────────────
module.exports = {
  postRide,
  postCustomerRideRequest,
  getActiveRides,
  updateRideStatus,
  getMyRides,
  getRideById,
  updateRideLocation,
  updateRide,
  deleteRide,
  getCaptainStats,
  calculateRouteProximityMeters,
  buildRideRouteSummary,
  routeMatchScore,
  notifyCaptainsAboutRequest,
};