File size: 87,298 Bytes
246e086
 
 
 
5859516
cad9b32
 
3ebd164
aa1770c
f1a3c28
a62dad1
d10dfb9
 
 
a62dad1
10c9dcf
88892b2
d10dfb9
10c9dcf
88892b2
 
d10dfb9
10c9dcf
88892b2
d10dfb9
10c9dcf
a62dad1
aa1770c
c6007f0
 
 
 
246e086
bb477f6
3a4187b
62e89ef
 
6ed6c8e
 
246e086
 
 
bb477f6
 
 
 
 
 
246e086
3a4187b
 
 
 
 
246e086
bb477f6
 
 
246e086
cad9b32
 
 
 
 
70f578f
bb477f6
70f578f
5e51b23
70f578f
 
5e51b23
 
70f578f
 
 
 
5e51b23
 
 
 
 
 
 
 
 
 
 
 
bb477f6
5e51b23
 
 
 
 
 
 
 
 
70f578f
 
5e51b23
bb477f6
 
 
 
 
 
 
 
 
 
 
 
6ed6c8e
 
 
bb477f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70f578f
82f010d
246e086
64088ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb477f6
 
246e086
bb477f6
 
 
 
 
 
 
246e086
 
 
82f010d
 
246e086
 
9cea188
64088ff
 
9cea188
64088ff
 
 
 
 
 
 
 
 
 
 
 
9cea188
 
 
64088ff
 
 
 
 
 
 
 
9cea188
64088ff
 
 
 
 
 
 
 
 
 
 
 
 
 
9cea188
bb477f6
82f010d
 
 
 
 
 
 
 
 
bb477f6
246e086
 
 
82f010d
 
 
 
70f578f
82f010d
 
 
 
 
5859516
d40911b
0ec609a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d40911b
3a4187b
 
 
 
 
 
 
 
 
 
 
 
 
d10dfb9
 
5859516
3a4187b
 
 
 
 
9cea188
3a4187b
 
 
 
 
 
 
 
 
 
6ed6c8e
 
3a4187b
bb477f6
bcb6b1a
6ed6c8e
 
 
 
 
 
bb477f6
6ed6c8e
 
 
d10dfb9
6ed6c8e
5e51b23
6ed6c8e
 
 
d10dfb9
6ed6c8e
 
 
 
d10dfb9
 
 
 
 
6ed6c8e
d10dfb9
6ed6c8e
d10dfb9
6ed6c8e
d10dfb9
6ed6c8e
d10dfb9
6ed6c8e
d10dfb9
6ed6c8e
 
d10dfb9
3a4187b
bee5ce2
bb477f6
c6007f0
 
bee5ce2
 
 
 
 
bb477f6
246e086
10c9dcf
 
 
 
 
 
 
 
 
 
6ed6c8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a4187b
6ed6c8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10c9dcf
6ed6c8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10c9dcf
6ed6c8e
 
 
10c9dcf
6ed6c8e
 
10c9dcf
6ed6c8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a4187b
6ed6c8e
 
 
 
10c9dcf
6ed6c8e
 
246e086
6ed6c8e
 
246e086
6ed6c8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246e086
 
5859516
bee5ce2
5859516
bee5ce2
bcb6b1a
5859516
bee5ce2
5859516
bee5ce2
cad9b32
bee5ce2
64088ff
 
 
 
 
 
 
 
bee5ce2
 
d40911b
 
bee5ce2
 
 
 
 
 
 
 
d40911b
3a4187b
246e086
3a4187b
 
cad9b32
bee5ce2
d40911b
3a4187b
 
 
 
d40911b
bee5ce2
 
d40911b
89f8a67
 
bee5ce2
89f8a67
 
 
d40911b
 
bee5ce2
cad9b32
 
 
 
 
c8f5f95
d40911b
 
 
 
 
 
c8f5f95
bee5ce2
 
246e086
cad9b32
64088ff
 
bee5ce2
64088ff
 
 
 
 
 
 
 
 
246e086
5859516
6ed6c8e
 
 
 
bee5ce2
bcb6b1a
89f8a67
3a4187b
89f8a67
3a4187b
 
 
73d17ea
3a4187b
 
 
 
 
5859516
bee5ce2
bcb6b1a
5859516
73d17ea
 
 
 
 
 
160cdf5
d10dfb9
160cdf5
bee5ce2
160cdf5
bee5ce2
160cdf5
bee5ce2
160cdf5
246e086
6ed6c8e
d10dfb9
 
89f8a67
 
 
 
 
 
 
 
 
73d17ea
89f8a67
 
 
 
d10dfb9
 
 
 
5859516
d10dfb9
 
 
 
 
 
 
 
 
 
bcb6b1a
d10dfb9
bcb6b1a
 
 
 
 
 
 
 
 
 
 
 
d10dfb9
bcb6b1a
246e086
6ed6c8e
d10dfb9
 
 
 
246e086
3a4187b
 
 
 
 
 
2a0b695
3a4187b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d10dfb9
 
5e51b23
6ed6c8e
 
5e51b23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a4187b
a62dad1
5e51b23
a62dad1
 
 
6ed6c8e
a62dad1
 
 
 
 
10c9dcf
a62dad1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d10dfb9
a62dad1
 
5e51b23
a62dad1
 
5e51b23
 
 
a62dad1
 
5e51b23
58df022
5e51b23
a62dad1
5e51b23
a62dad1
 
5e51b23
 
 
 
 
 
a62dad1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ed6c8e
 
 
 
5e51b23
 
 
 
 
6ed6c8e
a62dad1
c6007f0
 
 
 
 
 
 
10c9dcf
c6007f0
 
 
 
 
6ed6c8e
c6007f0
 
 
 
 
 
 
 
ed8e109
 
bcb6b1a
6ed6c8e
d10dfb9
a62dad1
c6007f0
6ed6c8e
 
 
a62dad1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa1770c
bee5ce2
c6007f0
bee5ce2
 
3a4187b
 
5e51b23
bee5ce2
 
 
 
 
 
10c9dcf
 
 
 
 
 
 
 
 
 
 
 
 
 
6ed6c8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a086cf
6ed6c8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10c9dcf
6ed6c8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10c9dcf
6ed6c8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10c9dcf
6ed6c8e
 
 
10c9dcf
6ed6c8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10c9dcf
6ed6c8e
 
10c9dcf
6ed6c8e
 
 
 
 
10c9dcf
3a4187b
6ed6c8e
3a4187b
 
6ed6c8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10c9dcf
6ed6c8e
 
 
bee5ce2
 
6ed6c8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bee5ce2
89f8a67
3a4187b
 
 
d10dfb9
3a4187b
bcb6b1a
89f8a67
 
 
 
 
 
 
 
 
73d17ea
89f8a67
 
 
 
3a4187b
bcb6b1a
 
 
 
5e51b23
bcb6b1a
5e51b23
 
 
 
bcb6b1a
5e51b23
bcb6b1a
5e51b23
bcb6b1a
5e51b23
bcb6b1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bee5ce2
3a4187b
2a0b695
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d10dfb9
 
5e51b23
6ed6c8e
 
5e51b23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a0b695
bee5ce2
a62dad1
 
 
 
6ed6c8e
a62dad1
 
 
 
 
10c9dcf
a62dad1
 
 
 
 
5e51b23
a62dad1
 
 
 
 
 
 
 
 
d10dfb9
a62dad1
bee5ce2
5e51b23
 
a62dad1
 
5859516
5e51b23
 
 
246e086
5e51b23
58df022
5e51b23
bee5ce2
5e51b23
a62dad1
d10dfb9
 
5e51b23
 
 
 
 
d10dfb9
a62dad1
aa1770c
a62dad1
 
 
aa1770c
a62dad1
 
c6007f0
 
 
 
 
a62dad1
c6007f0
 
 
 
a62dad1
c6007f0
 
 
 
 
 
a62dad1
 
c6007f0
a62dad1
 
 
 
 
 
5e51b23
d10dfb9
a62dad1
 
 
6ed6c8e
 
 
 
5e51b23
 
 
 
 
6ed6c8e
5e51b23
a62dad1
 
6ed6c8e
a62dad1
 
 
 
 
10c9dcf
a62dad1
 
 
 
 
5e51b23
a62dad1
 
 
 
 
 
 
 
ed8e109
 
bcb6b1a
6ed6c8e
5e51b23
a62dad1
5e51b23
6ed6c8e
 
 
a62dad1
 
 
 
 
 
 
 
 
 
 
 
 
c6007f0
a62dad1
c6007f0
d10dfb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88892b2
 
 
 
 
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
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
import streamlit as st
import pandas as pd
from datetime import datetime
import os
import configparser  # For activating the config
from PIL import Image, ImageFilter 
import io
import base64
from google.cloud import bigquery
from google.oauth2 import service_account
import logging
from threading import Thread
from pyngrok import conf, ngrok
import requests

# Manually set ngrok path
# ngrok_path = "/Users/ninadmandavkar/Desktop/ngrok/ngrok 3"

# Set the ngrok path in the configuration
# config = conf.get_default()
# config.ngrok_path = ngrok_path

# Set your ngrok auth token
# ngrok.set_auth_token('2jQ6LRMBFqe25TP7o2ugti9EH6Y_3mBzBYbsjZmhh69eGq2Q1')

# logging.basicConfig(level=logging.INFO)


## Single value - 268 --> 675
## Feature Specific - 676 --> 980



global_user_data = []  # For appending user_data
global_data_rows = [] # For appending data rows




# Function to initialize session state
def initialize_session_state():
    if 'data' not in st.session_state:
        st.session_state.data = []
    if 'alphabet_counter' not in st.session_state:
        st.session_state.alphabet_counter = 0
    if 'serial_counter' not in st.session_state:
        st.session_state.serial_counter = -1


      # Initialize global_data_rows if not already initialized
    if 'global_data_rows' not in st.session_state:
        st.session_state.global_data_rows = []


# Function to save user data
def save_user_data(user_data):
    st.session_state.data.append(user_data)


# Function to save Slab data
def save_slab_data(new_row):
    st.session_state.data.append(new_row)


