File size: 47,271 Bytes
a7abf85
d3245ed
36d2eb6
b12f5e4
9bf1d7d
b12f5e4
444fe60
a7abf85
01b8424
a7abf85
01b8424
 
 
a7abf85
01b8424
a7abf85
 
098997e
 
d3245ed
 
3dc3601
8697d42
d3245ed
a69087c
d3245ed
 
 
8dec0d6
13d210d
 
 
98089df
 
 
 
 
 
 
 
3ad292c
a7abf85
 
c096c2c
 
 
 
 
 
 
 
 
 
 
 
 
3caadf3
c096c2c
444fe60
5e4ad36
 
 
 
 
 
425a7d1
 
 
 
 
 
509b124
 
425a7d1
 
 
 
b531c61
 
 
 
 
 
 
 
 
 
 
f358ba9
b531c61
 
 
 
 
 
 
 
 
 
53027ce
b531c61
daf672f
 
 
 
 
 
 
ca21363
127463e
daf672f
 
f358ba9
daf672f
 
b531c61
daf672f
425a7d1
f358ba9
b531c61
f358ba9
 
 
 
 
 
 
 
 
 
 
 
d7eda21
 
 
 
f358ba9
 
d7eda21
b531c61
f358ba9
d7eda21
 
 
 
 
f358ba9
d7eda21
 
 
 
 
 
 
 
f358ba9
 
b531c61
425a7d1
 
6bbfc57
f358ba9
b60c81b
db8a8bb
daf672f
9de76b8
2fffb7d
f8008d1
6479332
 
 
f8008d1
 
6479332
 
 
 
 
 
f8008d1
6479332
 
f8008d1
6479332
 
 
 
 
f8008d1
 
 
 
 
6479332
 
f8008d1
 
 
6479332
 
 
 
 
 
 
 
 
 
 
 
 
 
d5928b0
6479332
 
 
9de76b8
dc5e8d2
6307340
98312f8
6307340
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98312f8
6307340
 
98312f8
6307340
 
 
98312f8
5c3223b
 
 
6bbfc57
5c3223b
6bbfc57
 
5c3223b
6bbfc57
5c3223b
 
6bbfc57
5c3223b
6bbfc57
 
 
5c3223b
d316910
9de76b8
dc5e8d2
 
 
9de76b8
 
 
c4269f9
9de76b8
 
 
dc5e8d2
5c3223b
6bbfc57
 
 
 
820dc7a
80bae4f
 
 
 
 
 
 
 
 
 
 
 
 
 
9bf1d7d
 
 
f62a0a9
 
0270ecb
 
 
 
 
 
 
 
9de76b8
a7abf85
 
 
 
 
7bec215
a7abf85
d792a6f
 
 
a7abf85
8d8058b
5e4ad36
7bec215
 
b97f93a
 
7bec215
 
 
b97f93a
 
d792a6f
 
b35dc9c
d792a6f
 
 
 
 
5e4ad36
d792a6f
5e4ad36
2b574f3
5e4ad36
8d8058b
5e4ad36
 
8d8058b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b35dc9c
8d8058b
 
 
5e4ad36
bb11e62
a7abf85
 
 
 
98089df
5e4ad36
d792a6f
a7abf85
8d8058b
a7abf85
d792a6f
a7abf85
 
5e4ad36
a7abf85
444fe60
d792a6f
5e4ad36
b97f93a
494fc19
 
 
 
 
 
a60e6aa
494fc19
9fec724
494fc19
 
 
 
 
 
b95cfbd
9fec724
 
 
 
 
b95cfbd
9fec724
494fc19
9fec724
494fc19
 
9fec724
494fc19
 
 
 
 
 
 
9fec724
494fc19
 
 
9fec724
494fc19
 
 
 
9fec724
a60e6aa
494fc19
a60e6aa
494fc19
 
 
a60e6aa
494fc19
 
 
098997e
494fc19
b01288a
dab4dc4
 
 
738ef9d
 
b3777a0
 
 
 
 
 
1c7e537
b3777a0
 
acbce59
1c7e537
b3777a0
1c7e537
 
 
f3f2f2b
738ef9d
cc40c6f
 
 
 
 
 
738ef9d
 
cc40c6f
89d0ec7
cc40c6f
001eae9
dab4dc4
 
cc40c6f
dab4dc4
 
8e5b217
 
 
 
 
89d0ec7
 
 
 
bc4bcaa
89d0ec7
 
 
 
 
 
ee4b0b3
 
89d0ec7
ee4b0b3
89d0ec7
001eae9
89d0ec7
 
bc8319c
 
 
 
001eae9
bc8319c
 
dab4dc4
bc8319c
001eae9
 
 
808a232
 
 
89d0ec7
 
738ef9d
 
 
 
808a232
 
 
 
738ef9d
 
 
 
 
 
 
808a232
8e5b217
738ef9d
 
 
8e5b217
738ef9d
 
a7abf85
dab4dc4
 
738ef9d
 
cc40c6f
 
dab4dc4
1c7e537
dab4dc4
 
738ef9d
 
dab4dc4
cc40c6f
9ed7d51
1c7e537
 
dab4dc4
738ef9d
808a232
a7abf85
 
052e3ec
c053032
42a3b7a
af1892f
a7abf85
052e3ec
a7abf85
052e3ec
a7abf85
 
 
 
eea4e68
6867b80
eea4e68
 
a40731c
 
 
 
 
 
a5663b6
eea4e68
 
 
 
 
 
 
 
 
 
e4a58d1
052e3ec
27c8731
e4a58d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
052e3ec
eea4e68
 
 
 
 
 
052e3ec
 
eea4e68
 
a7abf85
 
052e3ec
eea4e68
a7abf85
8945517
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b69e71d
a7abf85
 
20f0433
 
 
 
 
 
 
 
 
a7abf85
 
 
 
33cad0b
 
5c3fd6b
33cad0b
 
 
 
76eace7
20f0433
 