# Function to generate PM_id
def generate_rule_id(user_data):
    company_id = user_data.get('Company ID', '')
    ordering_channels = user_data.get('Product lines', '')
    fulfilling_location = user_data.get('Fulfilling Location', '')
    application_id = user_data.get('Application ID', '')
    selected_fee_type = user_data.get('Fee Type', '')
    selected_variable_type = user_data.get('Variable Type', '')
    selected_chargeable_on = user_data.get('Chargeable on', '')
    selected_fee_nature = user_data.get('Fee Nature', '')
    threshold = user_data.get('Threshold option', '')

    # Ensure all fields are strings and handle None or empty values
    company_id = str(company_id).zfill(5)[:5] if company_id else '00000'
    ordering_channels = str(ordering_channels)[:2] if ordering_channels else '00'
    fulfilling_location = str(fulfilling_location)[:2] if fulfilling_location else '00'
    application_id = str(application_id)[:2] if application_id else '00'
    selected_fee_type = str(selected_fee_type)[:2] if selected_fee_type else '00'
    selected_variable_type = str(selected_variable_type)[:2] if selected_variable_type else '00'
    selected_chargeable_on = str(selected_chargeable_on)[:2] if selected_chargeable_on else '00'
    selected_fee_nature = str(selected_fee_nature)[:2] if selected_fee_nature else '00'
    threshold = str(threshold)[:2] if threshold else '00'

    # Extract first 2 characters from each field and ensure proper length
    rule_id_parts = [
        company_id,                 # Ensure 5 characters
        ordering_channels,          # Ensure 2 characters
        fulfilling_location,        # Ensure 2 characters
        application_id,             # Ensure 2 characters
        selected_fee_type,          # Ensure 2 characters
        selected_variable_type,     # Ensure 2 characters
        selected_chargeable_on,     # Ensure 2 characters
        selected_fee_nature,        # Ensure 2 characters
        threshold                   # Ensure 2 characters
    ]

    rule_id = '_'.join(rule_id_parts)  # Join parts with an underscore
    return rule_id


# Function to generate Plan ID
def generate_plan_id(user_data):
    initialize_session_state()  # Ensure session state is initialized

    alphabet_counter = st.session_state.alphabet_counter
    serial_counter = st.session_state.serial_counter

    company_id = user_data.get('Company ID', '')  # Get company_id from user_data

    if company_id is None or not company_id.strip():
        company_id = ''

    # Increment serial counter and reset if it reaches 999999
    serial_counter += 1
    if serial_counter > 999999:
        serial_counter = 0
        alphabet_counter += 1

    # Save updated counters back to session state
    st.session_state['alphabet_counter'] = alphabet_counter
    st.session_state['serial_counter'] = serial_counter

    # Calculate alphabet ('a' to 'z')
    current_alphabet = chr(alphabet_counter % 26 + ord('a'))

    # Format serial counter as six-digit number
    serial_number = str(serial_counter).zfill(6)

    # Get the first 5 characters of company_id and pad it with zeros
    company_id_part = company_id[:5].zfill(5)

    # Generate the plan_id
    plan_id = f"{company_id_part}_{current_alphabet}_{serial_number}"

    return plan_id


# Function to display saved records as a table and save to CSV
def display_saved_records():
    

    html_content_5 = """
        <h1 style='
            color: #a689f6;
            font-size: 25px;
            background-image: -webkit-linear-gradient(0deg, #a689f6 3%, #272191 33%, #413bb9 61%);
            background-clip: text;
            -webkit-background-clip: text;
            text-fill-color: transparent;
            -webkit-text-fill-color: transparent;
        '>Saved records</h1>
    """

    st.markdown(html_content_5, unsafe_allow_html=True)
    df = pd.DataFrame(st.session_state.data)

    if not df.empty:
        # Apply generate_rule_id function to each row to generate rule_id
        df['rule_id'] = df.apply(lambda row: generate_rule_id(row), axis=1)

        # Display DataFrame with increased height
        st.write(df.style.set_table_attributes('style="font-size: 14px; line-height: 18px; width: auto; height: auto;"'))

        # Save records to CSV
        folder_path = "data"  # Folder name
        os.makedirs(folder_path, exist_ok=True)  # Create folder if it doesn't exist
        file_path = os.path.join(folder_path, "records.csv")

        # Save DataFrame to CSV without index
        df.to_csv(file_path, index=False)

        # Custom CSS styles
        button_styles = """
        <style>
            .stDownloadButton button {
            color: #ffffff; /* Text color */
            font-size: 20px;
            background-image: linear-gradient(0deg, #a689f6 3%, #413bb9 61%);
            border: none;
            padding: 10px 20px;
            cursor: pointer;
            border-radius: 15px;
            display: inline-block;
            text-decoration: none;
            text-align: center;
            transition: background-color 0.3s ease;
        }
        .stDownloadButton button:hover {
            background-color: #00ff00; /* Hover background color */
            color: #ff0000; /* Hover text color */  
        }
        </style>
        """

# Render custom CSS styles
    st.markdown(button_styles, unsafe_allow_html=True)

# Define file path (replace with your actual file path)
    folder_path = "data"
    file_path = os.path.join(folder_path, "records.csv")

# Function to read file data
    def read_file(file_path):
        with open(file_path, 'rb') as f:
            file_data = f.read()
        return file_data

# Download button with custom styling
    st.download_button(
        label="Download CSV",
        data=read_file(file_path),
        file_name="records.csv",
        mime="text/csv",
    )

# Function to load theme configuration
def load_theme_config():
    config = configparser.ConfigParser()
    config.read('config.toml')

    # Extract theme settings
    theme_settings = config['theme']
    return theme_settings


# Initialize session state
initialize_session_state()

theme_settings = load_theme_config()

# Apply theme settings to Streamlit
st.set_page_config(
    page_title="Plan maker",
    page_icon="Fynd copy.png",  # Specify your own page icon
    layout="centered",  # "wide" or "centered"
    initial_sidebar_state="auto",  # "auto", "expanded", "collapsed"
)


# Display the image in the sidebar
# st.sidebar.image('Fynd_logo2.png', width=300)
st.sidebar.markdown("""
    <style>
        .left-aligned-image {
            display: flex;
            justify-content: flex-start;
            width: 100%;
        }
        .left-aligned-image img {
            width: 100%;
            margin-bottom: 20px;
            height: auto;
            border-radius: 15px;
        }
    </style>
    <div class="left-aligned-image">
        <img src="https://www.medianews4u.com/wp-content/uploads/2021/02/Fynd-Platform-encourages-SMEs-to-start-an-online-business-with-a-new-campaign.jpg" alt="Fynd Image">
    </div>
    """, unsafe_allow_html=True)


html_content = """
    <h1 style='
        color: #a689f6;
        font-size: 50px;
        background-image: -webkit-linear-gradient(0deg, #a689f6 3%, #272191 33%, #413bb9 61%);
        background-clip: text;
        -webkit-background-clip: text;
        text-fill-color: transparent;
        -webkit-text-fill-color: transparent;
    '>Plan maker</h1>
"""

st.markdown(html_content, unsafe_allow_html=True)

st.write(" ")     
user_name = st.sidebar.text_input("Enter your name:")


html_content_1 = """
    <h1 style='
        color: #a689f6;
        font-size: 22px;
        background-image: -webkit-linear-gradient(0deg, #a689f6 3%, #272191 33%, #413bb9 61%);
        background-clip: text;
        -webkit-background-clip: text;
        text-fill-color: transparent;
        -webkit-text-fill-color: transparent;
    '>Section 1: Plan Info</h1>
"""

st.sidebar.markdown(html_content_1, unsafe_allow_html=True)

if 'company_name' not in st.session_state:
    st.session_state['company_name'] = "Company name not available"

v1 = ["Commerce India", "Reliance", "Commerce Global", "Government Projects", "Individual BH"]
business_head = st.sidebar.selectbox("Business Head", [""] + v1, help="Business Head is used for cost centre allocation")

# Initialize company_name in session state
if 'company_name' not in st.session_state:
    st.session_state['company_name'] = ""

# User inputs Company ID
company_id = st.sidebar.text_input("Company ID", help="Enter the company ID")
company_id_int = None

if company_id.strip():
    try:
        company_id_int = int(company_id.strip())
        st.session_state['company_id'] = company_id.strip() #ALways store the company id in session state
    except ValueError:
        st.error("Invalid Company ID. Please enter a valid integer.")
        company_id_int = None

# Construct the API URL and fetch data only if company_id_int is valid
if company_id_int is not None:
    try:
        api_url = f"https://api.boltic.io/service/platform/bolt/share/v1/data?api_token=eyJpZCI6IjU4OTdmZjZlLTMwMTYtNDZjMC1hMzdlLTJmNjBkMjhjYjc5YyIsInRlbmFudCI6ImdvZnluZGNvbSJ9&batch_size=10000&display_meta=true&cmpid={company_id_int}"
        response = requests.get(api_url)

        if response.status_code == 200:
            data = response.json()
            if "results" in data and len(data["results"]) > 0:
                st.session_state['company_name'] = data["results"][0].get("company_name", "Company name not found")
            else:
                st.session_state['company_name'] = "Company name not found"
        else:
            st.session_state['company_name'] = f"Error: Unable to fetch data (Status code: {response.status_code})"
    except Exception as e:
        st.session_state['company_name'] = f"An error occurred: {e}"
else:
    st.session_state['company_name'] = ""  # Set to an empty string if Company ID is not provided or invalid

# Display the company name
st.sidebar.text_input("Company Name", value=st.session_state['company_name'], disabled=False)

currency = st.sidebar.radio("Currency", options= ["INR", "USD"], help= "Select the type of currency", index =None)
bundle_by = st.sidebar.radio(options=["Single value", "Feature specific"], label="Bundle by", help = "Select the bundle", index=None)


################################################## Feature Specific ##########################################################
if bundle_by == "Feature specific":

    plan_name = st.sidebar.text_input("Plan Name", help="Enter the name of the plan")
    plan_description = st.sidebar.text_area("Plan Description", max_chars=250, help="Describe the plan")
    plan_start_date = st.sidebar.date_input("Plan Start Date", value=datetime.now(), help="Select the start date of the plan")


# Section 3: Defining products for each dropdown
    product_options = ["GoFynd", "Uniket", "ONDC", "StoreOS", "Website", "OMS", "WMS", "TMS", "GMC", "Partner",
                   "Catalog Cloud", "FCP", "PixelBin", "Boltic",
                   "CoPilot"]
    fee_type_options = ["Development", "Licensing", "Logistics", "Marketing", "Packaging", "Subscription", "Transaction"]
    fee_nature_options = ["Fixed %", "Flat currency", "Slab based", "As per rate card"]
    variable_options = ["Application", "Bag", "Extension", "Integration", "Order", "Platform", "Resource", "Shipment", "User", "B2B", "B2C"]
    chargeable_on_options = ["Active", "Build", "Cancelled", "Delivered", "DTO", "Invoiced", "Packed", "Picked", "Placed", "Return Window", "RTO", "Setup", "SiteOps-hr", "Subscribed", "TechOps-hr"]
    plan_validity_options = ["One time", "Monthly", "Quarterly", "Bi-Annually", "Annually"]
    payment_method_options = ["Prepaid", "Postpaid"]
 

    # Initialize session state variables if they don't exist
    if 'new_mapping' not in st.session_state:
        st.session_state['new_mapping'] = {}
    if 'file_data' not in st.session_state:
        st.session_state['file_data'] = None
    if 'mapping_mode' not in st.session_state:
        st.session_state['mapping_mode'] = None  # None, 'default', 'custom'
    if 'button_clicked' not in st.session_state:
        st.session_state['button_clicked'] = None

    # Function to handle custom button click
    def custom_button_click():
        st.session_state.button_clicked = 'custom'

    # Function to handle default button click
    def default_button_click():
        st.session_state.button_clicked = 'default'

    
    # Display buttons without immediate execution on click
    col1, col2 = st.sidebar.columns([0.25, 0.45])
    with col1:
        default_clicked = st.button("Default mapping")
    with col2:
        custom_clicked = st.button("Custom mapping")

    # Determine action based on button clicked
    if default_clicked:
        default_button_click()
    elif custom_clicked:
        custom_button_click()

     # Define CSS styles for the button
    button_styles = """
    <style>
    div.stButton > button {
        color: #ffffff; /* Text color */
        font-size: 50px;
        background-image: linear-gradient(0deg, #a689f6 3%, #413bb9 61%);
        border: none;
        padding: 10px 20px;
        cursor: pointer;
        border-radius: 15px;
        display: inline-block;
    }
    div.stButton > button:hover {
        background-color: #00ff00; /* Hover background color */
        color: #ff0000; /* Hover text color */
    }
    </style>
    """

# Render the button with the specified styles
    st.markdown(button_styles, unsafe_allow_html=True)


    # Handle Default Mapping
    if st.session_state.button_clicked == 'default':
        # Default mapping data
        new_mapping = {
            "GoFynd": {
                "Marketing": {
                    "Order": ["Placed"]
                },
                "Transaction": {
                    "Bag": ["Return Window"],
                    "Shipment": ["Cancelled", "RTO", "DTO"]
                },
                "Logistics": {
                    "Shipment": ["Picked", "RTO", "DTO"]
                },
                "Packaging": {
                    "Shipment": ["Packed"]
                }
            },
            "Uniket": {
                "Marketing": {
                    "Order": ["Placed"]
                },
                "Transaction": {
                    "Bag": ["Return Window"],
                    "Shipment": ["Cancelled", "RTO", "DTO"]
                },
                "Logistics": {
                    "Shipment": ["Picked", "RTO", "DTO"]
                },
                "Packaging": {
                    "Shipment": ["Packed"]
                }
            },
            "ONDC": {
                "Transaction": {
                    "Bag": ["Return Window"],
                    "Shipment": ["Cancelled", "RTO", "DTO"]
                },
                "Logistics": {
                    "Shipment": ["Picked", "RTO", "DTO"]
                },
                "Packaging": {
                    "Shipment": ["Packed"]
                }
            },
            "StoreOS": {
                "Licensing": {
                    "Application": ["Setup"]
                },
                "Subscription": {
                    "Application": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                },
                "Marketing": {
                    "Order": ["Placed"]
                },
                "Transaction": {
                    "Order": ["Placed"],
                    "Bag": ["Invoiced", "Delivered"]
                },
                "Logistics": {
                    "Shipment": ["Picked", "RTO", "DTO"]
                },
                "Packaging": {
                    "Shipment": ["Packed"]
                }
            },
            "Website": {
                "Licensing": {
                    "Application": ["Setup"]
                },
                "Subscription": {
                    "Application": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                },
                "Transaction": {
                    "Order": ["Placed"],
                    "Bag": ["Invoiced", "Delivered"]
                },
                "Logistics": {
                    "Shipment": ["Picked", "RTO", "DTO"]
                },
                "Packaging": {
                    "Shipment": ["Packed"]
                },
                "Development": {
                    "Application": ["Build"]
                }
            },
            "OMS": {
                "Development": {
                    "Integration": ["Build"]
                },
                "Licensing": {
                    "Integration": ["Setup"]
                },
                "Subscription": {
                    "Integration": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                },
                "Transaction": {
                    "Order": ["Placed"],
                    "Bag": ["Invoiced", "Delivered"]
                }
            },
            "WMS": {
                "Development": {
                    "Integration": ["Build"]
                },
                "Licensing": {
                    "Integration": ["Setup"]
                },
                "Subscription": {
                    "Integration": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                },
                "Transaction": {
                    "Order": ["Placed"],
                    "Bag": ["Invoiced", "Delivered"],
                    "B2B": ["Invoiced"],
                    "B2C": ["Invoiced"]
                }
            },
            "TMS": {
                "Development": {
                    "Integration": ["Build"]
                },
                "Licensing": {
                    "Integration": ["Setup"]
                },
                "Subscription": {
                    "Integration": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                },
                "Logistics": {
                    "Shipment": ["Picked", "RTO", "DTO"]
                }
            },
            "GMC": {
                "Development": {
                    "Extension": ["Build"],
                    "Integration": ["Build"]
                },
                "Licensing": {
                    "Extension": ["Setup"]
                },
                "Subscription": {
                    "Extension": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                }
            },
            "Partner": {
                "Development": {
                    "Extension": ["Build"]
                },
                "Licensing": {
                    "Extension": ["Setup"]
                },
                "Subscription": {
                    "Extension": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                }
            },
            "Catalog Cloud": {
                "Development": {
                    "Extension": ["Build"]
                },
                "Licensing": {
                    "Extension": ["Setup"]
                },
                "Subscription": {
                    "Extension": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                }
            },
            "FCP": {
                "Subscription": {
                    "Platform": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                }
            },
            "PixelBin": {
                "Subscription": {
                    "Platform": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                }
            },
            "Boltic": {
                "Subscription": {
                    "Platform": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                }
            },
            "CoPilot": {
                "Subscription": {
                    "Platform": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                }
            }
        }
        
        st.session_state['new_mapping'] = new_mapping
        st.session_state['mapping_mode'] = 'default'

        html_content_1 = """
                            <h1 style='
                                color: #a689f6;
                                font-size: 22px;
                                background-image: -webkit-linear-gradient(0deg, #a689f6 3%, #272191 33%, #413bb9 61%);
                                background-clip: text;
                                -webkit-background-clip: text;
                                text-fill-color: transparent;
                                -webkit-text-fill-color: transparent;
                            '>Section 2: Rule Info</h1>
                        """

        st.sidebar.markdown(html_content_1, unsafe_allow_html=True)

    # Handle Custom Mapping
    elif st.session_state.button_clicked == 'custom':

        # Define CSS styles for the button
        button_styles = """
            <style>
            div.stButton > button {
                color: #ffffff; /* Text color */
                font-size: 50px;
                background-image: linear-gradient(0deg, #a689f6 3%, #413bb9 61%);
                border: none;
                padding: 10px 20px;
                cursor: pointer;
                border-radius: 15px;
                display: inline-block;
            }
            div.stButton > button:hover {
                background-color: #00ff00; /* Hover background color */
                color: #ff0000; /* Hover text color */
            }
            </style>
            """