a06e7a7
5c3fd6b
 
 
20f0433
 
 
5c3fd6b
 
 
 
 
 
 
 
 
 
 
 
 
 
33cad0b
20f0433
 
 
 
 
 
 
33cad0b
 
5c3fd6b
 
20f0433
5c3fd6b
20f0433
5c3fd6b
33cad0b
20f0433
 
 
 
 
 
 
 
 
 
 
33cad0b
af1892f
61b8aca
444fe60
af1892f
a7abf85
 
61b8aca
98110bb
a7abf85
 
 
 
 
61b8aca
 
a7abf85
61b8aca
 
 
a7abf85
61b8aca
 
 
 
 
 
8f12848
61b8aca
 
 
a7abf85
61b8aca
a7abf85
 
 
61b8aca
a7abf85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
731301f
 
 
 
 
 
a7abf85
731301f
0767ada
731301f
 
 
0767ada
731301f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7abf85
 
 
731301f
221944a
a7abf85
b25a46a
 
 
 
 
 
 
 
a7abf85
b25a46a
f85d242
 
a7abf85
 
f85d242
a7abf85
90e7132
f85d242
a7abf85
 
f85d242
 
 
 
 
 
90e7132
f85d242
acd347f
 
f85d242
 
 
 
acd347f
f85d242
a7abf85
acd347f
 
 
 
 
 
9c827f0
acd347f
 
 
9c827f0
 
 
a7abf85
f85d242
a7abf85
e35b13d
 
98110bb
b3777a0
8797416
e35b13d
 
 
 
adfe5ee
c673217
adfe5ee
 
e35b13d
b95cfbd
e35b13d
 
 
 
317f904
e35b13d
 
 
2dca26a
c673217
0bbedc4
 
 
 
 
 
a7a52a3
adfe5ee
0bbedc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adfe5ee
 
c673217
 
 
 
 
 
 
b95cfbd
9abbf76
 
 
 
 
 
 
 
 
 
 
 
b95cfbd
adfe5ee
 
 
 
 
 
 
9abbf76
c673217
adfe5ee
 
 
 
b95cfbd
 
 
e35b13d
98110bb
adfe5ee
e35b13d
317f904
e35b13d
9d3e1c8
 
 
 
 
76eace7
a7abf85
11514ee
a7abf85
0cdd44a
9d3e1c8
a7abf85
9d3e1c8
 
a7abf85
9d3e1c8
 
 
 
 
 
a7abf85
9d3e1c8
 
a7abf85
 
fcdec6b
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
from flask import Flask, render_template, request, jsonify, redirect, url_for, session
from flask_session import Session  # Import the Session class
from flask.sessions import SecureCookieSessionInterface  # Import the class
from salesforce import get_salesforce_connection
from datetime import timedelta
import os

# Initialize Flask app and Salesforce connection
print("Starting app...")
app = Flask(__name__)
print("Flask app initialized.")

# Add debug logs in Salesforce connection setup
sf = get_salesforce_connection()
print("Salesforce connection established.")

# Set the secret key to handle sessions securely
app.secret_key = os.getenv("SECRET_KEY", "sSSjyhInIsUohKpG8sHzty2q")  # Replace with a secure key

# Configure the session type
app.config["SESSION_TYPE"] = "filesystem"  # Use filesystem for session storage
#app.config["SESSION_COOKIE_NAME"] = "my_session"  # Optional: Change session cookie name
app.config["SESSION_COOKIE_SECURE"] = True  # Ensure cookies are sent over HTTPS
app.config["SESSION_COOKIE_SAMESITE"] = "None"  # Allow cross-site cookies

# Initialize the session
Session(app)  # Correctly initialize the Session object
print("Session interface configured.")

# Ensure secure session handling for environments like Hugging Face
app.session_interface = SecureCookieSessionInterface()
print("Session interface configured.")
import random
import string

def generate_referral_code(length=8):
    # Generates a random referral code with uppercase, lowercase letters, and digits
    characters = string.ascii_letters + string.digits  # A-Z, a-z, 0-9
    referral_code = ''.join(random.choice(characters) for _ in range(length))
    return referral_code

@app.route("/")
def home():
    # Fetch user details from URL parameters
    user_email = request.args.get("email")
    user_name = request.args.get("name")

    if user_email and user_name:
        session["user_email"] = user_email
        session["user_name"] = user_name
        print(f"User logged in via Hugging Face: {user_email} - {user_name}")

        # Ensure session is saved before redirecting
        session.modified = True
        return redirect(url_for("menu"))  # Redirect to menu directly

    return render_template("redirect_page.html") 


from datetime import datetime

def generate_coupon_code(length=10):
    """Generates a random alphanumeric coupon code"""
    characters = string.ascii_uppercase + string.digits  # A-Z, 0-9
    return ''.join(random.choice(characters) for _ in range(length))