# Render the button with the specified styles
        st.markdown(button_styles, unsafe_allow_html=True)

        # File upload
        uploaded_file = st.sidebar.file_uploader("Upload a file:", type=['csv', 'xlsx'], key='file_uploader')

        html_content_1 = """
                            <h1 style='
                                color: #a689f6;
                                font-size: 22px;
                                background-image: -webkit-linear-gradient(0deg, #a689f6 3%, #272191 33%, #413bb9 61%);
                                background-clip: text;
                                -webkit-background-clip: text;
                                text-fill-color: transparent;
                                -webkit-text-fill-color: transparent;
                            '>Section 2: Rule Info</h1>
                        """

        st.sidebar.markdown(html_content_1, unsafe_allow_html=True)

        if uploaded_file is not None:
            try:
                # Read the file, treating the first row as the header
                if uploaded_file.name.endswith('.csv'):
                    data = pd.read_csv(uploaded_file, header=0)
                elif uploaded_file.name.endswith('.xlsx'):
                    data = pd.read_excel(uploaded_file, header=0)

                if st.sidebar.button("Overwrite headers"):
                    # Rename the headers
                    new_headers = ['ordering_channel', 'fee_type', 'variable_type', 'chargeable_on']
                    data.columns = new_headers

                    # Save the data to session state
                    st.session_state['file_data'] = data

                    # Initialize the mapping
                    new_mapping = {}

                    

                    # Build the mapping
                    for _, row in data.iterrows():

                
                        ordering_channel = row['ordering_channel']
                        fee_type = row['fee_type']
                        variable_type = row['variable_type']
                        chargeable_on = row['chargeable_on']

                        if ordering_channel not in new_mapping:
                            new_mapping[ordering_channel] = {}

                        if fee_type not in new_mapping[ordering_channel]:
                            new_mapping[ordering_channel][fee_type] = {}

                        if variable_type not in new_mapping[ordering_channel][fee_type]:
                            new_mapping[ordering_channel][fee_type][variable_type] = []

                        if chargeable_on not in new_mapping[ordering_channel][fee_type][variable_type]:
                            new_mapping[ordering_channel][fee_type][variable_type].append(chargeable_on)

                    st.session_state['new_mapping'] = new_mapping
                    st.session_state['mapping_mode'] = 'custom'

            except Exception as e:
                st.error(f"Error reading the file: {e}")

    # Handle mapping display
   # Initialize session state variables if they don't exist
    # Initialize session state variables with default values if not already set
    if 'business_head' not in st.session_state:
        st.session_state['business_head'] = None
    if 'company_id' not in st.session_state:
        st.session_state['company_id'] = None
    if 'company_name' not in st.session_state:
        st.session_state['company_name'] = None
    if 'currency' not in st.session_state:
        st.session_state['currency'] = None
    if 'bundle_by' not in st.session_state:
        st.session_state['bundle_by'] = None
    if 'plan_name' not in st.session_state:
        st.session_state['plan_name'] = None
    if 'plan_description' not in st.session_state:
        st.session_state['plan_description'] = None
    if 'selected_ordering_channel' not in st.session_state:
        st.session_state['selected_ordering_channel'] = None
    if 'fulfilling_location' not in st.session_state:
        st.session_state['fulfilling_location'] = None
    if 'application_id' not in st.session_state:
        st.session_state['application_id'] = None
    if 'selected_fee_type' not in st.session_state:
        st.session_state['selected_fee_type'] = None
    if 'selected_variable_type' not in st.session_state:
        st.session_state['selected_variable_type'] = None
    if 'selected_chargeable_on' not in st.session_state:
        st.session_state['selected_chargeable_on'] = None
    if 'selected_fee_nature' not in st.session_state:
        st.session_state['selected_fee_nature'] = None
    if 'user_input_3' not in st.session_state:
        st.session_state['user_input_3'] = None
    if 'usage_3' not in st.session_state:
        st.session_state['usage_3'] = None
    if 'Capping_or_Minimum_Guarantee_3' not in st.session_state:
        st.session_state['Capping_or_Minimum_Guarantee_3'] = None
    if 'threshold' not in st.session_state:
        st.session_state['threshold'] = None
    if 'expected_billing' not in st.session_state:
        st.session_state['expected_billing'] = None
    if 'selected_payment_method' not in st.session_state:
        st.session_state['selected_payment_method'] = None
    if 'selected_plan_validity' not in st.session_state:
        st.session_state['selected_plan_validity'] = None
    if 'final_billing' not in st.session_state:
        st.session_state['final_billing'] = None
    if 'fee_reversal' not in st.session_state:
        st.session_state['fee_reversal'] = None
    if 'reversal_per' not in st.session_state:
        st.session_state['reversal_per'] = None

    # Check if 'mapping_mode' exists in session state before using it
    if 'mapping_mode' in st.session_state and st.session_state['mapping_mode']:
        # 1st layer: Select ordering channel
        selected_ordering_channel = st.sidebar.selectbox(
            "Product lines",
            [""] + list(st.session_state['new_mapping'].keys()),
            help="Select a Product line",
            key='ordering_channel'
        )

        if selected_ordering_channel:
            selected_ordering_channel = str(selected_ordering_channel) or int(selected_ordering_channel)

            # Determine fulfilling_location and application_id based on selected_ordering_channel
            if any(channel in selected_ordering_channel for channel in ["TMS", "GMC", "Catalog Cloud"]):
                st.session_state['fulfilling_location'] = None
                st.session_state['application_id'] = None
            else:
                abc = ["Store", "Warehouse"]
                st.session_state['fulfilling_location'] = st.sidebar.selectbox(
                    "Fulfilling Location", [""] + abc, help="Select the fulfilling location"
                )
                st.session_state['application_id'] = st.sidebar.text_input(
                    "Application ID", key="application_id_input", help="Enter the application ID"
                )

            # Use the session state variables in your app
            fulfilling_location = st.session_state['fulfilling_location']
            application_id = st.session_state['application_id']



            # 2nd layer: Select fee type based on the selected ordering channel

            if 'selected_fee_type' not in st.session_state:
                st.session_state['selected_fee_type'] = None

            fee_types_for_channel = st.session_state['new_mapping'].get(selected_ordering_channel, {}).keys()
            fee_types_for_channel = list(fee_types_for_channel)

            if fee_types_for_channel:


                # Define selected_fee_type based on user input

                st.session_state['selected_fee_type'] = st.selectbox(
                    "Fee Type",
                    [""] + fee_types_for_channel,
                    help="Fee type is a revenue head for grouping the revenue",
                    key='fee_type'
                )

                # Access selected_fee_type for any session state operations
                selected_fee_type = st.session_state['selected_fee_type']

                # 3rd layer: Select variable type based on the selected fee type
                variable_type_options = []
                if selected_fee_type:
                    details = st.session_state['new_mapping'].get(selected_ordering_channel, {})
                    variables = details.get(selected_fee_type, {})
                    variable_type_options = list(variables.keys())

                st.session_state['selected_variable_type'] = st.selectbox(
                    "Variable Type",
                    [""] + variable_type_options,
                    help="Variable type is an attribute on which revenue calculation will be done",
                    key='variable_type'
                )

                selected_variable_type = st.session_state['selected_variable_type']

            
                # 4th layer: Select chargeable on based on the selected variable type
                chargeable_on_options = []
                if selected_variable_type:
                    chargeable_on_options = variables.get(selected_variable_type, [])

                st.session_state['selected_chargeable_on'] = st.selectbox(
                    "Chargeable on",
                    [""] + chargeable_on_options,
                    help="Chargeable on is a service status on which the above fee will be applicable",
                    key='chargeable_on'
                )

                selected_chargeable_on = st.session_state['selected_chargeable_on']

            else:
                st.write("No fee types available for the selected ordering channel.")
        else:
            st.write("")


# Create an empty DataFrame with the desired column names
    slabs_df = pd.DataFrame(columns=["Slab", "Max_value", "Commercial_value", "Usage", "Capping/Min_Guarantee_value", "Threshold"])

    ttt = ["Fixed %", "Flat currency", "Slab based"]
    selected_fee_nature = st.selectbox("Fee Nature:", [""] + ttt, key="fee_nature_select", help = "Fee nature is an option to consume the price in flat / % / slab")

    if selected_fee_nature == "Slab based":

        def add_data(slab, max_value, user_input, Usage, Capping_or_Minimum_Guarantee, threshold):
    # Convert values to int if not None
            max_value = int(max_value.strip()) if max_value else None
            user_input_11 = int(user_input.strip()) if user_input else None
            try:
                user_input_22 = float(user_input_11)
                user_input = "{:.2f}".format(user_input_22)
            except (TypeError, ValueError):
                user_input = 0


            Usage = int(Usage.strip()) if Usage else None
            Capping_or_Minimum_Guarantee = int(Capping_or_Minimum_Guarantee.strip()) if Capping_or_Minimum_Guarantee else None

            # Create a new row dictionary
            new_row = {
                "Slab": slab,
                "Max_value": max_value,
                "Commercial_value": user_input,
                "Usage": Usage,
                "Capping/Min_Guarantee_value": Capping_or_Minimum_Guarantee,
                "Threshold": threshold
            }

             # Append new_row to global list of rows in session state

            st.session_state.global_data_rows.append(new_row)
            

        def main():

            # Initialize session state if not already initialized
            if 'global_data_rows' not in st.session_state:
                st.session_state.global_data_rows = []

            # Inputs for adding data
            col1, col2 = st.columns([2, 2])
            with col1:
                slab = st.selectbox("Slab:", [1, 2, 3], key="slab_select")  # Selectbox for slab numbers
                user_input = st.text_input("Commercial Value:", key="user_input")
                Capping_or_Minimum_Guarantee = st.text_input("Capping/Min_Guarantee:", key="Capping_or_Minimum_Guarantee_input")
            with col2:
                max_value = st.text_input("Max value:", key="max_value_input")
                Usage = st.text_input("Usage:", key="Usage_input")
                threshold = st.text_input("Threshold:", key="threshold_input")

            # Button to add data
            if st.button("Add Data", key="add_data_button"):
                if not (slab and user_input and Capping_or_Minimum_Guarantee):
                    st.warning("Please fill in all required fields.")
                else:
                    add_data(slab, max_value, user_input, Usage, Capping_or_Minimum_Guarantee, threshold)
                    st.success(f"Data for Slab '{slab}' added successfully!")

            # Display all entries in a single table
            if global_data_rows:
                st.subheader("All Slabs Data Summary")
                for i, row in enumerate(global_data_rows):
                    st.write(f"Entry {i + 1}:")
                    st.table([row])

        if __name__ == "__main__":
            main()


        user_input_3 = 0
        
    elif selected_fee_nature in ["Fixed %", "Flat currency"]:
        user_input_1 = st.number_input("Enter Commercial value:",min_value=0.0, help = "Enter the Commercial value")
        

        try:
            user_input_2 = float(user_input_1)
            user_input_3 = "{:.2f}".format(user_input_2)
        except (TypeError, ValueError):
            user_input_3 = 0
    

    # 6th layer: Expected Billing

        selected_fee_type = st.session_state['selected_fee_type']
        selected_variable_type = st.session_state['selected_variable_type']

        fee_reversal_options = ["RTO", "DTO", "Cancel"]
        fee_reversal = st.multiselect("Fee Reversal", fee_reversal_options, help = "Fee reversal is an option of reversing to be given to any fee on a particular service status") if selected_fee_type == "Transaction" and selected_variable_type == "Bag" else None
        reversal_per = st.number_input("Reversal %", min_value= 0.0, help = "Enter the reversal percentage") if selected_fee_type == "Transaction" and selected_variable_type == "Bag" else None

        usage_1 = st.number_input(f"Usage limit for {selected_variable_type}", min_value=0.0, help="Enter the usage limit")
        usage_2 = float(usage_1)
        usage_3 = "{:.2f}".format(usage_2)

        

        Product_1 = usage_1 * user_input_1 if selected_fee_nature in ["Fixed %", "Flat currency"] else 0
        Product_2 = float(Product_1)
        Product_3 = "{:.2f}".format(Product_2)


        abc = ['Capping value', 'Minimum Guarantee']
        threshold = st.selectbox("Threshold option:", [""] + abc, help = "If Minimum guarantee is set as threshold option then Expected billing will be the higher value b/w 'Commercial*Usage' & 'Threshold' and if it is set to Capping then Expected billing will be the lower value b/w 'Commercial*Usage' & 'Threshold'")

        Capping_or_Minimum_Guarantee_1 = st.number_input(f"Threshold value for {selected_variable_type}", min_value=0.0,
                                                    help="Enter the Capping/Minimum Gauarntee value")
        Capping_or_Minimum_Guarantee_2 = float(Capping_or_Minimum_Guarantee_1)
        Capping_or_Minimum_Guarantee_3 = "{:.2f}".format(Capping_or_Minimum_Guarantee_2)


        initial_expected_billing = Product_1
        expected_billing = None
        
        if threshold == 'Capping value':
            expected_billing = min(Product_1, Capping_or_Minimum_Guarantee_1)
        elif threshold == 'Minimum Guarantee':
            expected_billing = max(Product_1, Capping_or_Minimum_Guarantee_1)
        else:
            expected_billing = initial_expected_billing # Default to initial Product value if no threshold selected

        plan_validity_options = ["One time", "Monthly", "Quarterly", "Bi-Annually", "Annually"]
        selected_plan_validity = st.selectbox("Plan Validity", [""] + plan_validity_options, help="Plan validity defines the periodicity of calculation for the above billing", key="plan_vd_2")

        html_content_7 = f"""
        <h1 style='
            color: #a689f6;
            font-size: 20px;
            background-image: -webkit-linear-gradient(0deg, #a689f6 3%, #272191 33%, #413bb9 61%);
            background-clip: text;
            -webkit-background-clip: text;
            text-fill-color: transparent;
            -webkit-text-fill-color: transparent;
            '>Expected Monthly Billing (excluding GST): {expected_billing}</h1>
            """

        # Using Streamlit to display the HTML content
        st.markdown(html_content_7, unsafe_allow_html=True)
            
        initial_net_billing = 0
        final_billing = 0
        

        if selected_plan_validity == 'One time' or selected_plan_validity == 'Monthly':
            final_billing = 1*expected_billing
        elif selected_plan_validity == 'Quarterly':
            final_billing = 3*expected_billing
        elif selected_plan_validity == 'Bi-Annually':
            final_billing = 6*expected_billing
        elif selected_plan_validity == 'Annually':
            final_billing = 12*expected_billing
        else:
            final_billing = initial_net_billing

        html_content_8 = f"""
        <h1 style='
            color: #a689f6;
            font-size: 20px;
            background-image: -webkit-linear-gradient(0deg, #a689f6 3%, #272191 33%, #413bb9 61%);
            background-clip: text;
            -webkit-background-clip: text;
            text-fill-color: transparent;
            -webkit-text-fill-color: transparent;
            '>Net total billing (excluding GST): {final_billing}</h1>
            """

        # Using Streamlit to display the HTML content
        st.markdown(html_content_8, unsafe_allow_html=True)


    payment_method_options = ["Prepaid", "Postpaid"]
    selected_payment_method = st.selectbox("Payment Method", [""] + payment_method_options, help="Payment method defines the mode of payment of the plan", key="py_vd_2")

    

# Submit button
    # Define CSS styles for the button
    button_styles = """
    <style>
    div.stButton > button {
        color: #ffffff; /* Text color */
        font-size: 50px;
        background-image: linear-gradient(0deg, #a689f6 3%, #413bb9 61%);
        border: none;
        padding: 10px 20px;
        cursor: pointer;
        border-radius: 15px;
        display: inline-block;
    }
    div.stButton > button:hover {
        background-color: #00ff00; /* Hover background color */
        color: #ff0000; /* Hover text color */
    }
    </style>
    """

# Render the button with the specified styles
    st.markdown(button_styles, unsafe_allow_html=True)


    rule_id = None
    plan_id = None 
    # business_head = st.session_state['business_head']
    company_name = st.session_state['company_name']
    company_id = st.session_state['company_id']
    # currency = st.session_state['currency']
    # # bundle_by = st.session_state['bundle_by']
    # plan_name = st.session_state['plan_name']
    # plan_description = st.session_state['plan_description']
    # selected_ordering_channel = st.session_state['selected_ordering_channel']
    # fulfilling_location = st.session_state['fulfilling_location']
    # application_id = st.session_state['application_id']
    # selected_fee_type = st.session_state['selected_fee_type']
    # selected_variable_type = st.session_state['selected_variable_type']
    # selected_chargeable_on = st.session_state['selected_chargeable_on']
    # user_input_3 = st.session_state['user_input_3']
    # usage_3 = st.session_state['usage_3']
    # Capping_or_Minimum_Guarantee_3 = st.session_state['Capping_or_Minimum_Guarantee_3']
    # fee_reversal = st.session_state['fee_reversal']
    # reversal_per = st.session_state['reversal_per']
    # threshold = st.session_state['threshold']
    # expected_billing = st.session_state['expected_billing']
    # selected_payment_method = st.session_state['selected_payment_method']
    # selected_plan_validity = st.session_state['selected_plan_validity']
    # final_billing = int(st.session_state['final_billing']) if st.session_state['final_billing'] is not None else 0

    if st.button("Submit", key ='submit_button'):

    # Save user data
        user_data = {
        "Business_Head": business_head,
        "Company_ID": company_id.strip() if company_id else None,
        "Company_Name": company_name,
        "Currency": currency,
        "Bundle_by": bundle_by,
        "Plan_Name": plan_name,
        "Plan_Description": plan_description,
        "Product_lines": selected_ordering_channel,
        "Fulfilling_Location": fulfilling_location,
        "Application_ID": application_id,
        "Fee_Type": selected_fee_type,
        "Variable_Type": selected_variable_type,
        "Chargeable_on": selected_chargeable_on,
        "Fee_Nature": selected_fee_nature,
        "Commercial_value": user_input_3,
        "Fee_reversal": 0,
        "Reversal_%": 0,
        "Usage_limit": usage_3,
        "Threshold_value": Capping_or_Minimum_Guarantee_3,
        "Threshold_option": threshold,
        "Expected_Billing": expected_billing,
        "Payment_Method": selected_payment_method,
        "Plan_Validity": selected_plan_validity,
        "Net_total_billing":int(final_billing)
        }

        # Generate rule_id and plan_id
        rule_id = generate_rule_id(user_data)
        plan_id = generate_plan_id(user_data)

        # Update user_data with generated IDs
        user_data['rule_id'] = rule_id
        user_data['plan_id'] = plan_id

        # Save the user data locally and to session state
        # global_user_data.append((user_name, user_data))
        save_user_data(user_data)

        # Display saved records
        display_saved_records()


        # Store created_at, rule_id, and plan_id in session state
        st.session_state['created_at'] = datetime.now().isoformat()
        st.session_state['rule_id'] = rule_id
        st.session_state['plan_id'] = plan_id

    
    if st.button("Export data to BQ"):
        def insert_into_bq(user_data, project_id, dataset_id, table_id, service_account_info):
            try:
                # Use from_service_account_info instead of from_service_account_file
                credentials = service_account.Credentials.from_service_account_info(service_account_info)
                
                # Initialize a BigQuery client
                client = bigquery.Client(credentials=credentials, project=project_id)
                logging.info("BigQuery client initialized successfully.")
                
                # Specify the table reference
                table_ref = f"{project_id}.{dataset_id}.{table_id}"
                table = client.get_table(table_ref)
                logging.info(f"Table reference obtained: {table_ref}")
                
                # Insert form data into the table
                rows_to_insert = [user_data]
                errors = client.insert_rows_json(table, rows_to_insert)
                
                if errors:
                    logging.error(f"Errors occurred while inserting rows: {errors}")
                    st.error(f"Errors occurred while inserting rows: {errors}")
                else:
                    logging.info("Insert successful")
                    st.success("Insert successful")
            except Exception as e:
                logging.exception("An error occurred during the BigQuery insert operation.")
                st.error(f"An error occurred: {e}")

        project_id = "fynd-db"
        dataset_id = "finance_dwh"
        table_id = "plan_v2"
        fulfilling_location = st.session_state['fulfilling_location']
        application_id = st.session_state['application_id']
        # bundle_by = st.session_state['bundle_by']

        # Retrieve created_at, rule_id, and plan_id from session state
        created_at = st.session_state.get('created_at')
        rule_id = st.session_state.get('rule_id')
        plan_id = st.session_state.get('plan_id')


        user_data = {
            "Business_Head": business_head,
            "Company_ID": company_id,
            "Company_Name": company_name,
            "Currency": currency,
            "Bundle_by": bundle_by,
            "Plan_Name": plan_name,
            "Plan_Description": plan_description,
            "Product_lines": selected_ordering_channel,
            "Fulfilling_Location": fulfilling_location,
            "Application_ID": application_id,
            "Fee_Type": selected_fee_type,
            "Variable_Type": selected_variable_type,
            "Chargeable_on": selected_chargeable_on,
            "Fee_Nature": selected_fee_nature,
            "Commercial_value": user_input_3,
            "Fee_reversal": 0,
            "Reversal_%": 0,
            "Usage_limit": usage_3,
            "Threshold_value": Capping_or_Minimum_Guarantee_3,
            "Threshold_option": threshold,
            "Expected_Billing": expected_billing,
            "Payment_Method": selected_payment_method,
            "Plan_Validity": selected_plan_validity,
            "rule_id": rule_id,
            "plan_id": plan_id,
            "created_at": created_at,
            "Net_total_billing":int(final_billing)
        }
        

        save_user_data(user_data)  # Save user data after submission

        service_account_info = {
            "type": "service_account",
            "project_id": "fynd-db",
            "private_key_id": "48954327ef172acc3091a91881840dc353ff0413",
            "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCmIs0vgXflZmHD\n0IeHCiDEXavRb2GvRQqkCZGLy/EHFO7XLmpNrFzZHTSQach6HErARWTMsd2AMIA5\nHbmjEyt9deTOMUmICszkwNwpn6pzuoF2v4+fL2PQ4VBElLfCv4qbP9JGljvLsj5b\nC9UWe9rWgRrw2Z88LlVmFy3k4Om80Q57gW3hSYZpG4rBhC9+YRXjZ+Iq5SFEJQ+U\nj8ej7q6DnI8Prbvw/e13JH1pxm9AUQDm+sTsBmvjh4Ye02vKbfUP+pFOM0eC3Eoa\nOyG7unZnDuAaK9g87xVjd9HtD6MfaM0EHAEF1eyC4UqeiXTbVMwi7+0IGA2evEDZ\n3WRR2i+lAgMBAAECggEAF7Vis7NVsVqFfCS/moFTAMLfWLF87r67EIK/DwSpHloZ\n5YJdsDz3ORKmZC98aRSthDfC9UUyt270dXItAj0jmTFgWB6HgE8OQ1zUbmo3MHG+\ntPwMWmqwY2gsBMV2Xefot1QJxYH+AYkrxeFv5NgC+FaPSiy8QSHZlQqcxYtlP8kY\nzv8QFMgZ3b0sixK+YcSjCRXHu/DznnS/ZU7XlTALxANa50hT11rVLq6zYDF40fqC\nOWCOZvupjMOnNimXCWn7q6D1F8S/IMYlnHArJpEb7SdbJnJzxUvJC2rrMSoY896r\nyFEVhlElWp5GfTopu0WnL7iaw3pukM1OtpanFAk29wKBgQDW+yMzL7RiQ5LdElVP\n7Ml5hs+t6cjrUJTgBI8Uf9qGxE+tMe7Ye1fyJfbYslbOYO9xIUUT1jJmU6jadNWX\nA7UT59/IjvAOBIYyMEdh6Xo+H6ggW8DeU1HSdvixnQdgi0c03E6OCbOKi9E/UzK/\nTK2RBJJLN4MCiNL0FzrhTuxyEwKBgQDF1cz9Sb2UbrL5hr8LdcEH0ctatNlhJcSN\nnMd0Fn+e1EAkip+Hs9HmTvNdty4gQE+yEq/Tj0cRHXmjzK2AYx0WYRAhWGNSTlTS\n4OJdjE6vsoPu7Ouz1R/i/egKniUifEXYSgyL+oPBUrqqsnk9lgGBkJipN3u1f9+K\niuUJJP/OZwKBgF6ZvqCcom0HPU5I7f+wu+vdVfA6yy45lHmLqAamSFw7cLBPI8Jh\nbI7jA9/Rgn9oipUmxcX34M/Eiq4u8Xp1qC4tP/16YMpaVU8qjY7ZdfB2b75lgdaT\npZLOxZsq9X8Xauso8uxv+nDCG/8YtmEV9d61u0acE+t+mA3PVxqkZ0m/AoGBAI5c\nY76AqeN+JVxaEm/0tIsj9Om46hR2URJ2lzB6YCuzINUqy9GjHJBWj9oITzD2FmNV\n/yCGIeW3CClOyCtzJyNLhYf5Sr+XjoKRQVN/+7+C/l2YL6Sg4Ok/PRMm6iH+u2QB\nJTY1d0pOdfUPqR8gKsVJgBGE04iwE/RmLpp9/XZRAoGAFtaa9JeHk9dmZiST3lev\ns5syeOK1cCj1+nJer/GPEjqWMKxbCULvoEYBp7RaoEXjfGi7P4Mc6YHnbljHc+sL\niJzHhxrv/HYZdlCdTU3IRWT0sNHzqZemfYnsVTSp+4y61sGDxioDYL4CLnMq5T2F\nqzfc2qp3s8ApVloY3giuqnI=\n-----END PRIVATE KEY-----\n",
            "client_email": "plan-maker@fynd-db.iam.gserviceaccount.com",
            "client_id": "114883060650442183907",
            "auth_uri": "https://accounts.google.com/o/oauth2/auth",
            "token_uri": "https://oauth2.googleapis.com/token",
            "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
            "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/plan-maker%40fynd-db.iam.gserviceaccount.com",
            "universe_domain": "googleapis.com"
        }

        insert_into_bq(user_data, project_id, dataset_id, table_id, service_account_info)