@app.route("/generate_custom_dish", methods=["POST"])
def generate_custom_dish():
    try:
        data = request.form
        dish_name = data.get("name")
        description = data.get("description")
        item_image_url = "https://huggingface.co/spaces/nagasurendra/BiryaniHubflask30/resolve/main/static/customized.jpg"
        item_image_url2 = "https://huggingface.co/spaces/nagasurendra/BiryaniHubflask30/resolve/main/static/customized1.jpg"

        if not dish_name or not description:
            return jsonify({"success": False, "error": "Both fields are required"}), 400

        # Generate a random price for the custom dish
        price = random.randint(10, 30)  # Example logic for price setting

        # Determine Veg/Non-Veg
        veg_keywords = ["paneer", "vegetable", "mushroom", "cheese"]
        non_veg_keywords = ["chicken", "mutton", "fish", "egg"]
        
        category = "Veg" if any(word in description.lower() for word in veg_keywords) else \
                   "Non veg" if any(word in description.lower() for word in non_veg_keywords) else \
                   "both"

        # Query to check if the dish already exists in Salesforce (Custom_Dish__c object)
        existing_dish_query = f"SELECT Id, Name, Price__c, Image1__c, Image2__c, Description__c, Veg_NonVeg__c FROM Custom_Dish__c WHERE Name = '{dish_name}'"
        existing_dish_result = sf.query(existing_dish_query)

        if existing_dish_result['totalSize'] > 0:
            # If the dish exists, use the existing details
            existing_dish = existing_dish_result['records'][0]
            price = existing_dish['Price__c']
            item_image_url = existing_dish['Image1__c']
            item_image_url2 = existing_dish['Image2__c']
            category = existing_dish['Veg_NonVeg__c']
        else:
            # If the dish does not exist, create a new custom dish
            custom_dish = {
                'Name': dish_name,
                'Price__c': price,
                'Image1__c': item_image_url,
                'Image2__c': item_image_url2,
                'Description__c': description,
                'Veg_NonVeg__c': category,
                'Section__c': 'Customized dish',
                'Total_Ordered__c': 0
            }

            # Insert the custom dish into Salesforce (Custom_Dish__c object)
            result = sf.Custom_Dish__c.create(custom_dish)

            if not result.get('success'):
                return jsonify({"success": False, "error": "Failed to create custom dish in Salesforce"}), 500

        # After ensuring the dish exists, check if it's already in the Cart_Item__c
        email = session.get('user_email')  # Assuming you have the user's email in session
        
        # Query to check if the custom dish already exists in the cart for the logged-in user
        cart_item_query = f"SELECT Id, Quantity__c, Price__c, Base_Price__c FROM Cart_Item__c WHERE Customer_Email__c = '{email}' AND Name = '{dish_name}'"
        cart_item_result = sf.query(cart_item_query)

        if cart_item_result['totalSize'] > 0:
            # If the custom dish is already in the cart, update the quantity and price
            cart_item = cart_item_result['records'][0]
            new_quantity = cart_item['Quantity__c'] + 1  # Increase quantity by 1
            new_price = price * new_quantity  # Update price based on new quantity
            
            # Update the cart item in Salesforce
            updated_cart_item = {
                'Quantity__c': new_quantity,
                'Price__c': new_price
            }
            
            cart_item_update = sf.Cart_Item__c.update(cart_item['Id'], updated_cart_item)

        else:
            # If the custom dish is not in the cart, create a new cart item
            cart_item = {
                'Name': dish_name,
                'Price__c': price,
                'Base_Price__c': price,
                'Image1__c': item_image_url,
                'Quantity__c': 1,  # Default quantity is 1
                'Add_Ons__c': '',  # Set Add_ons__c to empty
                'Add_Ons_Price__c': 0,  # Set Add_ons_Price__c to 0
                'Customer_Email__c': email  # Associate the custom dish with the logged-in user
            }

            # Insert the custom dish as a Cart_Item__c record in Salesforce
            cart_result = sf.Cart_Item__c.create(cart_item)

        # Redirect to the cart page after successfully adding or updating the cart item
        return redirect(url_for("cart"))

    except Exception as e:
        return jsonify({"success": False, "error": str(e)}), 500

                



import re
@app.route("/edit_profile", methods=["GET", "POST"])
def edit_profile():
    email = session.get('user_email')  # Get logged-in user's email
    if not email:
        return redirect(url_for("login"))

    try:
        # Fetch user details from Salesforce
        result = sf.query(f"""
            SELECT Id, Name, Email__c, Phone_Number__c, Password__c
            FROM Customer_Login__c
            WHERE Email__c = '{email}'
        """)

        if not result['records']:
            return redirect(url_for("login"))

        user = result['records'][0]
        user_id = user.get("Id")
        user_name = user.get("Name")
        user_phone = user.get("Phone_Number__c")
        user_email = user.get("Email__c")

    except Exception as e:
        print(f"Error fetching user data: {str(e)}")
        return jsonify({"success": False, "message": "Error fetching user data"})

    try:
        # Process user profile update
        new_name = request.form.get('name')
        new_email = request.form.get('email')
        new_phone = request.form.get('phone')
        new_password = request.form.get('password')

        update_data = {
            'Name': new_name,
            'Email__c': new_email,
            'Phone_Number__c': new_phone
        }

        if new_password:
            update_data['Password__c'] = new_password

        # Update Salesforce record
        sf.Customer_Login__c.update(user_id, update_data)

        return redirect(url_for('customer_details'))

    except Exception as e:
        return render_template("edit_profile.html", user_name=user_name, user_phone=user_phone, user_email=user_email, error=str(e))

import re
@app.route("/customer_details", methods=["GET"])
def customer_details():
    email = session.get('user_email')  # Get logged-in user's email

    if not email:
        return redirect(url_for("login"))  # If no email is found, redirect to login

    try:
        # Fetch customer details from Salesforce based on the email
        customer_record = sf.query(f"""
            SELECT Name, Email__c, Phone_Number__c, Referral__c, Reward_Points__c
            FROM Customer_Login__c
            WHERE Email__c = '{email}'
            LIMIT 1
        """)

        # If no customer record found, handle it
        if not customer_record.get("records"):
            return jsonify({"success": False, "message": "Customer not found in Salesforce"})

        # Get the customer details
        customer = customer_record["records"][0]

        # Prepare the data to return to the frontend
        customer_data = {
            "name": customer.get("Name", ""),
            "email": customer.get("Email__c", ""),
            "phone": customer.get("Phone_Number__c", ""),
            "referral_code": customer.get("Referral__c", ""),
            "reward_points": customer.get("Reward_Points__c", 0)
        }

        # Return the customer details as JSON response
        return render_template("customer_details.html", customer=customer_data)

    except Exception as e:
        print(f"Error fetching customer details: {str(e)}")
        return jsonify({"success": False, "message": f"Error fetching customer details: {str(e)}"})