###################################################### Single value ###########################################################

elif bundle_by == "Single value":
    user_input_1 = st.sidebar.number_input("Enter Commercial value:")
    user_input_2 = float(user_input_1)
    st.session_state['user_input_3'] = "{:.2f}".format(user_input_2)

    plan_name = st.sidebar.text_input("Plan Name", help="Enter the name of the plan")
    plan_description = st.sidebar.text_area("Plan Description", max_chars=250, help="Describe the plan")
    plan_start_date = st.sidebar.date_input("Plan Start Date", value=datetime.now(), help="Select the start date of the plan")




# Section 3: Defining products for each dropdown
    product_options = ["GoFynd", "Uniket", "ONDC", "StoreOS", "Website", "OMS", "WMS", "TMS", "GMC", "Partner",
                   "Catalog Cloud", "FCP", "PixelBin", "Boltic",
                   "CoPilot"]
    fee_type_options = ["Development", "Licensing", "Logistics", "Marketing", "Packaging", "Subscription", "Transaction"]
    fee_nature_options = ["Fixed %", "Flat currency", "Slab based", "As per rate card"]
    variable_options = ["Application", "Bag", "Extension", "Integration", "Order", "Platform", "Resource", "Shipment", "User", "B2B", "B2C"]
    chargeable_on_options = ["Active", "Build", "Cancelled", "Delivered", "DTO", "Invoiced", "Packed", "Picked", "Placed", "Return Window", "RTO", "Setup", "SiteOps-hr", "Subscribed", "TechOps-hr"]
    plan_validity_options = ["One time", "Monthly", "Quarterly", "Bi-Annually", "Annually"]
    payment_method_options = ["Prepaid", "Postpaid"]


    # Initialize session state variables if they don't exist
    if 'new_mapping' not in st.session_state:
        st.session_state['new_mapping'] = {}
    if 'file_data' not in st.session_state:
        st.session_state['file_data'] = None
    if 'mapping_mode' not in st.session_state:
        st.session_state['mapping_mode'] = None  # None, 'default', 'custom'
    if 'button_clicked' not in st.session_state:
        st.session_state['button_clicked'] = None

    # Function to handle custom button click
    def custom_button_click():
        st.session_state.button_clicked = 'custom'

    # Function to handle default button click
    def default_button_click():
        st.session_state.button_clicked = 'default'

    
    # Display buttons without immediate execution on click
    col1, col2 = st.sidebar.columns([0.25, 0.45])
    with col1:
        default_clicked = st.button("Default mapping")
    with col2:
        custom_clicked = st.button("Custom mapping")

    # Determine action based on button clicked
    if default_clicked:
        default_button_click()
    elif custom_clicked:
        custom_button_click()

     # Define CSS styles for the button
    button_styles = """
    <style>
    div.stButton > button {
        color: #ffffff; /* Text color */
        font-size: 50px;
        background-image: linear-gradient(0deg, #a689f6 3%, #413bb9 61%);
        border: none;
        padding: 10px 20px;
        cursor: pointer;
        border-radius: 15px;
        display: inline-block;
    }
    div.stButton > button:hover {
        background-color: #00ff00; /* Hover background color */
        color: #ff0000; /* Hover text color */
    }
    </style>
    """

# Render the button with the specified styles
    st.markdown(button_styles, unsafe_allow_html=True)

# Given new_mapping dictionary
 # Handle Default Mapping
    if st.session_state.button_clicked == 'default':
        new_mapping = {
            "GoFynd": {
                "Marketing": {
                    "Order": ["Placed"]
                },
                "Transaction": {
                    "Bag": ["Return Window"],
                    "Shipment": ["Cancelled", "RTO", "DTO"]
                },
                "Logistics": {
                    "Shipment": ["Picked", "RTO", "DTO"]
                },
                "Packaging": {
                    "Shipment": ["Packed"]
                }
            },
            "Uniket": {
                "Marketing": {
                    "Order": ["Placed"]
                },
                "Transaction": {
                    "Bag": ["Return Window"],
                    "Shipment": ["Cancelled", "RTO", "DTO"]
                },
                "Logistics": {
                    "Shipment": ["Picked", "RTO", "DTO"]
                },
                "Packaging": {
                    "Shipment": ["Packed"]
                }
            },
            "ONDC": {
                "Transaction": {
                    "Bag": ["Return Window"],
                    "Shipment": ["Cancelled", "RTO", "DTO"]
                },
                "Logistics": {
                    "Shipment": ["Picked", "RTO", "DTO"]
                },
                "Packaging": {
                    "Shipment": ["Packed"]
                }
            },
            "StoreOS": {
                "Licensing": {
                    "Application": ["Setup"]
                },
                "Subscription": {
                    "Application": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                },
                "Marketing": {
                    "Order": ["Placed"]
                },
                "Transaction": {
                    "Order": ["Placed"],
                    "Bag": ["Invoiced", "Delivered"]
                },
                "Logistics": {
                    "Shipment": ["Picked", "RTO", "DTO"]
                },
                "Packaging": {
                    "Shipment": ["Packed"]
                }
            },
            "Website": {
                "Licensing": {
                    "Application": ["Setup"]
                },
                "Subscription": {
                    "Application": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                },
                "Transaction": {
                    "Order": ["Placed"],
                    "Bag": ["Invoiced", "Delivered"]
                },
                "Logistics": {
                    "Shipment": ["Picked", "RTO", "DTO"]
                },
                "Packaging": {
                    "Shipment": ["Packed"]
                },
                "Development": {
                    "Application": ["Build"]
                }
            },
            "OMS": {
                "Development": {
                    "Integration": ["Build"]
                },
                "Licensing": {
                    "Integration": ["Setup"]
                },
                "Subscription": {
                    "Integration": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                },
                "Transaction": {
                    "Order": ["Placed"],
                    "Bag": ["Invoiced", "Delivered"]
                }
            },
            "WMS": {
                "Development": {
                    "Integration": ["Build"]
                },
                "Licensing": {
                    "Integration": ["Setup"]
                },
                "Subscription": {
                    "Integration": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                },
                "Transaction": {
                    "Order": ["Placed"],
                    "Bag": ["Invoiced", "Delivered"],
                    "B2B": ["Invoiced"],
                    "B2C": ["Invoiced"]
                }
            },
            "TMS": {
                "Development": {
                    "Integration": ["Build"]
                },
                "Licensing": {
                    "Integration": ["Setup"]
                },
                "Subscription": {
                    "Integration": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                },
                "Logistics": {
                    "Shipment": ["Picked", "RTO", "DTO"]
                }
            },
            "GMC": {
                "Development": {
                    "Extension": ["Build"],
                    "Integration": ["Build"]
                },
                "Licensing": {
                    "Extension": ["Setup"]
                },
                "Subscription": {
                    "Extension": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                }
            },
            "Partner": {
                "Development": {
                    "Extension": ["Build"]
                },
                "Licensing": {
                    "Extension": ["Setup"]
                },
                "Subscription": {
                    "Extension": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                }
            },
            "Catalog Cloud": {
                "Development": {
                    "Extension": ["Build"]
                },
                "Licensing": {
                    "Extension": ["Setup"]
                },
                "Subscription": {
                    "Extension": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                }
            },
            "FCP": {
                "Subscription": {
                    "Platform": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                }
            },
            "PixelBin": {
                "Subscription": {
                    "Platform": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                }
            },
            "Boltic": {
                "Subscription": {
                    "Platform": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                }
            },
            "CoPilot": {
                "Subscription": {
                    "Platform": ["Subscribed"],
                    "User": ["Active"],
                    "Resource": ["TechOps-hr", "SiteOps-hr"]
                }
            }
        }

        st.session_state['new_mapping'] = new_mapping
        st.session_state['mapping_mode'] = 'default'

        html_content_1 = """
                            <h1 style='
                                color: #a689f6;
                                font-size: 22px;
                                background-image: -webkit-linear-gradient(0deg, #a689f6 3%, #272191 33%, #413bb9 61%);
                                background-clip: text;
                                -webkit-background-clip: text;
                                text-fill-color: transparent;
                                -webkit-text-fill-color: transparent;
                            '>Section 2: Rule Info</h1>
                        """

        st.sidebar.markdown(html_content_1, unsafe_allow_html=True)

    # Handle Custom Mapping
    elif st.session_state.button_clicked == 'custom':

        # Define CSS styles for the button
        button_styles = """
            <style>
            div.stButton > button {
                color: #ffffff; /* Text color */
                font-size: 50px;
                background-image: linear-gradient(0deg, #a689f6 3%, #413bb9 61%);
                border: none;
                padding: 10px 20px;
                cursor: pointer;
                border-radius: 15px;
                display: inline-block;
            }
            div.stButton > button:hover {
                background-color: #00ff00; /* Hover background color */
                color: #ff0000; /* Hover text color */
            }
            </style>
            """

# Render the button with the specified styles
        st.markdown(button_styles, unsafe_allow_html=True)

        # File upload
        uploaded_file = st.sidebar.file_uploader("Upload a file:", type=['csv', 'xlsx'], key='file_uploader')

        html_content_1 = """
                            <h1 style='
                                color: #a689f6;
                                font-size: 22px;
                                background-image: -webkit-linear-gradient(0deg, #a689f6 3%, #272191 33%, #413bb9 61%);
                                background-clip: text;
                                -webkit-background-clip: text;
                                text-fill-color: transparent;
                                -webkit-text-fill-color: transparent;
                            '>Section 2: Rule Info</h1>
                        """

        st.sidebar.markdown(html_content_1, unsafe_allow_html=True)

        if uploaded_file is not None:
            try:
                # Read the file, treating the first row as the header
                if uploaded_file.name.endswith('.csv'):
                    data = pd.read_csv(uploaded_file, header=0)
                elif uploaded_file.name.endswith('.xlsx'):
                    data = pd.read_excel(uploaded_file, header=0)

                if st.sidebar.button("Overwrite headers"):
                    # Rename the headers
                    new_headers = ['ordering_channel', 'fee_type', 'variable_type', 'chargeable_on']
                    data.columns = new_headers

                    # Save the data to session state
                    st.session_state['file_data'] = data

                    # Initialize the mapping
                    new_mapping = {}

                    

                    # Build the mapping
                    for _, row in data.iterrows():

                
                        ordering_channel = row['ordering_channel']
                        fee_type = row['fee_type']
                        variable_type = row['variable_type']
                        chargeable_on = row['chargeable_on']

                        if ordering_channel not in new_mapping:
                            new_mapping[ordering_channel] = {}

                        if fee_type not in new_mapping[ordering_channel]:
                            new_mapping[ordering_channel][fee_type] = {}

                        if variable_type not in new_mapping[ordering_channel][fee_type]:
                            new_mapping[ordering_channel][fee_type][variable_type] = []

                        if chargeable_on not in new_mapping[ordering_channel][fee_type][variable_type]:
                            new_mapping[ordering_channel][fee_type][variable_type].append(chargeable_on)

                    st.session_state['new_mapping'] = new_mapping
                    st.session_state['mapping_mode'] = 'custom'

            except Exception as e:
                st.error(f"Error reading the file: {e}")
    # Section 2: Company Info
    
    


     # Handle mapping display
   # Initialize session state variables if they don't exist
    # Initialize session state variables with default values if not already set
    if 'business_head' not in st.session_state:
        st.session_state['business_head'] = None
    if 'company_id' not in st.session_state:
        st.session_state['company_id'] = None
    if 'company_name' not in st.session_state:
        st.session_state['company_name'] = None
    if 'currency' not in st.session_state:
        st.session_state['currency'] = None
    if 'bundle_by' not in st.session_state:
        st.session_state['bundle_by'] = None
    if 'plan_name' not in st.session_state:
        st.session_state['plan_name'] = None
    if 'plan_description' not in st.session_state:
        st.session_state['plan_description'] = None
    if 'selected_ordering_channel' not in st.session_state:
        st.session_state['selected_ordering_channel'] = None
    if 'fulfilling_location' not in st.session_state:
        st.session_state['fulfilling_location'] = None
    if 'application_id' not in st.session_state:
        st.session_state['application_id'] = None
    if 'selected_fee_type' not in st.session_state:
        st.session_state['selected_fee_type'] = None
    if 'selected_variable_type' not in st.session_state:
        st.session_state['selected_variable_type'] = None
    if 'selected_chargeable_on' not in st.session_state:
        st.session_state['selected_chargeable_on'] = None
    if 'selected_fee_nature' not in st.session_state:
        st.session_state['selected_fee_nature'] = None
    if 'user_input_3' not in st.session_state:
        st.session_state['user_input_3'] = None
    if 'usage_3' not in st.session_state:
        st.session_state['usage_3'] = None
    if 'Capping_or_Minimum_Guarantee_3' not in st.session_state:
        st.session_state['Capping_or_Minimum_Guarantee_3'] = None
    if 'threshold' not in st.session_state:
        st.session_state['threshold'] = None
    if 'expected_billing' not in st.session_state:
        st.session_state['expected_billing'] = None
    if 'selected_payment_method' not in st.session_state:
        st.session_state['selected_payment_method'] = None
    if 'selected_plan_validity' not in st.session_state:
        st.session_state['selected_plan_validity'] = None
    if 'final_billing' not in st.session_state:
        st.session_state['final_billing'] = None
    if 'fee_reversal' not in st.session_state:
        st.session_state['fee_reversal'] = None
    if 'reversal_per' not in st.session_state:
        st.session_state['reversal_per'] = None


    # Check if 'mapping_mode' exists in session state before using it
    if 'mapping_mode' in st.session_state and st.session_state['mapping_mode']:
        # 1st layer: Select ordering channel
        selected_ordering_channel = st.sidebar.selectbox(
            "Product lines",
            [""] + list(st.session_state['new_mapping'].keys()),
            help="Select a Product line",
            key='ordering_channel'
        )

        if selected_ordering_channel:
            selected_ordering_channel = str(selected_ordering_channel) or int(selected_ordering_channel)

            # Determine fulfilling_location and application_id based on selected_ordering_channel
            if any(channel in selected_ordering_channel for channel in ["TMS", "GMC", "Catalog Cloud"]):
                st.session_state['fulfilling_location'] = None
                st.session_state['application_id'] = None
            else:
                abc = ["Store", "Warehouse"]
                st.session_state['fulfilling_location'] = st.sidebar.selectbox(
                    "Fulfilling Location", [""] + abc, help="Select the fulfilling location"
                )
                st.session_state['application_id'] = st.sidebar.text_input(
                    "Application ID", key="application_id_input", help="Enter the application ID"
                )

            # Use the session state variables in your app
            fulfilling_location = st.session_state['fulfilling_location']
            application_id = st.session_state['application_id']



            # 2nd layer: Select fee type based on the selected ordering channel

            if 'selected_fee_type' not in st.session_state:
                st.session_state['selected_fee_type'] = None

            fee_types_for_channel = st.session_state['new_mapping'].get(selected_ordering_channel, {}).keys()
            fee_types_for_channel = list(fee_types_for_channel)

            if fee_types_for_channel:


                # Define selected_fee_type based on user input

                st.session_state['selected_fee_type'] = st.selectbox(
                    "Fee Type",
                    [""] + fee_types_for_channel,
                    help="Fee type is a revenue head for grouping the revenue",
                    key='fee_type'
                )

                # Access selected_fee_type for any session state operations
                selected_fee_type = st.session_state['selected_fee_type']

                # 3rd layer: Select variable type based on the selected fee type
                variable_type_options = []
                if selected_fee_type:
                    details = st.session_state['new_mapping'].get(selected_ordering_channel, {})
                    variables = details.get(selected_fee_type, {})
                    variable_type_options = list(variables.keys())

                st.session_state['selected_variable_type'] = st.selectbox(
                    "Variable Type",
                    [""] + variable_type_options,
                    help="Variable type is an attribute on which revenue calculation will be done",
                    key='variable_type'
                )

                selected_variable_type = st.session_state['selected_variable_type']

            
                # 4th layer: Select chargeable on based on the selected variable type
                chargeable_on_options = []
                if selected_variable_type:
                    chargeable_on_options = variables.get(selected_variable_type, [])

                st.session_state['selected_chargeable_on'] = st.selectbox(
                    "Chargeable on",
                    [""] + chargeable_on_options,
                    help="Chargeable on is a service status on which the above fee will be applicable",
                    key='chargeable_on'
                )

                selected_chargeable_on = st.session_state['selected_chargeable_on']

            else:
                st.write("No fee types available for the selected ordering channel.")
        else:
            st.write("")

    
    selected_variable_type = st.session_state['selected_variable_type']

    usage_1 = st.number_input(f"Usage limit for {selected_variable_type}", min_value=0.0, help="Enter the usage limit")
    usage_2 = float(usage_1)
    usage_3 = "{:.2f}".format(usage_2)

    expected_billing = user_input_1


    html_content_8 = f"""
        <h1 style='
            color: #a689f6;
            font-size: 20px;
            background-image: -webkit-linear-gradient(0deg, #a689f6 3%, #272191 33%, #413bb9 61%);
            background-clip: text;
            -webkit-background-clip: text;
            text-fill-color: transparent;
            -webkit-text-fill-color: transparent;
            '>Expected Monthly Billing (excluding GST): {expected_billing}</h1>
            """

        # Using Streamlit to display the HTML content
    st.markdown(html_content_8, unsafe_allow_html=True)

    selected_plan_validity = st.selectbox("Plan Validity", [""] + plan_validity_options, help="Plan validity defines the periodicity of calculation for the above billing", key="plan_vd_2")
    selected_payment_method = st.selectbox("Payment Method", [""] + payment_method_options, help="Payment method defines the mode of payment of the plan", key="py_vd_2")

    initial_net_billing = 0
    user_input_3 = st.session_state['user_input_3']

    if selected_plan_validity == 'One time':
        final_billing = 1 * expected_billing
    elif selected_plan_validity == 'Monthly':
        final_billing = 1 * expected_billing
    elif selected_plan_validity == 'Quarterly':
        final_billing = 3 * expected_billing
    elif selected_plan_validity == 'Bi-Annually':
        final_billing = 6 * expected_billing
    elif selected_plan_validity == 'Annually':
        final_billing = 12 * expected_billing
    else:
        final_billing = initial_net_billing


    html_content_8 = f"""
        <h1 style='
            color: #a689f6;
            font-size: 20px;
            background-image: -webkit-linear-gradient(0deg, #a689f6 3%, #272191 33%, #413bb9 61%);
            background-clip: text;
            -webkit-background-clip: text;
            text-fill-color: transparent;
            -webkit-text-fill-color: transparent;
            '>Net total billing (excluding GST): {final_billing}</h1>
            """

        # Using Streamlit to display the HTML content
    st.markdown(html_content_8, unsafe_allow_html=True)


    button_styles = """
    <style>
    div.stButton > button {
        color: #ffffff; /* Text color */
        font-size: 50px;
        background-image: linear-gradient(0deg, #a689f6 3%, #413bb9 61%);
        border: none;
        padding: 10px 20px;
        cursor: pointer;
        border-radius: 15px;
        display: inline-block;
    }
    div.stButton > button:hover {
        background-color: #00ff00; /* Hover background color */
        color: #ff0000; /* Hover text color */
    }
    </style>
    """

# Render the button with the specified styles
    st.markdown(button_styles, unsafe_allow_html=True)

    rule_id = None
    plan_id = None 
    # business_head = st.session_state['business_head']
    company_name = st.session_state['company_name']
    company_id = st.session_state['company_id']
    # currency = st.session_state['currency']
    # # bundle_by = st.session_state['bundle_by']
    # plan_name = st.session_state['plan_name']
    # plan_description = st.session_state['plan_description']
    # selected_ordering_channel = st.session_state['selected_ordering_channel']
    # fulfilling_location = st.session_state['fulfilling_location']
    # application_id = st.session_state['application_id']
    # selected_fee_type = st.session_state['selected_fee_type']
    # selected_variable_type = st.session_state['selected_variable_type']
    # selected_chargeable_on = st.session_state['selected_chargeable_on']
    # user_input_3 = st.session_state['user_input_3']
    # usage_3 = st.session_state['usage_3']
    # Capping_or_Minimum_Guarantee_3 = st.session_state['Capping_or_Minimum_Guarantee_3']
    # fee_reversal = st.session_state['fee_reversal']
    # reversal_per = st.session_state['reversal_per']
    # threshold = st.session_state['threshold']
    # expected_billing = st.session_state['expected_billing']
    # selected_payment_method = st.session_state['selected_payment_method']
    # selected_plan_validity = st.session_state['selected_plan_validity']
    # final_billing = int(st.session_state['final_billing']) if st.session_state['final_billing'] is not None else 0
       