@app.route("/order-history", methods=["GET"])
def order_history():
    email = session.get('user_email')  # Get logged-in user's email
    if not email:
        return redirect(url_for("login"))

    try:
        # Fetch past orders for the user
        result = sf.query(f"""
            SELECT Id, Customer_Name__c, Customer_Email__c, Total_Amount__c, 
                   Order_Details__c, Order_Status__c, Discount__c, Total_Bill__c, CreatedDate
            FROM Order__c
            WHERE Customer_Email__c = '{email}'
            ORDER BY CreatedDate DESC
        """)

        orders = result.get("records", [])  # Fetch all orders

        # Strip image URLs from order details and split remaining data by new lines
        for order in orders:
            order_details = order.get("Order_Details__c", "")
            # Remove image URLs using regex
            cleaned_details = re.sub(r'http[s]?://\S+', '', order_details)
            
            # Now split the cleaned details by lines and join them with <br> to create line breaks
            cleaned_details = cleaned_details.replace("\n", "  ")

            # Update the order details with the cleaned and formatted details
            order['Order_Details__c'] = cleaned_details

        return render_template("order_history.html", orders=orders)

    except Exception as e:
        print(f"Error fetching order history: {str(e)}")
        return render_template("order_history.html", orders=[], error=str(e))
app.permanent_session_lifetime = timedelta(minutes=5)
@app.before_request
def check_session_timeout():
    if "last_activity" in session:
        last_activity_time = session["last_activity"]
        now = datetime.now().timestamp()
        
        # Check if inactivity time has exceeded 5 minutes (300 seconds)
        if now - last_activity_time > 300:
            session.clear()  # Clear session
            return redirect(url_for("logout"))
    
    # Update last activity timestamp on every request
    session["last_activity"] = datetime.now().timestamp()

@app.route("/dashboard")
def dashboard():
    return render_template("dashboard.html") 
@app.route("/logout")
def logout():
    # Clear session variables
    session.pop('name', None)
    session.pop('email', None)
    session.pop('rewardPoints', None)
    session.pop('coupon', None)
    
    # Render HTML that will handle the redirect via JavaScript
    return render_template("redirect_page.html")

@app.route("/signup", methods=["GET", "POST"])
def signup():
    if request.method == "POST":
        name = request.form.get("name")
        phone = request.form.get("phone")
        email = request.form.get("email").strip()  # Trim spaces
        password = request.form.get("password")
        referral_code = request.form.get("referral")  # Fetch referral code from the form
        generated_referral_code = generate_referral_code()

        try:
            ref = 0  # Default reward points for new user

            # **Fix: Fetch all emails and compare in Python (Case-Insensitive)**
            email_query = "SELECT Id, Email__c FROM Customer_Login__c"
            email_result = sf.query(email_query)

            # Convert all stored emails to lowercase and compare with user input
            existing_emails = {record["Email__c"].lower() for record in email_result["records"]}
            if email.lower() in existing_emails:
                return render_template("signup.html", error="Email already in use! Please use a different email.")

            # Check if a referral code is entered
            if referral_code:
                referral_query = f"SELECT Id, Email__c, Name FROM Customer_Login__c WHERE Referral__c = '{referral_code}'"
                referral_result = sf.query(referral_query)

                if not referral_result['records']:
                    return render_template("signup.html", error="Invalid referral code!")

                # Get referrer's details
                referrer = referral_result['records'][0]
                referrer_email = referrer.get('Email__c')
                referrer_name = referrer.get('Name')

                # Generate a new unique coupon code
                new_coupon_code = generate_coupon_code()

                # Check if referrer already has a record in Referral_Coupon__c
                existing_coupon_query = f"SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{referrer_email}'"
                existing_coupon_result = sf.query(existing_coupon_query)

                if existing_coupon_result['records']:
                    referral_record = existing_coupon_result['records'][0]
                    referral_id = referral_record['Id']
                    existing_coupons = referral_record.get('Coupon_Code__c', '')

                    updated_coupons = f"{existing_coupons}\n{new_coupon_code}".strip()

                    # Update the existing record with the new coupon
                    sf.Referral_Coupon__c.update(referral_id, {
                        "Coupon_Code__c": updated_coupons
                    })
                else:
                    # If no record exists, create a new one
                    sf.Referral_Coupon__c.create({
                        "Name": referrer_name,
                        "Referral_Email__c": referrer_email,
                        "Coupon_Code__c": new_coupon_code
                    })

            # **Fix: Ensure Salesforce enforces unique email constraint**
            sf.Customer_Login__c.create({
                "Name": name,
                "Phone_Number__c": phone,
                "Email__c": email,
                "Password__c": password,
                "Reward_Points__c": ref,  # No points added, only coupon is created
                "Referral__c": generated_referral_code
            })

            return redirect(url_for("login"))

        except Exception as e:
            return render_template("signup.html", error=f"Error: {str(e)}")

    return render_template("signup.html")




@app.route("/login", methods=["GET", "POST"])
def login():
    if request.method == "POST":
        email = request.form.get("email")
        password = request.form.get("password")
        print(f"Login attempt with email: {email}")  # Debug log

        try:
            # Fetch user details from Salesforce
            query = f"SELECT Id, Name, Email__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c='{email}' AND Password__c='{password}'"
            result = sf.query(query)

            if result["records"]:
                user = result["records"][0]
                session['user_id'] = user['Id']

                # ✅ Always store or update session email
                if 'user_email' not in session or session['user_email'] != email:
                    session['user_email'] = email
                    session['user_name'] = user.get("Name", "")
                    print(f"✅ Session email updated: {session['user_email']}")

                reward_points = user.get("Reward_Points__c") or 0

                # Coupon generation logic (if reward points >= 500)
                if reward_points >= 500:
                    new_coupon_code = generate_coupon_code()
                    coupon_query = sf.query(f"SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'")

                    if coupon_query["records"]:
                        coupon_record = coupon_query["records"][0]
                        referral_coupon_id = coupon_record["Id"]
                        existing_coupons = coupon_record.get("Coupon_Code__c", "")

                        updated_coupons = f"{existing_coupons}\n{new_coupon_code}".strip()
                        sf.Referral_Coupon__c.update(referral_coupon_id, {"Coupon_Code__c": updated_coupons})
                    else:
                        sf.Referral_Coupon__c.create({
                            "Referral_Email__c": email,
                            "Name": user.get("Name", ""),
                            "Coupon_Code__c": new_coupon_code
                        })

                    new_reward_points = reward_points - 500
                    sf.Customer_Login__c.update(user['Id'], {"Reward_Points__c": new_reward_points})

                return redirect(url_for("menu"))

            else:
                print("Invalid credentials!")
                return render_template("login.html", error="Invalid credentials!")

        except Exception as e:
            print(f"Error during login: {str(e)}")
            return render_template("login.html", error=f"Error: {str(e)}")

    return render_template("login.html")