# Submit button
    if st.button("Submit"):
    # Save user data
        user_data = {
        "Business Head": business_head,
        "Company ID": company_id.strip() if company_id else None,
        "Company Name": company_name,
        "Currency": currency,
        "Bundle by": bundle_by,
        "Plan Name": plan_name,
        "Plan Description": plan_description,
        "Product lines": selected_ordering_channel,
        "Fulfilling Location": fulfilling_location,
        "Application ID": application_id,
        "Fee Type": selected_fee_type,
        "Variable Type": selected_variable_type,
        "Chargeable on": selected_chargeable_on,
        "Fee Nature": 0,
        "Commercial value": user_input_3,
        "Fee reversal": 0,
        "Reversal %": 0,
        "Usage limit": usage_3,
        "Threshold value": 0,
        "Threshold option": 0,
        "Expected Billing": user_input_3,
        "Plan Validity": selected_plan_validity,
        "Payment Method": selected_payment_method,
        "Net_total_billing":int(final_billing)
        }

        
        # Generate rule_id and plan_id
        rule_id = generate_rule_id(user_data)
        plan_id = generate_plan_id(user_data)

        # Update user_data with generated IDs
        user_data['rule_id'] = rule_id
        user_data['plan_id'] = plan_id

        # Save the user data locally and to session state
        # global_user_data.append((user_name, user_data))
        save_user_data(user_data)

        # Display saved records
        display_saved_records()


         # Store created_at, rule_id, and plan_id in session state
        st.session_state['created_at'] = datetime.now().isoformat()
        st.session_state['rule_id'] = rule_id
        st.session_state['plan_id'] = plan_id


    if st.button("Export data to BQ"):

        import logging
        from google.oauth2 import service_account
        from google.cloud import bigquery

        def insert_into_bq(user_data, project_id, dataset_id, table_id, service_account_info):
            try:
                # Use from_service_account_info instead of from_service_account_file
                credentials = service_account.Credentials.from_service_account_info(service_account_info)
                
                # Initialize a BigQuery client
                client = bigquery.Client(credentials=credentials, project=project_id)
                logging.info("BigQuery client initialized successfully.")
                
                # Specify the table reference
                table_ref = f"{project_id}.{dataset_id}.{table_id}"
                table = client.get_table(table_ref)
                logging.info(f"Table reference obtained: {table_ref}")
                
                # Insert form data into the table
                rows_to_insert = [user_data]
                errors = client.insert_rows_json(table, rows_to_insert)
                
                if errors:
                    logging.error(f"Errors occurred while inserting rows: {errors}")
                    st.error(f"Errors occurred while inserting rows: {errors}")
                else:
                    logging.info("Insert successful")
                    st.success("Insert successful")
            except Exception as e:
                logging.exception("An error occurred during the BigQuery insert operation.")
                st.error(f"An error occurred: {e}")

  

        project_id = "fynd-db"
        dataset_id = "finance_dwh"
        table_id = "plan_v2"
        fulfilling_location = st.session_state['fulfilling_location']
        application_id = st.session_state['application_id']
        # bundle_by = st.session_state['bundle_by']
        
        
        # Retrieve created_at, rule_id, and plan_id from session state
        created_at = st.session_state.get('created_at')
        rule_id = st.session_state.get('rule_id')
        plan_id = st.session_state.get('plan_id')

        # Prepare user_data for BigQuery with correct rule_id and plan_id
        user_data = {
            "Business_Head": business_head,
            "Company_ID": company_id if company_id and company_id.strip() else None,
            "Company_Name": company_name,
            "Currency": currency,
            "Bundle_by": bundle_by,
            "Plan_Name": plan_name,
            "Plan_Description": plan_description,
            "Product_lines": selected_ordering_channel,
            "Fulfilling_Location": fulfilling_location,
            "Application_ID": application_id,
            "Fee_Type": selected_fee_type,
            "Variable_Type": selected_variable_type,
            "Chargeable_on": selected_chargeable_on,
            "Fee_Nature": 0,
            "Commercial_value": user_input_3,
            "Fee_reversal": 0,
            "Reversal_%": 0,
            "Usage_limit": usage_3,
            "Threshold_value": 0,
            "Threshold_option": 0,
            "Expected_Billing": user_input_3,
            "Payment_Method": selected_payment_method,
            "Plan_Validity": selected_plan_validity,
            "rule_id": rule_id,
            "plan_id": plan_id,
            "created_at": created_at,
            "Net_total_billing": int(final_billing)
        }
        

        save_user_data(user_data)

        service_account_info = {
            "type": "service_account",
            "project_id": "fynd-db",
            "private_key_id": "48954327ef172acc3091a91881840dc353ff0413",
            "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCmIs0vgXflZmHD\n0IeHCiDEXavRb2GvRQqkCZGLy/EHFO7XLmpNrFzZHTSQach6HErARWTMsd2AMIA5\nHbmjEyt9deTOMUmICszkwNwpn6pzuoF2v4+fL2PQ4VBElLfCv4qbP9JGljvLsj5b\nC9UWe9rWgRrw2Z88LlVmFy3k4Om80Q57gW3hSYZpG4rBhC9+YRXjZ+Iq5SFEJQ+U\nj8ej7q6DnI8Prbvw/e13JH1pxm9AUQDm+sTsBmvjh4Ye02vKbfUP+pFOM0eC3Eoa\nOyG7unZnDuAaK9g87xVjd9HtD6MfaM0EHAEF1eyC4UqeiXTbVMwi7+0IGA2evEDZ\n3WRR2i+lAgMBAAECggEAF7Vis7NVsVqFfCS/moFTAMLfWLF87r67EIK/DwSpHloZ\n5YJdsDz3ORKmZC98aRSthDfC9UUyt270dXItAj0jmTFgWB6HgE8OQ1zUbmo3MHG+\ntPwMWmqwY2gsBMV2Xefot1QJxYH+AYkrxeFv5NgC+FaPSiy8QSHZlQqcxYtlP8kY\nzv8QFMgZ3b0sixK+YcSjCRXHu/DznnS/ZU7XlTALxANa50hT11rVLq6zYDF40fqC\nOWCOZvupjMOnNimXCWn7q6D1F8S/IMYlnHArJpEb7SdbJnJzxUvJC2rrMSoY896r\nyFEVhlElWp5GfTopu0WnL7iaw3pukM1OtpanFAk29wKBgQDW+yMzL7RiQ5LdElVP\n7Ml5hs+t6cjrUJTgBI8Uf9qGxE+tMe7Ye1fyJfbYslbOYO9xIUUT1jJmU6jadNWX\nA7UT59/IjvAOBIYyMEdh6Xo+H6ggW8DeU1HSdvixnQdgi0c03E6OCbOKi9E/UzK/\nTK2RBJJLN4MCiNL0FzrhTuxyEwKBgQDF1cz9Sb2UbrL5hr8LdcEH0ctatNlhJcSN\nnMd0Fn+e1EAkip+Hs9HmTvNdty4gQE+yEq/Tj0cRHXmjzK2AYx0WYRAhWGNSTlTS\n4OJdjE6vsoPu7Ouz1R/i/egKniUifEXYSgyL+oPBUrqqsnk9lgGBkJipN3u1f9+K\niuUJJP/OZwKBgF6ZvqCcom0HPU5I7f+wu+vdVfA6yy45lHmLqAamSFw7cLBPI8Jh\nbI7jA9/Rgn9oipUmxcX34M/Eiq4u8Xp1qC4tP/16YMpaVU8qjY7ZdfB2b75lgdaT\npZLOxZsq9X8Xauso8uxv+nDCG/8YtmEV9d61u0acE+t+mA3PVxqkZ0m/AoGBAI5c\nY76AqeN+JVxaEm/0tIsj9Om46hR2URJ2lzB6YCuzINUqy9GjHJBWj9oITzD2FmNV\n/yCGIeW3CClOyCtzJyNLhYf5Sr+XjoKRQVN/+7+C/l2YL6Sg4Ok/PRMm6iH+u2QB\nJTY1d0pOdfUPqR8gKsVJgBGE04iwE/RmLpp9/XZRAoGAFtaa9JeHk9dmZiST3lev\ns5syeOK1cCj1+nJer/GPEjqWMKxbCULvoEYBp7RaoEXjfGi7P4Mc6YHnbljHc+sL\niJzHhxrv/HYZdlCdTU3IRWT0sNHzqZemfYnsVTSp+4y61sGDxioDYL4CLnMq5T2F\nqzfc2qp3s8ApVloY3giuqnI=\n-----END PRIVATE KEY-----\n",
            "client_email": "plan-maker@fynd-db.iam.gserviceaccount.com",
            "client_id": "114883060650442183907",
            "auth_uri": "https://accounts.google.com/o/oauth2/auth",
            "token_uri": "https://oauth2.googleapis.com/token",
            "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
            "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/plan-maker%40fynd-db.iam.gserviceaccount.com",
            "universe_domain": "googleapis.com"
        }

        insert_into_bq(user_data, project_id, dataset_id, table_id, service_account_info)

st.write(" ")
st.write(" ")
st.write(" ")
st.write(" ")

st.write(" ")
st.write(" ")
st.write(" ")
st.write(" ")


# #Sidebar with an iframe
# st.sidebar.markdown("""
#     <style>
#         .left-aligned-iframe {
#             display: flex;
#             justify-content: flex-start;
#             width: 100%;
#             margin-top: 20px; /* Add a margin above the iframe */
#         }
#         .left-aligned-iframe iframe {
#             width: 100%; /* Adjust width as needed */
#             height: 800px; /* Adjust height as needed */
#             border: none;
#             border-radius: 15px;
#         }
#     </style>
#     <div class="left-aligned-iframe">
#         <iframe src="https://platform.fynd.com/blog" title="Fynd Platform"></iframe>
#     </div>
#     """, unsafe_allow_html=True)
# Start a tunnel to the correct port

# Connect to a port (e.g., 8501) without a custom subdomain
# try:
#     ngrok_tunnel = ngrok.connect(8501)  # Replace 8501 with your port number
#     print(f"ngrok tunnel URL: {ngrok_tunnel.public_url}")
# except Exception as e:
#     print(f"Failed to start ngrok tunnel: {e}")