@app.route("/menu", methods=["GET", "POST"])
def menu():
    selected_category = request.args.get("category", "All")
    user_email = session.get('user_email')

    if not user_email:
        user_email = request.args.get("email")
        user_name = request.args.get("name")

        if user_email:
            session['user_email'] = user_email
            session['user_name'] = user_name  # Store name in session
        else:
            return redirect(url_for("login"))
    else:
        user_name = session.get('user_name')  # Get name from session if it's already stored

    # Get the first letter of the user's name (make it uppercase for consistency)
    first_letter = user_name[0].upper() if user_name else "A"

    try:
        # Fetch user referral and reward points
        user_query = f"SELECT Referral__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{user_email}'"
        user_result = sf.query(user_query)

        if not user_result['records']:
            return redirect(url_for('login'))

        referral_code = user_result['records'][0].get('Referral__c', 'N/A')
        reward_points = user_result['records'][0].get('Reward_Points__c', 0)

        # Query to fetch Menu_Item__c records including Total_Ordered__c for best sellers
        menu_query = """
            SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c, Total_Ordered__c 
            FROM Menu_Item__c
        """
        result = sf.query(menu_query)
        food_items = result['records'] if 'records' in result else []

        # Ensure Total_Ordered__c has a valid value
        for item in food_items:
            if 'Total_Ordered__c' not in item or item['Total_Ordered__c'] is None:
                item['Total_Ordered__c'] = 0  # Default value

        # Query to fetch Custom_Dish__c records created within the last 7 days with Total_Ordered__c > 10
        custom_dish_query = """
            SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c, Total_Ordered__c 
            FROM Custom_Dish__c
            WHERE CreatedDate >= LAST_N_DAYS:1
        """
        custom_dish_result = sf.query(custom_dish_query)
        custom_dishes = custom_dish_result['records'] if 'records' in custom_dish_result else []

        # Merge both Menu_Item__c and Custom_Dish__c records into the ordered menu
        all_items = food_items + custom_dishes

        # Define the order of sections, adding "Best Sellers" at the top
        section_order = ["Best Sellers", "Starters", "Biryanis", "Curries", "Breads", "Customized dish", "Apetizer", "Desserts", "Soft Drinks"]
        ordered_menu = {section: [] for section in section_order}

        # Sort items by Total_Ordered__c in descending order and pick top 4 as best sellers
        best_sellers = sorted(all_items, key=lambda x: x.get("Total_Ordered__c", 0), reverse=True)

        if selected_category == "Veg":
            best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Veg", "both"]]
        elif selected_category == "Non veg":
            best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Non veg", "both"]]

        # Take only the top 4 best sellers after filtering
        best_sellers = best_sellers[:4]

        # Ensure "Best Sellers" is added only if there are items after filtering
        if best_sellers:
            ordered_menu["Best Sellers"] = best_sellers

        # Create a set to track item names already added to prevent duplicates
        added_item_names = set()

        # Filter and organize menu items based on category and section (to avoid duplicates)
        for item in all_items:
            section = item.get("Section__c", "Others")  # Default to "Others" if missing
            if section not in ordered_menu:
                ordered_menu[section] = []

            # Skip item if it's already been added to avoid duplicates
            if item['Name'] in added_item_names:
                continue

            # Apply category filters
            if selected_category == "Veg" and item.get("Veg_NonVeg__c") not in ["Veg", "both"]:
                continue
            if selected_category == "Non veg" and item.get("Veg_NonVeg__c") not in ["Non veg", "both"]:
                continue

            ordered_menu[section].append(item)
            added_item_names.add(item['Name'])  # Add item to the set of added items
            print(f"Added item to {section}: {item['Name']}")  # Debugging

        # Remove empty sections
        ordered_menu = {section: items for section, items in ordered_menu.items() if items}
        print(f"Final ordered menu: {ordered_menu.keys()}")  # Debugging

        categories = ["All", "Veg", "Non veg"]

    except Exception as e:
        print(f"Error fetching menu data: {str(e)}")
        ordered_menu = {}
        categories = ["All", "Veg", "Non veg"]
        referral_code = 'N/A'
        reward_points = 0

    # Pass the user's first letter (first_letter) to the template
    return render_template(
        "menu.html",
        ordered_menu=ordered_menu,
        categories=categories,
        selected_category=selected_category,
        referral_code=referral_code,
        reward_points=reward_points,
        user_name=user_name,  # Pass name to the template
        first_letter=first_letter  # Pass first letter to the template
    )


@app.route("/cart", methods=["GET"])
def cart():
    email = session.get('user_email')
    if not email:
        return redirect(url_for("login"))

    try:
        # Fetch cart items with Category and Section
        result = sf.query(f"""
            SELECT Name, Price__c, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Image1__c, Instructions__c, Category__c, Section__c
            FROM Cart_Item__c
            WHERE Customer_Email__c = '{email}'
        """)
        cart_items = result.get("records", [])

        subtotal = sum(item['Price__c'] for item in cart_items)

        # Fetch reward points
        customer_result = sf.query(f"""
            SELECT Reward_Points__c 
            FROM Customer_Login__c
            WHERE Email__c = '{email}'
        """)
        reward_points = customer_result['records'][0].get('Reward_Points__c', 0) if customer_result['records'] else 0

        # Fetch coupons for the user
        coupon_result = sf.query(f"""
            SELECT Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'
        """)
        if coupon_result["records"]:
            raw_coupons = coupon_result["records"][0].get("Coupon_Code__c", "")
            coupons = raw_coupons.split("\n") if raw_coupons else []
        else:
            coupons = []

        # Initialize suggestions as an empty list
        suggestions = []

        # If there are items in the cart, fetch suggestions
        if cart_items:
            # Get the category and section of the first item in the cart (You can choose which item you want to base suggestions on)
            first_item = cart_items[0]
            item_category = first_item.get('Category__c', 'All')  # Default to 'All' if not found
            item_section = first_item.get('Section__c', 'Biryanis')  # Default to 'Biryanis' if not found

            # Define section-to-complementary section mapping
            complementary_sections = {
                'Breads': ['Curries', 'Biryanis', 'Starters'],
                'Biryanis': ['Curries', 'Starters', 'Desserts'],
                'Curries': ['Rice', 'Breads', 'Starters'],
                'Starters': ['Biryanis', 'Curries', 'Desserts'],
                'Desserts': ['Biryanis', 'Curries', 'Soft Drinks'],
                'Soft Drinks': ['Starters', 'Biryanis', 'Curries']
            }

            # Get the complementary sections for the selected section
            suggested_sections = complementary_sections.get(item_section, [])

            # Fetch suggestions from the complementary sections
            try:
                for suggested_section in suggested_sections:
                    if item_category == "All":
                        query = f"""
                            SELECT Name, Price__c, Image1__c
                            FROM Menu_Item__c
                            WHERE Section__c = '{suggested_section}' 
                            AND (Veg_NonVeg__c = 'Veg' OR Veg_NonVeg__c = 'Non veg')
                            LIMIT 4
                        """
                    else:
                        query = f"""
                            SELECT Name, Price__c, Image1__c
                            FROM Menu_Item__c
                            WHERE Section__c = '{suggested_section}' 
                            AND Veg_NonVeg__c = '{item_category}'
                            LIMIT 4
                        """
                    suggestion_result = sf.query(query)
                    suggestions.extend(suggestion_result.get("records", []))  # Add suggestions from each section

                # Limit the number of suggestions to 4
                if len(suggestions) > 4:
                    suggestions = suggestions[:4]

            except Exception as e:
                print(f"Error fetching suggestions: {e}")

        return render_template(
            "cart.html",
            cart_items=cart_items,
            subtotal=subtotal,
            reward_points=reward_points,
            customer_email=email,
            coupons=coupons,
            suggestions=suggestions
        )

    except Exception as e:
        print(f"Error fetching cart items: {e}")
        return render_template("cart.html", cart_items=[], subtotal=0, reward_points=0, coupons=[], suggestions=[])


@app.route("/cart/add_suggestion_to_cart", methods=["POST"])
def add_suggestion_to_cart():
    try:
        # Get data from the request
        data = request.get_json()
        item_name = data.get('item_name').strip()
        item_price = data.get('item_price')
        item_image = data.get('item_image')
        item_id = data.get('item_id')
        customer_email = data.get('customer_email')
        addons = data.get('addons', [])
        instructions = data.get('instructions', "")

        # Default values if addons and instructions are not provided
        addons_price = 0
        addons_string = "None"

        # Check if the customer already has this item in their cart
        query = f"""
            SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c 
            FROM Cart_Item__c
            WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
        """
        result = sf.query(query)
        cart_items = result.get("records", [])

        # If item already exists in the cart, update its quantity and other details
        if cart_items:
            cart_item_id = cart_items[0]['Id']
            existing_quantity = cart_items[0]['Quantity__c']
            existing_addons = cart_items[0].get('Add_Ons__c', "None")
            existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
            existing_instructions = cart_items[0].get('Instructions__c', "")

            # Combine existing and new addons
            combined_addons = existing_addons if existing_addons != "None" else ""
            if addons:
                combined_addons = f"{combined_addons}; {addons}".strip("; ")

            combined_instructions = existing_instructions
            if instructions:
                combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")

            combined_addons_list = combined_addons.split("; ")
            combined_addons_price = sum(
                float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
            )

            # Update the cart item
            sf.Cart_Item__c.update(cart_item_id, {
                "Quantity__c": existing_quantity + 1,
                "Add_Ons__c": combined_addons,
                "Add_Ons_Price__c": combined_addons_price,
                "Instructions__c": combined_instructions,
                "Price__c": (existing_quantity + 1) * float(item_price) + combined_addons_price
            })
        else:
            # If item doesn't exist in cart, create a new cart item
            total_price = float(item_price) + addons_price

            # Create a new cart item in Salesforce
            sf.Cart_Item__c.create({
                "Name": item_name,
                "Price__c": total_price,
                "Base_Price__c": item_price,
                "Quantity__c": 1,
                "Add_Ons_Price__c": addons_price,
                "Add_Ons__c": addons_string,
                "Image1__c": item_image,
                "Customer_Email__c": customer_email,
                "Instructions__c": instructions
            })

        return jsonify({"success": True, "message": "Item added to cart successfully."})

    except Exception as e:
        print(f"Error adding item to cart: {str(e)}")
        return jsonify({"success": False, "error": str(e)})

@app.route('/cart/add', methods=['POST'])
def add_to_cart():
    data = request.json
    item_name = data.get('itemName').strip()
    item_price = data.get('itemPrice')
    item_image = data.get('itemImage')
    addons = data.get('addons', [])
    instructions = data.get('instructions', '')
    category = data.get('category')
    section = data.get('section')
    customer_email = session.get('user_email')

    if not item_name or not item_price:
        return jsonify({"success": False, "error": "Item name and price are required."})

    try:
        query = f"""
            SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c FROM Cart_Item__c
            WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
        """
        result = sf.query(query)
        cart_items = result.get("records", [])

        addons_price = sum(addon['price'] for addon in addons)
        new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])

        if cart_items:
            cart_item_id = cart_items[0]['Id']
            existing_quantity = cart_items[0]['Quantity__c']
            existing_addons = cart_items[0].get('Add_Ons__c', "None")
            existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
            existing_instructions = cart_items[0].get('Instructions__c', "")

            combined_addons = existing_addons if existing_addons != "None" else ""
            if new_addons:
                combined_addons = f"{combined_addons}; {new_addons}".strip("; ")

            combined_instructions = existing_instructions
            if instructions:
                combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")

            combined_addons_list = combined_addons.split("; ")
            combined_addons_price = sum(
                float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
            )

            sf.Cart_Item__c.update(cart_item_id, {
                "Quantity__c": existing_quantity + 1,
                "Add_Ons__c": combined_addons,
                "Add_Ons_Price__c": combined_addons_price,
                "Instructions__c": combined_instructions,
                "Price__c": (existing_quantity + 1) * item_price + combined_addons_price,
                "Category__c": category,
                "Section__c": section
            })
        else:
            addons_string = "None"
            if addons:
                addons_string = new_addons

            total_price = item_price + addons_price

            sf.Cart_Item__c.create({
                "Name": item_name,
                "Price__c": total_price,
                "Base_Price__c": item_price,
                "Quantity__c": 1,
                "Add_Ons_Price__c": addons_price,
                "Add_Ons__c": addons_string,
                "Image1__c": item_image,
                "Customer_Email__c": customer_email,
                "Instructions__c": instructions,
                "Category__c": category,
                "Section__c": section
            })

        return jsonify({"success": True, "message": "Item added to cart successfully."})
    except Exception as e:
        print(f"Error adding item to cart: {str(e)}")
        return jsonify({"success": False, "error": str(e)})



@app.route("/cart/add_item", methods=["POST"])
def add_item_to_cart():
    data = request.json  # Extract JSON data from the request
    email = data.get('email')  # Customer email
    item_name = data.get('item_name')  # Item name
    quantity = data.get('quantity', 1)  # Quantity to add (default is 1)
    addons = data.get('addons', [])  # Add-ons for the item (optional)

    # Validate inputs
    if not email or not item_name:
        return jsonify({"success": False, "error": "Email and item name are required."}), 400

    try:
        # Add a new item to the cart with the provided details
        sf.Cart_Item__c.create({
            "Customer_Email__c": email,  # Associate the cart item with the customer's email
            "Item_Name__c": item_name,  # Item name
            "Quantity__c": quantity,  # Quantity to add
            "Add_Ons__c": addons_string
        })

        return jsonify({"success": True, "message": "Item added to cart successfully."})
    except Exception as e:
        print(f"Error adding item to cart: {str(e)}")  # Log the error for debugging
        return jsonify({"success": False, "error": str(e)}), 500



@app.route('/cart/remove/<item_name>', methods=['POST'])
def remove_cart_item(item_name):
    try:
        customer_email = session.get('user_email')
        if not customer_email:
            return jsonify({'success': False, 'message': 'User email not found. Please log in again.'}), 400
        query = f"""
            SELECT Id FROM Cart_Item__c 
            WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
        """
        result = sf.query(query)
        if result['totalSize'] == 0:
            return jsonify({'success': False, 'message': 'Item not found in cart.'}), 400
        cart_item_id = result['records'][0]['Id']
        sf.Cart_Item__c.delete(cart_item_id)
        return jsonify({'success': True, 'message': f"'{item_name}' removed successfully!"}), 200
    except Exception as e:
        print(f"Error: {str(e)}")
        return jsonify({'success': False, 'message': f"An error occurred: {str(e)}"}), 500

@app.route('/api/addons', methods=['GET'])
def get_addons():
    item_name = request.args.get('item_name')  
    item_section = request.args.get('item_section')  

    if not item_name or not item_section:
        return jsonify({"success": False, "error": "Item name and section are required."})

    try:
        # Fetch customization options from Salesforce based on the section
        query = f"""
            SELECT Name, Customization_Type__c, Options__c, Max_Selections__c, Extra_Charge__c, Extra_Charge_Amount__c
            FROM Customization_Options__c
            WHERE Section__c = '{item_section}'
        """
        result = sf.query(query)
        addons = result.get('records', [])

        # Format data for frontend
        formatted_addons = []
        for addon in addons:
            options = addon.get("Options__c", "").split(", ")  # Convert comma-separated options into a list
            formatted_addons.append({
                "name": addon["Name"],
                "type": addon["Customization_Type__c"],
                "options": options,
                "max_selections": addon.get("Max_Selections__c", 1),
                "extra_charge": addon.get("Extra_Charge__c", False),
                "extra_charge_amount": addon.get("Extra_Charge_Amount__c", 0)
            })

        return jsonify({"success": True, "addons": formatted_addons})

    except Exception as e:
        print(f"Error fetching add-ons: {e}")
        return jsonify({"success": False, "error": "Unable to fetch add-ons. Please try again later."})

@app.route("/cart/update_quantity", methods=["POST"])
def update_quantity():
    data = request.json  # Extract JSON data from the request
    email = data.get('email')
    item_name = data.get('item_name')
    try:
        # Convert quantity to an integer
        quantity = int(data.get('quantity'))
    except (ValueError, TypeError):
        return jsonify({"success": False, "error": "Invalid quantity provided."}), 400

    # Validate inputs
    if not email or not item_name or quantity is None:
        return jsonify({"success": False, "error": "Email, item name, and quantity are required."}), 400

    try:
        # Query the cart item in Salesforce
        cart_items = sf.query(
            f"SELECT Id, Quantity__c, Price__c, Base_Price__c, Add_Ons_Price__c FROM Cart_Item__c "
            f"WHERE Customer_Email__c = '{email}' AND Name = '{item_name}'"
        )['records']

        if not cart_items:
            return jsonify({"success": False, "error": "Cart item not found."}), 404

        # Retrieve the first matching record
        cart_item_id = cart_items[0]['Id']
        base_price = cart_items[0]['Base_Price__c']
        addons_price = cart_items[0].get('Add_Ons_Price__c', 0)

        # Calculate the new item price
        new_item_price = (base_price * quantity) + addons_price

        # Update the record in Salesforce
        sf.Cart_Item__c.update(cart_item_id, {
            "Quantity__c": quantity,
            "Price__c": new_item_price,  # Update base price
        })

        # Recalculate the subtotal for all items in the cart
        cart_items = sf.query(f"""
            SELECT Price__c, Add_Ons_Price__c 
            FROM Cart_Item__c 
            WHERE Customer_Email__c = '{email}'
        """)['records']
        new_subtotal = sum(item['Price__c'] for item in cart_items) 

        # Return updated item price and subtotal
        return jsonify({"success": True, "new_item_price": new_item_price, "subtotal": new_subtotal})
        print(f"New item price: {new_item_price}, New subtotal: {new_subtotal}")
        return jsonify({"success": True, "new_item_price": new_item_price, "subtotal": new_subtotal})

    except Exception as e:
        print(f"Error updating quantity: {str(e)}")
        return jsonify({"success": False, "error": str(e)}), 500
@app.route("/checkout", methods=["POST"])
def checkout():
    email = session.get('user_email')
    user_id = session.get('user_name')

    if not email or not user_id:
        return jsonify({"success": False, "message": "User not logged in"})

    try:
        data = request.json
        selected_coupon = data.get("selectedCoupon", "").strip()

        # Fetch cart items
        result = sf.query(f"""
            SELECT Id, Name, Price__c, Add_Ons_Price__c, Quantity__c, Add_Ons__c, Instructions__c, Image1__c
            FROM Cart_Item__c
            WHERE Customer_Email__c = '{email}'
        """)
        cart_items = result.get("records", [])

        if not cart_items:
            return jsonify({"success": False, "message": "Cart is empty"})

        total_price = sum(item['Price__c'] for item in cart_items)
        discount = 0
        # Fetch the user's existing coupons
        coupon_query = sf.query(f"""
            SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'
        """)

        has_coupons = bool(coupon_query["records"])  # Check if user has any coupons

        if selected_coupon:
            # Case 3: User selected a valid coupon → Apply discount & remove coupon
            discount = total_price * 0.10  # 10% discount
            referral_coupon_id = coupon_query["records"][0]["Id"]
            existing_coupons = coupon_query["records"][0]["Coupon_Code__c"].split("\n")

            # Remove only the selected coupon
            updated_coupons = [coupon for coupon in existing_coupons if coupon.strip() != selected_coupon]

            # Convert list back to a string with newlines
            updated_coupons_str = "\n".join(updated_coupons).strip()

            # Update the Referral_Coupon__c record with the remaining coupons
            sf.Referral_Coupon__c.update(referral_coupon_id, {
                "Coupon_Code__c": updated_coupons_str
            })
        else:
            # Case 1 & Case 2: User has no coupons or has coupons but didn’t select one → Add 10% to reward points
            reward_points_to_add = total_price * 0.10

            # Fetch current reward points
            customer_record = sf.query(f"""
                SELECT Id, Reward_Points__c FROM Customer_Login__c
                WHERE Email__c = '{email}'
            """)
            customer = customer_record.get("records", [])[0] if customer_record else None

            if customer:
                current_reward_points = customer.get("Reward_Points__c") or 0
                new_reward_points = current_reward_points + reward_points_to_add

                print(f"Updating reward points: Current = {current_reward_points}, Adding = {reward_points_to_add}, New = {new_reward_points}")

                # Update reward points in Salesforce
                sf.Customer_Login__c.update(customer["Id"], {
                    "Reward_Points__c": new_reward_points
                })
                print(f"Successfully updated reward points for {email}")

        total_bill = total_price - discount

        # ✅ Store **all details** including Add-Ons, Instructions, Price, and Image
        order_details = "\n".join(
            f"{item['Name']} x{item['Quantity__c']} | Add-Ons: {item.get('Add_Ons__c', 'None')} | "
            f"Instructions: {item.get('Instructions__c', 'None')} | "
            f"Price: ${item['Price__c']} | Image: {item['Image1__c']}"
            for item in cart_items
        )

        # Fetch Customer ID from Customer_Login__c based on email
        customer_query = sf.query(f"""
            SELECT Id FROM Customer_Login__c
            WHERE Email__c = '{email}'
        """)
        
        # Assuming the customer exists
        customer_id = customer_query["records"][0]["Id"] if customer_query["records"] else None
        
        if not customer_id:
            return jsonify({"success": False, "message": "Customer record not found in Salesforce"})

        # Store the order details in Order__c
        order_data = {
            "Customer_Name__c": user_id,
            "Customer_Email__c": email,
            "Total_Amount__c": total_price,
            "Discount__c": discount,
            "Total_Bill__c": total_bill,
            "Order_Status__c": "Pending",
            "Customer2__c": customer_id,  # Correcting to use the Salesforce Customer record ID
            "Order_Details__c": order_details  # ✅ Now includes **all details**
        }

        sf.Order__c.create(order_data)

        # ✅ Delete cart items after order is placed
        for item in cart_items:
            sf.Cart_Item__c.delete(item["Id"])

        return jsonify({"success": True, "message": "Order placed successfully!"})

    except Exception as e:
        print(f"Error during checkout: {str(e)}")
        return jsonify({"success": False, "error": str(e)})
@app.route("/order", methods=["GET"])
def order_summary():
    email = session.get('user_email')  # Fetch logged-in user's email
    if not email:
        return redirect(url_for("login"))

    try:
        # Fetch the most recent order for the user
        result = sf.query(f"""
            SELECT Id, Customer_Name__c, Customer_Email__c, Total_Amount__c, Order_Details__c, Order_Status__c, Discount__c, Total_Bill__c
            FROM Order__c
            WHERE Customer_Email__c = '{email}'
            ORDER BY CreatedDate DESC
            LIMIT 1
        """)
        order = result.get("records", [])[0] if result.get("records") else None

        if not order:
            return render_template("order.html", order=None)

        return render_template("order.html", order=order)
    except Exception as e:
        print(f"Error fetching order details: {str(e)}")
        return render_template("order.html", order=None, error=str(e))

if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port=7860)