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
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
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
# Importing necessary libraries
import streamlit as st

st.set_page_config(
    page_title="Scenario Planner",
    page_icon="⚖️",
    layout="wide",
    initial_sidebar_state="collapsed",
)

# Disable +/- for number input
st.markdown(
    """

<style>

    button.step-up {display: none;}

    button.step-down {display: none;}

    div[data-baseweb] {border-radius: 4px;}

</style>""",
    unsafe_allow_html=True,
)

import re
import sys
import copy
import pickle
import traceback
import numpy as np
import pandas as pd
from scenario import numerize
import plotly.graph_objects as go
from post_gres_cred import db_cred
from scipy.optimize import minimize
from log_application import log_message
from utilities import project_selection, update_db, set_header, load_local_css
from utilities import (
    get_panels_names,
    get_metrics_names,
    name_formating,
    load_rcs_metadata_files,
    load_scenario_metadata_files,
    generate_rcs_data,
    generate_scenario_data,
)
from constants import (
    xtol_tolerance_per,
    mroi_threshold,
    word_length_limit_lower,
    word_length_limit_upper,
)


schema = db_cred["schema"]
load_local_css("styles.css")
set_header()

# Initialize project name session state
if "project_name" not in st.session_state:
    st.session_state["project_name"] = None

# Fetch project dictionary
if "project_dct" not in st.session_state:
    project_selection()
    st.stop()

# Display Username and Project Name
if "username" in st.session_state and st.session_state["username"] is not None:

    cols1 = st.columns([2, 1])

    with cols1[0]:
        st.markdown(f"**Welcome {st.session_state['username']}**")
    with cols1[1]:
        st.markdown(f"**Current Project: {st.session_state['project_name']}**")

# Initialize ROI threshold
if "roi_threshold" not in st.session_state:
    st.session_state.roi_threshold = 1

# Initialize message display holder
if "message_display" not in st.session_state:
    st.session_state.message_display = {"type": "success", "message": None, "icon": ""}


# Function to reset modified_scenario_data
def reset_scenario(metrics_selected=None, panel_selected=None):
    # Clear message_display
    st.session_state.message_display = {"type": "success", "message": None, "icon": ""}

    # Use default values from session state if not provided
    if metrics_selected is None:
        metrics_selected = st.session_state["response_metrics_selectbox_sp"]
    if panel_selected is None:
        panel_selected = st.session_state["panel_selected_selectbox_sp"]

    # Load original scenario data
    original_data = st.session_state["project_dct"]["scenario_planner"][
        "original_metadata_file"
    ]
    original_scenario_data = original_data[metrics_selected][panel_selected]

    # Load modified scenario data
    data = st.session_state["project_dct"]["scenario_planner"]["modified_metadata_file"]

    # Update the specific section with the original scenario data
    data[metrics_selected][panel_selected] = copy.deepcopy(original_scenario_data)
    st.session_state["project_dct"]["scenario_planner"]["modified_metadata_file"] = data


# Function to build s curve
def s_curve(x, power, K, b, a, x0):
    return K / (1 + b * np.exp(-a * ((x / 10**power) - x0)))


# Function to retrieve S-curve parameters for a given metric, panel, and channel
def get_s_curve_params(

    metrics_selected,

    panel_selected,

    channel_selected,

    original_rcs_data,

    modified_rcs_data,

):
    # Retrieve 'power' parameter from the original data for the specific metric, panel, and channel
    power = original_rcs_data[metrics_selected][panel_selected][channel_selected][
        "power"
    ]

    # Get the S-curve parameters from the modified data for the same metric, panel, and channel
    s_curve_param = modified_rcs_data[metrics_selected][panel_selected][
        channel_selected
    ]

    # Load modified scenario metadata
    data = st.session_state["project_dct"]["scenario_planner"]["modified_metadata_file"]

    # Update modified S-curve parameters
    data[metrics_selected][panel_selected]["channels"][channel_selected][
        "response_curve_params"
    ] = s_curve_param

    # Update modified scenario metadata
    st.session_state["project_dct"]["scenario_planner"]["modified_metadata_file"] = data

    # Update the 'power' parameter in the modified S-curve parameters with the original 'power' value
    s_curve_param["power"] = power

    # Return the updated S-curve parameters
    return s_curve_param


# Function to calculate total contribution
def get_total_contribution(

    spends, channels, s_curve_params, channels_proportion, modified_scenario_data

):
    total_contribution = 0
    for i in range(len(channels)):
        channel_name = channels[i]
        channel_s_curve_params = s_curve_params[channel_name]
        spend_proportion = spends[i] * channels_proportion[channel_name]
        total_contribution += sum(
            s_curve(
                spend_proportion,
                channel_s_curve_params["power"],
                channel_s_curve_params["K"],
                channel_s_curve_params["b"],
                channel_s_curve_params["a"],
                channel_s_curve_params["x0"],
            )
        ) + sum(
            modified_scenario_data["channels"][channel_name]["correction"]
        )  # correction for s-curve
    return total_contribution + sum(modified_scenario_data["constant"])


# Function to calculate total spends
def get_total_spends(spends, channels_conversion_ratio):
    return np.sum(spends * np.array(list(channels_conversion_ratio.values())))


# Function to optimizes spends for all channels given bounds and a total spend target
def optimizer(

    optimization_goal,

    s_curve_params,

    channels_spends,

    channels_proportion,

    channels_conversion_ratio,

    total_target,

    bounds_dict,

    modified_scenario_data,

):
    # Extract channel names and corresponding actual spends
    channels = list(channels_spends.keys())
    actual_spends = np.array(list(channels_spends.values()))
    num_channels = len(actual_spends)

    # Define the objective function based on the optimization goal
    def objective_fun(spends):
        if optimization_goal == "Spend":
            # Minimize negative total contribution to maximize the total contribution
            return -get_total_contribution(
                spends,
                channels,
                s_curve_params,
                channels_proportion,
                modified_scenario_data,
            )
        else:
            # Minimize total spends
            return get_total_spends(spends, channels_conversion_ratio)

    def constraint_fun(spends):
        if optimization_goal == "Spend":
            # Ensure the total spends equals the total spend target
            return get_total_spends(spends, channels_conversion_ratio)
        else:
            # Ensure the total contribution equals the total contribution target
            return get_total_contribution(
                spends,
                channels,
                s_curve_params,
                channels_proportion,
                modified_scenario_data,
            )

    # Equality constraint
    constraints = {
        "type": "eq",
        "fun": lambda spends: constraint_fun(spends) - total_target,
    }  # Sum of all channel spends/metrics should equal the total spend/metrics target

    # Bounds for each channel's spend based
    bounds = [
        (
            actual_spends[i] * (1 + bounds_dict[channels[i]][0] / 100),
            actual_spends[i] * (1 + bounds_dict[channels[i]][1] / 100),
        )
        for i in range(num_channels)
    ]

    # Initial guess for the optimization
    initial_guess = np.array(actual_spends)

    # Calculate xtol as n% of the minimum of spends
    xtol = max(10, (xtol_tolerance_per / 100) * np.min(actual_spends))

    # Perform the optimization using 'trust-constr' method
    result = minimize(
        objective_fun,
        initial_guess,
        method="trust-constr",
        constraints=constraints,
        bounds=bounds,
        options={
            "disp": True,  # Display the optimization process
            "xtol": xtol,  # Dynamic step size tolerance
            "maxiter": 1e5,  # Maximum number of iterations
        },
    )

    # Extract the optimized spends from the result
    optimized_spends_array = result.x

    # Convert optimized spends back to a dictionary with channel names
    optimized_spends = {
        channels[i]: max(0, optimized_spends_array[i]) for i in range(num_channels)
    }

    return optimized_spends, result.success


# Function to calculate achievable targets at lower and upper spend bounds
@st.cache_data(show_spinner=False)
def max_target_achievable(

    channels_spends,

    s_curve_params,

    channels_proportion,

    modified_scenario_data,

    bounds_dict,

):
    # Extract channel names and corresponding actual spends
    channels = list(channels_spends.keys())
    actual_spends = np.array(list(channels_spends.values()))
    num_channels = len(actual_spends)

    # Bounds for each channel's spend
    lower_spends, upper_spends = [], []
    for i in range(num_channels):
        lower_spends.append(actual_spends[i] * (1 + bounds_dict[channels[i]][0] / 100))
        upper_spends.append(actual_spends[i] * (1 + bounds_dict[channels[i]][1] / 100))

    # Calculate achievable targets at lower and upper spend bounds
    lower_achievable_target = get_total_contribution(
        lower_spends,
        channels,
        s_curve_params,
        channels_proportion,
        modified_scenario_data,
    )
    upper_achievable_target = get_total_contribution(
        upper_spends,
        channels,
        s_curve_params,
        channels_proportion,
        modified_scenario_data,
    )

    # Return achievable targets with ±0.1% safety margin
    return max(0, 1.001 * lower_achievable_target), 0.999 * upper_achievable_target


# Function to check if number is in valid format
def is_valid_number_format(number_str):
    # Check for None
    if number_str is None:
        # Store the message details in session state for invalid input
        st.session_state.message_display = {
            "type": "warning",
            "message": "Invalid input: Please enter a valid number.",
            "icon": "⚠️",
        }
        return False

    # Define the valid suffixes
    valid_suffixes = {"K", "M", "B", "T"}

    # Check for negative numbers
    if number_str[0] == "-":
        # Store the message details in session state for invalid input
        st.session_state.message_display = {
            "type": "warning",
            "message": "Invalid input: Please enter a valid number.",
            "icon": "⚠️",
        }
        return False

    # Check if the string ends with a digit
    if number_str[-1].isdigit():
        try:
            # Attempt to convert the entire string to float
            number = float(number_str)
            # Ensure the number is non-negative
            if number >= 0:
                return True
            else:
                # Store the message details in session state for invalid input
                st.session_state.message_display = {
                    "type": "warning",
                    "message": "Invalid input: Please enter a valid number.",
                    "icon": "⚠️",
                }
                return False
        except ValueError:
            # Store the message details in session state for invalid input
            st.session_state.message_display = {
                "type": "warning",
                "message": "Invalid input: Please enter a valid number.",
                "icon": "⚠️",
            }
            return False

    # Check if the string ends with a valid suffix
    suffix = number_str[-1].upper()
    if suffix in valid_suffixes:
        num_part = number_str[:-1]  # Extract the numerical part
        try:
            # Attempt to convert the numerical part to float
            number = float(num_part)
            # Ensure the number part is non-negative
            if number >= 0:
                return True
            else:
                # Store the message details in session state for invalid input
                st.session_state.message_display = {
                    "type": "warning",
                    "message": "Invalid input: Please enter a valid number.",
                    "icon": "⚠️",
                }
                return False
        except ValueError:
            # Store the message details in session state for invalid input
            st.session_state.message_display = {
                "type": "warning",
                "message": "Invalid input: Please enter a valid number.",
                "icon": "⚠️",
            }
            return False

    # If neither condition is met, return False
    st.session_state.message_display = {
        "type": "warning",
        "message": "Invalid input: Please enter a valid number.",
        "icon": "⚠️",
    }
    return False


# Function to converts a string with number suffixes (K, M, B, T) to a float
def convert_to_float(number_str):
    # Dictionary mapping suffixes to their multipliers
    multipliers = {
        "K": 1e3,  # Thousand
        "M": 1e6,  # Million
        "B": 1e9,  # Billion
        "T": 1e12,  # Trillion
    }

    # If there's no suffix, directly convert to float
    if number_str[-1].isdigit():
        return float(number_str)

    # Extract the suffix (last character) and the numerical part
    suffix = number_str[-1].upper()
    num_part = number_str[:-1]

    # Convert the numerical part to float and multiply by the corresponding multiplier
    return float(num_part) * multipliers[suffix]


# Function to update absolute_channel_spends change
def absolute_channel_spends_change(

    channel_key, channel_spends_actual, channel, metrics_selected, panel_selected

):
    # Do not update if the number is in an invalid format
    if not is_valid_number_format(st.session_state[f"{channel_key}_abs_spends_key"]):
        return

    # Get updated absolute spends from session state
    new_absolute_spends = (
        convert_to_float(st.session_state[f"{channel_key}_abs_spends_key"])
        * st.session_state["multiplier"]
    )

    # Load modified scenario data
    data = st.session_state["project_dct"]["scenario_planner"]["modified_metadata_file"]

    # Total channel spends
    total_channel_spends = 0
    for current_channel in list(
        data[metrics_selected][panel_selected]["channels"].keys()
    ):
        # Channel key
        channel_key = f"{metrics_selected}_{panel_selected}_{current_channel}"

        total_channel_spends += (
            convert_to_float(st.session_state[f"{channel_key}_abs_spends_key"])
            * st.session_state["multiplier"]
        )

    # Check if total channel spends are within the allowed range (±50% of the original total spends)
    if (
        total_channel_spends
        < 1.5 * data[metrics_selected][panel_selected]["actual_total_spends"]
        and total_channel_spends
        > 0.5 * data[metrics_selected][panel_selected]["actual_total_spends"]
    ):
        # Update the modified_total_spends for the specified channel
        data[metrics_selected][panel_selected]["channels"][channel][
            "modified_total_spends"
        ] = new_absolute_spends / float(
            data[metrics_selected][panel_selected]["channels"][channel][
                "conversion_rate"
            ]
        )

        # Update total spends
        data[metrics_selected][panel_selected][
            "modified_total_spends"
        ] = total_channel_spends

        # Update modified scenario metadata
        st.session_state["project_dct"]["scenario_planner"][
            "modified_metadata_file"
        ] = data

    else:
        # Store the message details in session state
        st.session_state.message_display = {
            "type": "warning",
            "message": "Keep total spending within ±50% of the original value.",
            "icon": "⚠️",
        }


# Function to update percentage_channel_spends change
def percentage_channel_spends_change(

    channel_key, channel_spends_actual, channel, metrics_selected, panel_selected

):
    # Retrieve the percentage spend change from session state
    percentage_channel_spends = round(
        st.session_state[f"{channel_key}_per_spends_key"], 0
    )

    # Calculate the new absolute spends
    new_absolute_spends = channel_spends_actual * (1 + percentage_channel_spends / 100)

    # Load modified scenario data
    data = st.session_state["project_dct"]["scenario_planner"]["modified_metadata_file"]

    # Total channel spends
    total_channel_spends = 0
    for current_channel in list(
        data[metrics_selected][panel_selected]["channels"].keys()
    ):
        # Channel key
        channel_key = f"{metrics_selected}_{panel_selected}_{current_channel}"

        # Current channel spends actual
        current_channel_spends_actual = data[metrics_selected][panel_selected][
            "channels"
        ][current_channel]["actual_total_spends"]

        # Current channel conversion rate
        current_channel_conversion_rate = data[metrics_selected][panel_selected][
            "channels"
        ][current_channel]["conversion_rate"]

        # Calculate the current channel absolute spends
        current_channel_absolute_spends = (
            current_channel_spends_actual
            * current_channel_conversion_rate
            * (1 + st.session_state[f"{channel_key}_per_spends_key"] / 100)
        )

        total_channel_spends += current_channel_absolute_spends

    # Check if total channel spends are within the allowed range (±50% of the original total spends)
    if (
        total_channel_spends
        < 1.5 * data[metrics_selected][panel_selected]["actual_total_spends"]
        and total_channel_spends
        > 0.5 * data[metrics_selected][panel_selected]["actual_total_spends"]
    ):
        # Update the modified_total_spends for the specified channel
        data[metrics_selected][panel_selected]["channels"][channel][
            "modified_total_spends"
        ] = float(new_absolute_spends) / float(
            data[metrics_selected][panel_selected]["channels"][channel][
                "conversion_rate"
            ]
        )

        # Update total spends
        data[metrics_selected][panel_selected][
            "modified_total_spends"
        ] = total_channel_spends

        # Update modified scenario metadata
        st.session_state["project_dct"]["scenario_planner"][
            "modified_metadata_file"
        ] = data

    else:
        # Store the message details in session state
        st.session_state.message_display = {
            "type": "warning",
            "message": "Keep total spending within ±50% of the original value.",
            "icon": "⚠️",
        }


# # Function to update total input change
# def total_input_change(per_change):
#     # Load modified scenario data
#     data = st.session_state["project_dct"]["scenario_planner"]["modified_metadata_file"]

#     # Get the list of all channels in the specified panel and metric
#     channel_list = list(data[metrics_selected][panel_selected]["channels"].keys())

#     # Iterate over each channel to update their modified spends
#     for channel in channel_list:
#         # Retrieve the actual spends for the channel
#         channel_actual_spends = data[metrics_selected][panel_selected]["channels"][
#             channel
#         ]["actual_total_spends"]

#         # Calculate the modified spends for the channel based on the percent change
#         modified_channel_metrics = channel_actual_spends * ((100 + per_change) / 100)

#         # Update the channel's modified total spends in the data
#         data[metrics_selected][panel_selected]["channels"][channel][
#             "modified_total_spends"
#         ] = modified_channel_metrics

#         # Update modified scenario metadata
#         st.session_state["project_dct"]["scenario_planner"][
#             "modified_metadata_file"
#         ] = data


# Function to update total input change
def total_input_change(per_change, metrics_selected, panel_selected):
    # Load modified and original scenario data
    modified_data = st.session_state["project_dct"]["scenario_planner"][
        "modified_metadata_file"
    ].copy()
    original_data = st.session_state["project_dct"]["scenario_planner"][
        "original_metadata_file"
    ].copy()

    # Get the list of all channels in the selected panel and metric
    channel_list = list(
        modified_data[metrics_selected][panel_selected]["channels"].keys()
    )

    # Separate channels into unfrozen and frozen based on optimization status
    unfrozen_channels, frozen_channels = [], []
    for channel in channel_list:
        channel_key = f"{metrics_selected}_{panel_selected}_{channel}"
        if st.session_state.get(f"{channel_key}_allow_optimize_key", False):
            frozen_channels.append(channel)
        else:
            unfrozen_channels.append(channel)

    # Calculate spends and total share from frozen channels, weighted by conversion rate
    frozen_channel_share, frozen_channel_spends = 0, 0
    for channel in frozen_channels:
        conversion_rate = original_data[metrics_selected][panel_selected]["channels"][
            channel
        ]["conversion_rate"]
        actual_spends = original_data[metrics_selected][panel_selected]["channels"][
            channel
        ]["actual_total_spends"]
        modified_spends = modified_data[metrics_selected][panel_selected]["channels"][
            channel
        ]["modified_total_spends"]
        spends_diff = max(actual_spends, 1e-3) * ((100 + per_change) / 100) - max(
            modified_spends, 1e-3
        )
        frozen_channel_share += spends_diff * conversion_rate
        frozen_channel_spends += max(actual_spends, 1e-3) * conversion_rate

    # Redistribute frozen share across unfrozen channels based on original spend weights
    for channel in unfrozen_channels:
        conversion_rate = original_data[metrics_selected][panel_selected]["channels"][
            channel
        ]["conversion_rate"]
        actual_spends = original_data[metrics_selected][panel_selected]["channels"][
            channel
        ]["actual_total_spends"]

        # Calculate weight of the current channel's original spends
        total_original_spends = original_data[metrics_selected][panel_selected][
            "actual_total_spends"
        ]
        channel_weight = (actual_spends * conversion_rate) / (
            total_original_spends - frozen_channel_spends
        )

        # Calculate the modified spends with redistributed frozen share
        modified_spends = (
            max(actual_spends, 1e-3) * ((100 + per_change) / 100)
            + (frozen_channel_share * channel_weight) / conversion_rate
        )

        # Update modified total spends in the modified data
        modified_data[metrics_selected][panel_selected]["channels"][channel][
            "modified_total_spends"
        ] = modified_spends

    # Save the updated modified scenario data back to the session state
    st.session_state["project_dct"]["scenario_planner"][
        "modified_metadata_file"
    ] = modified_data


# Function to update total_absolute_main_key change
def total_absolute_main_key_change(metrics_selected, panel_selected, optimization_goal):
    # Do not update if the number is in an invalid format
    if not is_valid_number_format(st.session_state["total_absolute_main_key"]):
        return

    # Get updated absolute from session state
    new_absolute = (
        convert_to_float(st.session_state["total_absolute_main_key"])
        * st.session_state["multiplier"]
    )

    # Load modified scenario data
    data = st.session_state["project_dct"]["scenario_planner"]["modified_metadata_file"]

    if optimization_goal == "Spend":
        # Retrieve the old absolute spends
        old_absolute = data[metrics_selected][panel_selected]["actual_total_spends"]
    else:
        # Retrieve the old absolute metrics
        old_absolute = data[metrics_selected][panel_selected]["actual_total_sales"]

    # Calculate the allowable range for new spends
    lower_bound = old_absolute * 0.5
    upper_bound = old_absolute * 1.5

    # Ensure the new spends are within ±50% of the old value
    if new_absolute < lower_bound or new_absolute > upper_bound:
        new_absolute = old_absolute

        # Store the message details in session state
        st.session_state.message_display = {
            "type": "warning",
            "message": "Keep total spending within ±50% of the original value.",
            "icon": "⚠️",
        }

    if optimization_goal == "Spend":
        # Update the modified_total_spends with the constrained value
        data[metrics_selected][panel_selected]["modified_total_spends"] = new_absolute
    else:
        # Update the modified_total_sales with the constrained value
        data[metrics_selected][panel_selected]["modified_total_sales"] = new_absolute

    # Update modified scenario metadata
    st.session_state["project_dct"]["scenario_planner"]["modified_metadata_file"] = data

    # Update total input change
    if optimization_goal == "Spend":
        per_change = ((new_absolute - old_absolute) / old_absolute) * 100
        total_input_change(per_change, metrics_selected, panel_selected)


# Function to update total_absolute_key change
def total_absolute_key_change(metrics_selected, panel_selected, optimization_goal):
    # Get updated absolute from session state
    new_absolute = (
        convert_to_float(st.session_state["total_absolute_key"])
        * st.session_state["multiplier"]
    )

    # Load modified scenario data
    data = st.session_state["project_dct"]["scenario_planner"]["modified_metadata_file"]

    if optimization_goal == "Spend":
        # Update the modified_total_spends for the specified channel
        data[metrics_selected][panel_selected]["modified_total_spends"] = new_absolute
        old_absolute = data[metrics_selected][panel_selected]["actual_total_spends"]
    else:
        # Update the modified_total_sales for the specified channel
        data[metrics_selected][panel_selected]["modified_total_sales"] = new_absolute
        old_absolute = data[metrics_selected][panel_selected]["actual_total_sales"]

    # Update modified scenario metadata
    st.session_state["project_dct"]["scenario_planner"]["modified_metadata_file"] = data

    # Update total input change
    if optimization_goal == "Spend":
        per_change = ((new_absolute - old_absolute) / old_absolute) * 100
        total_input_change(per_change, metrics_selected, panel_selected)


# Function to update total_absolute_key change
def total_percentage_key_change(

    metrics_selected,

    panel_selected,

    absolute_value,

    optimization_goal,

):
    # Get updated absolute from session state
    new_absolute = absolute_value * (1 + st.session_state["total_percentage_key"] / 100)

    # Load modified scenario data
    data = st.session_state["project_dct"]["scenario_planner"]["modified_metadata_file"]

    if optimization_goal == "Spend":
        # Update the modified_total_spends for the specified channel
        data[metrics_selected][panel_selected]["modified_total_spends"] = new_absolute
        old_absolute = data[metrics_selected][panel_selected]["actual_total_spends"]
    else:
        # Update the modified_total_sales for the specified channel
        data[metrics_selected][panel_selected]["modified_total_sales"] = new_absolute
        old_absolute = data[metrics_selected][panel_selected]["actual_total_sales"]

    # Update modified scenario metadata
    st.session_state["project_dct"]["scenario_planner"]["modified_metadata_file"] = data

    # Update total input change
    if optimization_goal == "Spend":
        per_change = ((new_absolute - old_absolute) / old_absolute) * 100
        total_input_change(per_change, metrics_selected, panel_selected)


# Function to update bound change
def bound_change(metrics_selected, panel_selected, channel_key, channel):
    # Get updated bounds from session state
    new_lower_bound = st.session_state[f"{channel_key}_lower_key"]
    new_upper_bound = st.session_state[f"{channel_key}_upper_key"]
    if new_lower_bound > new_upper_bound:
        new_bounds = [-10, 10]

        # Store the message details in session state
        st.session_state.message_display = {
            "type": "warning",
            "message": "Lower bound cannot be greater than Upper bound.",
            "icon": "⚠️",
        }

    else:
        new_bounds = [new_lower_bound, new_upper_bound]

    # Load modified scenario data
    data = st.session_state["project_dct"]["scenario_planner"]["modified_metadata_file"]

    # Update the bounds for the specified channel
    data[metrics_selected][panel_selected]["channels"][channel]["bounds"] = new_bounds

    # Update modified scenario metadata
    st.session_state["project_dct"]["scenario_planner"]["modified_metadata_file"] = data


# Function to update freeze change
def freeze_change(metrics_selected, panel_selected, channel_key, channel, channel_list):
    # Initialize counter for channels that are not frozen
    unfrozen_channel_count = 0

    # Check the optimization status of each channel
    for current_channel in channel_list:
        current_channel_key = f"{metrics_selected}_{panel_selected}_{current_channel}"
        unfrozen_channel_count += (
            1
            if not st.session_state[f"{current_channel_key}_allow_optimize_key"]
            else 0
        )

    # Ensure at least two channels are allowed for optimization
    if unfrozen_channel_count < 2:
        st.session_state.message_display = {
            "type": "warning",
            "message": "Please allow at least two channels to be optimized.",
            "icon": "⚠️",
        }
        return

    if st.session_state[f"{channel_key}_allow_optimize_key"]:
        # Updated bounds from session state
        new_lower_bound, new_upper_bound = 0, 0
        new_bounds = [new_lower_bound, new_upper_bound]
        new_freeze = True
    else:
        # Updated bounds from session state
        new_lower_bound, new_upper_bound = -10, 10
        new_bounds = [new_lower_bound, new_upper_bound]
        new_freeze = False

    # Load modified scenario data
    data = st.session_state["project_dct"]["scenario_planner"]["modified_metadata_file"]

    # Update the bounds for the specified channel
    data[metrics_selected][panel_selected]["channels"][channel]["bounds"] = new_bounds
    data[metrics_selected][panel_selected]["channels"][channel]["freeze"] = new_freeze

    # Update modified scenario metadata
    st.session_state["project_dct"]["scenario_planner"]["modified_metadata_file"] = data


# Function to calculate y, ROI and MROI for given point
def get_point_parms(

    x_val,

    current_s_curve_params,

    current_channel_proportion,

    current_conversion_rate,

    channel_correction,

):
    # Calculate y value for the given spend point
    y_val = (
        sum(
            s_curve(
                (x_val * current_channel_proportion),
                current_s_curve_params["power"],
                current_s_curve_params["K"],
                current_s_curve_params["b"],
                current_s_curve_params["a"],
                current_s_curve_params["x0"],
            )
        )
        + channel_correction
    )

    # Calculate MROI using a small nudge for actual spends
    nudge = 1e-3
    x1 = float(x_val * current_conversion_rate)
    y1 = float(y_val)
    x2 = x1 + nudge
    y2 = (
        sum(
            s_curve(
                ((x2 / current_conversion_rate) * current_channel_proportion),
                current_s_curve_params["power"],
                current_s_curve_params["K"],
                current_s_curve_params["b"],
                current_s_curve_params["a"],
                current_s_curve_params["x0"],
            )
        )
        + channel_correction
    )
    mroi_val = (float(y2) - y1) / (x2 - x1) if x2 != x1 else 0

    # Calculate ROI
    roi_val = y_val / (x_val * current_conversion_rate)

    return roi_val, mroi_val, y_val


# Function to find segment value
def find_segment_value(x, roi, mroi, roi_threshold=1, mroi_threshold=0.05):
    # Initialize the start and end values of the x array
    start_value = x[0]
    end_value = x[-1]

    # Define the condition for the "green region" where both ROI and MROI exceed their respective thresholds
    green_condition = (roi > roi_threshold) & (mroi > mroi_threshold)

    # Find indices where ROI exceeds the ROI threshold
    left_indices = np.where(roi > roi_threshold)[0]

    # Find indices where both ROI and MROI exceed their thresholds (green condition)
    right_indices = np.where(green_condition)[0]

    # Determine the left value based on the first index where ROI exceeds the threshold
    left_value = x[left_indices[0]] if left_indices.size > 0 else x[0]

    # Determine the right value based on the last index where both ROI and MROI exceed their thresholds
    right_value = x[right_indices[-1]] if right_indices.size > 0 else x[0]

    # Ensure the left value does not exceed the right value, adjust if necessary
    if left_value > right_value:
        left_value = right_value

    return start_value, end_value, left_value, right_value


# Function to generate response curves plots
@st.cache_data(show_spinner=False)
def generate_response_curve_plots(

    channel_list,

    s_curve_params,

    channels_proportion,

    original_scenario_data,

    multiplier,

):
    figures, channel_roi_mroi, region_start_end = [], {}, {}

    for channel in channel_list:
        spends_actual = original_scenario_data["channels"][channel][
            "actual_total_spends"
        ]
        conversion_rate = original_scenario_data["channels"][channel]["conversion_rate"]
        channel_correction = sum(
            original_scenario_data["channels"][channel]["correction"]
        )

        x_actual = np.linspace(0, 5 * spends_actual, 100)
        x_plot = x_actual * conversion_rate

        # Calculate y values for the S-curve
        y_plot = [
            sum(
                s_curve(
                    (x * channels_proportion[channel]),
                    s_curve_params[channel]["power"],
                    s_curve_params[channel]["K"],
                    s_curve_params[channel]["b"],
                    s_curve_params[channel]["a"],
                    s_curve_params[channel]["x0"],
                )
            )
            + channel_correction
            for x in x_actual
        ]

        # Calculate ROI and ensure they are scalar values
        roi = [float(y) / float(x) if x != 0 else 0 for x, y in zip(x_plot, y_plot)]

        # Calculate MROI using a small nudge
        nudge = 1e-3
        mroi = []
        for i in range(len(x_plot)):
            x1 = float(x_plot[i])
            y1 = float(y_plot[i])
            x2 = x1 + nudge
            y2 = (
                sum(
                    s_curve(
                        ((x2 / conversion_rate) * channels_proportion[channel]),
                        s_curve_params[channel]["power"],
                        s_curve_params[channel]["K"],
                        s_curve_params[channel]["b"],
                        s_curve_params[channel]["a"],
                        s_curve_params[channel]["x0"],
                    )
                )
                + channel_correction
            )
            mroi_value = (float(y2) - y1) / (x2 - x1) if x2 != x1 else 0
            mroi.append(mroi_value)

        # Channel correction
        channel_correction = sum(
            original_scenario_data["channels"][channel]["correction"]
        )

        # Calculate y, ROI and MROI for the actual spend point
        roi_actual, mroi_actual, y_actual = get_point_parms(
            spends_actual,
            s_curve_params[channel],
            channels_proportion[channel],
            conversion_rate,
            channel_correction,
        )

        # Create the plotly figure
        fig = go.Figure()

        # Add S-curve line
        fig.add_trace(
            go.Scatter(
                x=np.array(x_plot) / multiplier,
                y=np.array(y_plot) / multiplier,
                mode="lines",
                name="Metrics",
                hoverinfo="text",
                text=[
                    f"Spends: {numerize(x / multiplier)}<br>{metrics_selected_formatted}: {numerize(y / multiplier)}<br>ROI: {r:.2f}<br>MROI: {m:.2f}"
                    for x, y, r, m in zip(x_plot, y_plot, roi, mroi)
                ],
            )
        )

        # Add current spend point
        fig.add_trace(
            go.Scatter(
                x=[spends_actual * conversion_rate / multiplier],
                y=[y_actual / multiplier],
                mode="markers",
                marker=dict(color="cyan", size=10, symbol="circle"),
                name="Actual Spend",
                hoverinfo="text",
                text=[
                    f"Actual Spend: {numerize(spends_actual * conversion_rate / multiplier)}<br>{metrics_selected_formatted}: {numerize(y_actual / multiplier)}<br>ROI: {roi_actual:.2f}<br>MROI: {mroi_actual:.2f}"
                ],
                showlegend=True,
            )
        )

        # ROI Threshold
        roi_threshold = st.session_state.roi_threshold

        # Scale x and y values
        x, y = np.array(x_plot), np.array(y_plot)
        x_scaled, y_scaled = x / max(x), y / max(y)

        # Calculate MROI scaled starting from the first point
        mroi_scaled = np.zeros_like(x_scaled)
        for j in range(1, len(x_scaled)):
            x1, y1 = x_scaled[j - 1], y_scaled[j - 1]
            x2, y2 = x_scaled[j], y_scaled[j]
            mroi_scaled[j] = (y2 - y1) / (x2 - x1) if (x2 - x1) != 0 else 0

        # Get the start_value, end_value, left_value, right_value for segments
        start_value, end_value, left_value, right_value = find_segment_value(
            x_plot, np.array(roi), mroi_scaled, roi_threshold, mroi_threshold
        )

        # Store region start and end points
        region_start_end[channel] = {
            "start_value": start_value,
            "end_value": end_value,
            "left_value": left_value,
            "right_value": right_value,
        }

        # Adding background colors
        y_max = max(y_plot) * 1.3  # 30% extra space above the max

        # Yellow region
        fig.add_shape(
            type="rect",
            x0=start_value / multiplier,
            y0=0,
            x1=left_value / multiplier,
            y1=y_max / multiplier,
            line=dict(width=0),
            fillcolor="rgba(255, 255, 0, 0.3)",
            layer="below",
        )

        # Green region
        fig.add_shape(
            type="rect",
            x0=left_value / multiplier,
            y0=0,
            x1=right_value / multiplier,
            y1=y_max / multiplier,
            line=dict(width=0),
            fillcolor="rgba(0, 255, 0, 0.3)",
            layer="below",
        )

        # Red region
        fig.add_shape(
            type="rect",
            x0=right_value / multiplier,
            y0=0,
            x1=end_value / multiplier,
            y1=y_max / multiplier,
            line=dict(width=0),
            fillcolor="rgba(255, 0, 0, 0.3)",
            layer="below",
        )

        # Layout adjustments
        fig.update_layout(
            title=f"{name_formating(channel)}",
            showlegend=False,
            xaxis=dict(
                showgrid=True,
                showticklabels=True,
                tickformat=".2s",
                gridcolor="lightgrey",
                gridwidth=0.5,
                griddash="dot",
            ),
            yaxis=dict(
                showgrid=True,
                showticklabels=True,
                tickformat=".2s",
                gridcolor="lightgrey",
                gridwidth=0.5,
                griddash="dot",
            ),
            template="plotly_white",
            margin=dict(l=20, r=20, t=30, b=20),
            height=100 * (len(channel_list) + 4 - 1) // 4,
        )

        figures.append(fig)

        # Store data of each channel ROI and MROI
        channel_roi_mroi[channel] = {
            "actual_roi": roi_actual,
            "actual_mroi": mroi_actual,
        }

    return figures, channel_roi_mroi, region_start_end


# Function to add modified spends/metrics point on plot
def modified_metrics_point(

    fig,

    modified_spends,

    s_curve_params,

    channels_proportion,

    conversion_rate,

    channel_correction,

):
    # Calculate ROI, MROI, and y for the modified point
    roi_modified, mroi_modified, y_modified = get_point_parms(
        modified_spends,
        s_curve_params,
        channels_proportion,
        conversion_rate,
        channel_correction,
    )

    # Add modified spend point
    fig.add_trace(
        go.Scatter(
            x=[modified_spends * conversion_rate / st.session_state["multiplier"]],
            y=[y_modified / st.session_state["multiplier"]],
            mode="markers",
            marker=dict(color="blueviolet", size=10, symbol="circle"),
            name="Optimized Spend",
            hoverinfo="text",
            text=[
                f"Modified Spend: {numerize(modified_spends * conversion_rate / st.session_state.multiplier)}<br>{metrics_selected_formatted}: {numerize(y_modified / st.session_state.multiplier)}<br>ROI: {roi_modified:.2f}<br>MROI: {mroi_modified:.2f}"
            ],
            showlegend=True,
        )
    )

    return roi_modified, mroi_modified, fig


# Function to update bound type change
def bound_type_change():
    # Get updated bound type from session state
    new_bound_type = st.session_state["bound_type_key"]

    # Load modified scenario data
    data = st.session_state["project_dct"]["scenario_planner"]["modified_metadata_file"]

    # Update the bound type
    data[metrics_selected][panel_selected]["bound_type"] = new_bound_type

    # Set bounds to default value if bound type is False (Default)
    channel_list = list(data[metrics_selected][panel_selected]["channels"].keys())
    if not new_bound_type:
        for channel in channel_list:
            data[metrics_selected][panel_selected]["channels"][channel]["bounds"] = [
                -10,
                10,
            ]

    # Update modified scenario metadata
    st.session_state["project_dct"]["scenario_planner"]["modified_metadata_file"] = data


# Function to format the numbers with decimal
def format_value(input_value):
    value = abs(input_value)
    return f"{input_value:.4f}" if value < 1 else f"{numerize(input_value, 1)}"


# Function to format the numbers with decimal
def round_value(input_value):
    value = abs(input_value)
    return round(input_value, 4) if value < 1 else round(input_value, 1)


# Function to generate ROI and MROI plots for all channels
@st.cache_data(show_spinner=False)
def roi_mori_plot(channel_roi_mroi):
    # Dictionary to store plots
    channel_roi_mroi_plot = {}
    for channel in channel_roi_mroi:
        channel_roi_mroi_data = channel_roi_mroi[channel]
        # Extract the data
        actual_roi = channel_roi_mroi_data["actual_roi"]
        optimized_roi = channel_roi_mroi_data["optimized_roi"]
        actual_mroi = channel_roi_mroi_data["actual_mroi"]
        optimized_mroi = channel_roi_mroi_data["optimized_mroi"]

        # Plot ROI
        fig_roi = go.Figure()
        fig_roi.add_trace(
            go.Bar(
                x=["Actual ROI"],
                y=[actual_roi],
                name="Actual ROI",
                marker_color="cyan",
                width=1,
                text=[format_value(actual_roi)],
                textposition="auto",
                textfont=dict(color="black", size=14),
            )
        )
        fig_roi.add_trace(
            go.Bar(
                x=["Optimized ROI"],
                y=[optimized_roi],
                name="Optimized ROI",
                marker_color="blueviolet",
                width=1,
                text=[format_value(optimized_roi)],
                textposition="auto",
                textfont=dict(color="black", size=14),
            )
        )

        fig_roi.update_layout(
            annotations=[
                dict(
                    x=0.5,
                    y=1.3,
                    xref="paper",
                    yref="paper",
                    text="ROI",
                    showarrow=False,
                    font=dict(size=14),
                )
            ],
            barmode="group",
            bargap=0,
            showlegend=False,
            width=110,
            height=110,
            xaxis=dict(
                showticklabels=True,
                showgrid=False,
                tickangle=0,
                ticktext=["Actual", "Optimized"],
                tickvals=["Actual ROI", "Optimized ROI"],
            ),
            yaxis=dict(showticklabels=False, showgrid=False),
            margin=dict(t=20, b=20, r=0, l=0),
        )

        # Plot MROI
        fig_mroi = go.Figure()
        fig_mroi.add_trace(
            go.Bar(
                x=["Actual MROI"],
                y=[actual_mroi],
                name="Actual MROI",
                marker_color="cyan",
                width=1,
                text=[format_value(actual_mroi)],
                textposition="auto",
                textfont=dict(color="black", size=14),
            )
        )
        fig_mroi.add_trace(
            go.Bar(
                x=["Optimized MROI"],
                y=[optimized_mroi],
                name="Optimized MROI",
                marker_color="blueviolet",
                width=1,
                text=[format_value(optimized_mroi)],
                textposition="auto",
                textfont=dict(color="black", size=14),
            )
        )

        fig_mroi.update_layout(
            annotations=[
                dict(
                    x=0.5,
                    y=1.3,
                    xref="paper",
                    yref="paper",
                    text="MROI",
                    showarrow=False,
                    font=dict(size=14),
                )
            ],
            barmode="group",
            bargap=0,
            showlegend=False,
            width=110,
            height=110,
            xaxis=dict(
                showticklabels=True,
                showgrid=False,
                tickangle=0,
                ticktext=["Actual", "Optimized"],
                tickvals=["Actual MROI", "Optimized MROI"],
            ),
            yaxis=dict(showticklabels=False, showgrid=False),
            margin=dict(t=20, b=20, r=0, l=0),
        )

        # Store plots
        channel_roi_mroi_plot[channel] = {"fig_roi": fig_roi, "fig_mroi": fig_mroi}

    return channel_roi_mroi_plot


# Function to save the current scenario with the mentioned name
def save_scenario(

    scenario_dict,

    metrics_selected,

    panel_selected,

    optimization_goal,

    channel_roi_mroi,

    timeframe,

    multiplier,

):
    # Remove extra space at start and ends
    if st.session_state["scenario_name"] is not None:
        st.session_state["scenario_name"] = st.session_state["scenario_name"].strip()

    if (
        st.session_state["scenario_name"] is None
        or st.session_state["scenario_name"] == ""
    ):
        # Store the message details in session state
        st.session_state.message_display = {
            "type": "warning",
            "message": "Please provide a name to save the scenario.",
            "icon": "⚠️",
        }
        return

    # Check the scenario name
    if not (
        word_length_limit_lower
        <= len(st.session_state["scenario_name"])
        <= word_length_limit_upper
        and bool(re.match("^[A-Za-z0-9_]*$", st.session_state["scenario_name"]))
    ):
        # Store the warning message details in session state
        st.session_state.message_display = {
            "type": "warning",
            "message": f"Please provide a valid scenario name ({word_length_limit_lower}-{word_length_limit_upper} characters, only A-Z, a-z, 0-9, and _).",
            "icon": "⚠️",
        }
        return

    # Check if the dictionary is empty
    if not scenario_dict:
        # Store the message details in session state
        st.session_state.message_display = {
            "type": "warning",
            "message": "Nothing to save. The scenario data is empty.",
            "icon": "⚠️",
        }
        return

    # Add additional scenario details
    scenario_dict["panel_selected"] = panel_selected
    scenario_dict["metrics_selected"] = metrics_selected
    scenario_dict["optimization"] = optimization_goal
    scenario_dict["channel_roi_mroi"] = channel_roi_mroi
    scenario_dict["timeframe"] = timeframe
    scenario_dict["multiplier"] = multiplier

    # Load existing scenarios
    saved_scenarios_dict = st.session_state["project_dct"]["saved_scenarios"][
        "saved_scenarios_dict"
    ]

    # Check if the name is already taken
    if st.session_state["scenario_name"] in saved_scenarios_dict.keys():
        # Store the message details in session state
        st.session_state.message_display = {
            "type": "warning",
            "message": "Name already exists. Please change the name or delete the existing scenario from the Saved Scenario page.",
            "icon": "⚠️",
        }
        return

    # Update the dictionary with the new scenario
    saved_scenarios_dict[st.session_state["scenario_name"]] = scenario_dict

    # Update the updated dictionary
    st.session_state["project_dct"]["saved_scenarios"][
        "saved_scenarios_dict"
    ] = saved_scenarios_dict

    # Update DB
    update_db(
        prj_id=st.session_state["project_number"],
        page_nam="Scenario Planner",
        file_nam="project_dct",
        pkl_obj=pickle.dumps(st.session_state["project_dct"]),
        schema=schema,
    )

    # Store the message details in session state
    st.session_state.message_display = {
        "type": "success",
        "message": f"Scenario '{st.session_state.scenario_name}' has been successfully saved!",
        "icon": "💾",
    }
    st.toast(
        f"Scenario '{st.session_state.scenario_name}' has been successfully saved!",
        icon="💾",
    )

    # Clear the scenario name input
    st.session_state["scenario_name"] = ""


# Function to calculate the RGBA color code based on the spends value and region boundaries
def calculate_rgba(spends_value, region_start_end):
    # Get region start and end points
    start_value = region_start_end["start_value"]
    end_value = region_start_end["end_value"]
    left_value = region_start_end["left_value"]
    right_value = region_start_end["right_value"]

    # Calculate alpha dynamically based on the position within the range
    def calculate_alpha(position, start, end, min_alpha=0.1, max_alpha=0.4):
        return min_alpha + (max_alpha - min_alpha) * (position - start) / (end - start)

    if start_value <= spends_value <= left_value:
        # Yellow range (0, 128, 0) - More transparent towards left, darker towards start
        alpha = calculate_alpha(spends_value, left_value, start_value)
        return (255, 255, 0, alpha)  # RGB for yellow
    elif left_value < spends_value <= right_value:
        # Green range (0, 128, 0) - More transparent towards right, darker towards left
        alpha = calculate_alpha(spends_value, right_value, left_value)
        return (0, 128, 0, alpha)  # RGB for green
    elif right_value < spends_value <= end_value:
        # Red range (255, 0, 0) - More transparent towards right, darker towards end
        alpha = calculate_alpha(spends_value, right_value, end_value)
        return (255, 0, 0, alpha)  # RGB for red


# Function to format and display the channel name with a color and background color
def display_channel_name_with_background_color(

    channel_name, background_color=(0, 128, 0, 0.1)

):
    formatted_name = name_formating(channel_name)

    # Unpack the RGBA values
    r, g, b, a = background_color

    # Create the HTML content with specified background color
    html_content = f"""

    <div style="

        background-color: rgba({r}, {g}, {b}, {a});

        padding: 10px;

        display: inline-block;

        border-radius: 5px;">

        <strong>{formatted_name}</strong>

    </div>

    """

    return html_content


# Function to check optimization success
def check_optimization_success(

    channel_list,

    input_channels_spends,

    output_channels_spends,

    bounds_dict,

    optimization_goal,

    modified_total_metrics,

    actual_total_metrics,

    modified_total_spends,

    actual_total_spends,

    original_total_spends,

    optimization_success,

):
    for channel in channel_list:
        input_channel_spends = input_channels_spends[channel]
        output_channel_spends = abs(output_channels_spends[channel])

        lower_percent = bounds_dict[channel][0]
        upper_percent = bounds_dict[channel][1]

        lower_allowed_value = max(
            (input_channel_spends * (100 + lower_percent - 1) / 100), 0
        )  # 1% Tolerance
        upper_allowed_value = max(
            (input_channel_spends * (100 + upper_percent + 1) / 100), 10
        )  # 1% Tolerance

        # Check if output spends are within allowed bounds
        if (
            output_channel_spends > upper_allowed_value
            or output_channel_spends < lower_allowed_value
        ):
            error_message = "Optimization failed: strict bounds. Use flexible bounds."
            return False, error_message, "❌"

    # Check optimization goal and percent change
    if optimization_goal == "Spend":
        percent_change_happened = abs(
            (modified_total_spends - actual_total_spends) / actual_total_spends
        )
        if percent_change_happened > 0.01:  # Greater than 1% Tolerance
            error_message = "Optimization failed: input and optimized spends differ. Use flexible bounds."
            return False, error_message, "❌"
    else:
        percent_change_happened = abs(
            (modified_total_metrics - actual_total_metrics) / actual_total_metrics
        )
        if percent_change_happened > 0.01:  # Greater than 1% Tolerance
            error_message = "Optimization failed: input and optimized metrics differ. Use flexible bounds."
            return False, error_message, "❌"

    # Define the allowable range for new spends
    lower_limit = original_total_spends * 0.5
    upper_limit = original_total_spends * 1.5

    # Check if the new spends are within the allowed range
    if modified_total_spends < lower_limit or modified_total_spends > upper_limit:
        error_message = "New spends optimized are outside the allowed range of ±50%."
        return False, error_message, "❌"

    # Check if the optimization failed to converge
    if not optimization_success:
        error_message = "Optimization failed to converge."
        return False, error_message, "❌"

    return True, "Optimization successful.", "💸"


# Function to check if the optimization target is achievable within the given bounds
def check_target_achievability(

    optimize_allow,

    optimization_goal,

    lower_achievable_target,

    upper_achievable_target,

    total_absolute_target,

):
    # Format the messages with appropriate numerization and naming
    given_input = "response metric" if optimization_goal == "Spend" else "spends"

    # Combined achievable message
    achievable_message = (
        f"Achievable {optimization_goal} with the given {given_input} and bounds ranges from "
        f"{numerize(lower_achievable_target / st.session_state.multiplier)} to "
        f"{numerize(upper_achievable_target / st.session_state.multiplier)}"
    )

    # Check if the target is within achievable bounds
    if (lower_achievable_target > total_absolute_target) or (
        upper_achievable_target < total_absolute_target
    ):
        # Update session state with the error message
        st.session_state.message_display = {
            "type": "error",
            "message": achievable_message,
            "icon": "⚠️",
        }
        optimize_allow = False

    elif (st.session_state.message_display["message"] is not None) and (
        str(st.session_state.message_display["message"]).startswith("Achievable")
    ):
        # Clear message_display
        st.session_state.message_display = {
            "type": "success",
            "message": None,
            "icon": "",
        }
        optimize_allow = True

    return optimize_allow


# Function to display a message with the appropriate type and icon
def display_message():
    # Retrieve the message details from the session state
    message_type = st.session_state.message_display["type"]
    message = st.session_state.message_display["message"]
    icon = st.session_state.message_display["icon"]

    # Display the message if it exists
    if message is not None:
        if message_type == "success":
            st.success(message, icon=icon)
            # Log message
            log_message("info", message, "Scenario Planner")
        elif message_type == "warning":
            st.warning(message, icon=icon)
            # Log message
            log_message("warning", message, "Scenario Planner")
        elif message_type == "error":
            st.error(message, icon=icon)
            # Log message
            log_message("error", message, "Scenario Planner")
        else:
            st.info(message, icon=icon)
            # Log message
            log_message("info", message, "Scenario Planner")


# Function to change bounds for all channels
def all_bound_change(channel_list, apply_all=False):
    # Fetch updated upper and lower bounds for all channels
    all_lower_bound = st.session_state["all_lower_key"]
    all_upper_bound = st.session_state["all_upper_key"]

    # Check if lower bound is not greater than upper bound
    if all_lower_bound < all_upper_bound:
        # Load modified scenario data
        data = st.session_state["project_dct"]["scenario_planner"][
            "modified_metadata_file"
        ]

        # Update the bounds for the all channels
        if apply_all:
            for channel in channel_list:
                if not data[metrics_selected][panel_selected]["channels"][channel][
                    "freeze"
                ]:
                    data[metrics_selected][panel_selected]["channels"][channel][
                        "bounds"
                    ] = [
                        all_lower_bound,
                        all_upper_bound,
                    ]

        # Update the bounds for the all channels holder
        data[metrics_selected][panel_selected]["bounds"] = [
            all_lower_bound,
            all_upper_bound,
        ]

        # Update modified scenario metadata
        st.session_state["project_dct"]["scenario_planner"][
            "modified_metadata_file"
        ] = data

    else:
        # Store the message details in session state
        st.session_state.message_display = {
            "type": "warning",
            "message": "Lower bound cannot be greater than Upper bound.",
            "icon": "⚠️",
        }
        return


try:
    # Page Title
    st.title("Scenario Planner")

    # Retrieve the list of all metric names from the specified directory
    metrics_list = get_metrics_names()

    # Check if there are any metrics available in the metrics list
    if not metrics_list:
        # Display a warning message to the user if no metrics are found
        st.warning(
            "Please tune at least one model to generate response curves data.",
            icon="⚠️",
        )

        # Log message
        log_message(
            "warning",
            "Please tune at least one model to generate response curves data.",
            "Scenario Planner",
        )

        st.stop()

    # Widget columns
    metric_col, panel_col, timeframe_col, save_progress_col = st.columns(4)

    # Metrics Selection
    metrics_selected = metric_col.selectbox(
        "Response Metrics",
        sorted(metrics_list),
        format_func=name_formating,
        key="response_metrics_selectbox_sp",
        index=0,
    )
    metrics_selected_formatted = name_formating(metrics_selected)

    # Retrieve the list of all panel names for specified Metrics
    panel_list = get_panels_names(metrics_selected)

    # Panel Selection
    panel_selected = panel_col.selectbox(
        "Panel",
        sorted(panel_list),
        format_func=name_formating,
        key="panel_selected_selectbox_sp",
        index=0,
    )
    panel_selected_formatted = name_formating(panel_selected)

    # Timeframe Selection
    timeframe_selected = timeframe_col.selectbox(
        "Timeframe",
        ["Input Data Range", "Yearly", "Quarterly", "Monthly"],
        key="timeframe_selected_selectbox_sp",
        index=0,
    )

    # Check if the RCS metadata file does not exist
    if (
        st.session_state["project_dct"]["response_curves"]["original_metadata_file"]
        is None
        or st.session_state["project_dct"]["response_curves"]["modified_metadata_file"]
        is None
    ):
        # RCS metadata file does not exist. Generating new RCS data
        generate_rcs_data()

        # Log message
        log_message(
            "info",
            "RCS metadata file does not exist. Generating new RCS data.",
            "Scenario Planner",
        )

    # Load rcs metadata files if they exist
    original_rcs_data, modified_rcs_data = load_rcs_metadata_files()

    # Check if the scenario metadata file does not exist
    if (
        st.session_state["project_dct"]["scenario_planner"]["original_metadata_file"]
        is None
        or st.session_state["project_dct"]["scenario_planner"]["modified_metadata_file"]
        is None
    ):
        # Scenario file does not exist. Generating new senario file data
        generate_scenario_data()

    # Load scenario metadata files if they exist
    original_data, modified_data = load_scenario_metadata_files()

    try:
        # Data date range
        date_range = pd.to_datetime(
            list(original_data[metrics_selected][panel_selected]["channels"].values())[
                0
            ]["dates"]
        )

        # Calculate the number of days between max and min dates
        date_diff = pd.Series(date_range).diff()
        day_data = int(
            (date_range.max() - date_range.min()).days
            + (6 if date_diff.value_counts().idxmax() == pd.Timedelta(weeks=1) else 0)
        )

        # Set the multiplier based on the selected timeframe
        if timeframe_selected == "Input Data Range":
            st.session_state["multiplier"] = 1
        elif timeframe_selected == "Yearly":
            st.session_state["multiplier"] = day_data / 365
        elif timeframe_selected == "Quarterly":
            st.session_state["multiplier"] = day_data / 90
        elif timeframe_selected == "Monthly":
            st.session_state["multiplier"] = day_data / 30
    except:
        st.session_state["multiplier"] = 1

    # Extract original scenario data for the selected metric and panel
    original_scenario_data = original_data[metrics_selected][panel_selected]

    # Extract modified scenario data for the same metric and panel
    modified_scenario_data = modified_data[metrics_selected][panel_selected]

    # Display Actual Vs Optimized
    st.divider()
    (
        actual_spends_col,
        actual_metrics_col,
        actual_CPA_col,
        base_col,
        optimized_spends_col,
        optimized_metrics_col,
        optimized_CPA_col,
    ) = st.columns([1, 1, 1, 1, 1.5, 1.5, 1.5])

    # Base Contribution
    base_contribution = (
        sum(original_scenario_data["constant"]) / st.session_state["multiplier"]
    )

    # Display Base Metric
    base_col.metric(
        f"Base {metrics_selected_formatted}",
        numerize(base_contribution),
    )

    # Extracting and formatting values
    actual_spends = numerize(
        original_scenario_data["actual_total_spends"] / st.session_state["multiplier"]
    )
    actual_metric_value = numerize(
        original_scenario_data["actual_total_sales"] / st.session_state["multiplier"]
    )
    optimized_spends = numerize(
        modified_scenario_data["modified_total_spends"] / st.session_state["multiplier"]
    )
    optimized_metric_value = numerize(
        modified_scenario_data["modified_total_sales"] / st.session_state["multiplier"]
    )

    # Calculate the deltas (differences) for spends and metrics
    spends_delta_value = (
        modified_scenario_data["modified_total_spends"]
        - original_scenario_data["actual_total_spends"]
    ) / st.session_state["multiplier"]

    metrics_delta_value = (
        modified_scenario_data["modified_total_sales"]
        - original_scenario_data["actual_total_sales"]
    ) / st.session_state["multiplier"]

    # Calculate the percentage changes for spends and metrics
    spends_percentage_change = (
        spends_delta_value
        / (
            original_scenario_data["actual_total_spends"]
            / st.session_state["multiplier"]
        )
    ) * 100

    metrics_percentage_change_media = (
        metrics_delta_value
        / (
            (
                original_scenario_data["actual_total_sales"]
                / st.session_state["multiplier"]
            )
            - base_contribution
        )
    ) * 100

    metrics_percentage_change_all = (
        metrics_delta_value
        / (
            original_scenario_data["actual_total_sales"]
            / st.session_state["multiplier"]
        )
    ) * 100

    # Format the percentage change for display
    spends_percentage_display = (
        f"({round(spends_percentage_change, 1)}%)"
        if abs(spends_percentage_change) >= 0.1
        else "(0%)"
    )
    metrics_percentage_display_media = (
        f"({round(metrics_percentage_change_media, 1)}%)"
        if abs(metrics_percentage_change_media) >= 0.1
        else "(0%)"
    )
    metrics_percentage_display_all = (
        f"({round(metrics_percentage_change_all, 1)}%)"
        if abs(metrics_percentage_change_all) >= 0.1
        else "(0%)"
    )

    # Check if the delta for spends is less than 0.1% in absolute terms
    if abs(spends_delta_value) < 0.001 * original_scenario_data["actual_total_spends"]:
        spends_delta = "0"
    else:
        spends_delta = numerize(spends_delta_value)

    # Check if the delta for metrics is less than 0.1% in absolute terms
    if abs(metrics_delta_value) < 0.001 * original_scenario_data["actual_total_sales"]:
        metrics_delta = "0"
    else:
        metrics_delta = numerize(metrics_delta_value)

    # Display current and optimized CPA
    actual_CPA = (
        original_scenario_data["actual_total_spends"]
        / original_scenario_data["actual_total_sales"]
    )
    optimized_CPA = (
        modified_scenario_data["modified_total_spends"]
        / modified_scenario_data["modified_total_sales"]
    )
    CPA_delta_value = optimized_CPA - actual_CPA

    # Calculate the percentage change for CPA
    CPA_percentage_change = (
        ((CPA_delta_value / actual_CPA) * 100) if actual_CPA != 0 else 0
    )
    CPA_percentage_display = (
        f"({round(CPA_percentage_change, 1)}%)"
        if abs(CPA_percentage_change) >= 0.1
        else "(0%)"
    )

    # Check if the CPA delta is less than 0.1% in absolute terms
    if abs(CPA_delta_value) < 0.001 * actual_CPA:
        CPA_delta = "0"
    else:
        CPA_delta = round_value(CPA_delta_value)

    # Display the metrics with percentage changes
    actual_CPA_col.metric(
        "Actual CPA",
        (numerize(actual_CPA) if actual_CPA >= 1000 else round_value(actual_CPA)),
    )
    optimized_spends_col.metric(
        "Optimized Spend",
        f"{optimized_spends} {spends_percentage_display}",
        delta=spends_delta,
    )
    optimized_metrics_col.metric(
        f"Optimized {metrics_selected_formatted}",
        f"{optimized_metric_value} {metrics_percentage_display_all}",
        delta=f"{metrics_delta} {metrics_percentage_display_media}",
    )
    optimized_CPA_col.metric(
        "Optimized CPA",
        (
            f"{numerize(optimized_CPA) if optimized_CPA >= 1000 else round_value(optimized_CPA)} {CPA_percentage_display}"
        ),
        delta=CPA_delta,
        delta_color="inverse",
    )

    # Displaying metrics in the columns
    actual_spends_col.metric("Actual Spend", actual_spends)
    actual_metrics_col.metric(
        f"Actual {metrics_selected_formatted}",
        actual_metric_value,
    )

    # Check if the percentage display for media starts with a negative sign
    if str(metrics_percentage_display_all[1:]).startswith("-"):
        # If negative, set the color to red
        metrics_percentage_display_media_str = f'<span style="color:rgb(255, 43, 43)">red <strong>{metrics_percentage_display_media}</strong></span>'
    else:
        # If positive, set the color to green
        metrics_percentage_display_media_str = f'<span style="color:rgb(9, 171, 59)">green <strong>{metrics_percentage_display_media}</strong></span>'

    # Display percentage calculation note
    st.markdown(
        f"**Note:** The percentage change for the response metric in {metrics_percentage_display_media_str} reflects the change based on the media-driven portion only, excluding the fixed base contribution and the percentage in black **{metrics_percentage_display_all}** represents the change based on the total response metric, including the base contribution. For spends, the percentage change **{spends_percentage_display}** is based on the total actual spends (base spends are always zero).",
        unsafe_allow_html=True,
    )

    # Divider
    st.divider()

    # Calculate ROI threshold
    st.session_state.roi_threshold = (
        original_scenario_data["actual_total_sales"]
        - sum(original_scenario_data["constant"])
    ) / original_scenario_data["actual_total_spends"]

    # Fetch and sort channels based on actual spends
    channel_list = list(
        sorted(
            original_scenario_data["channels"],
            key=lambda channel: (
                original_scenario_data["channels"][channel]["actual_total_spends"]
                * original_scenario_data["channels"][channel]["conversion_rate"]
            ),
            reverse=True,
        )
    )

    # Create columns for optimization goal and buttons
    (
        optimization_goal_col,
        message_display_col,
        button_col,
        bounds_col,
    ) = st.columns([3, 6, 3, 3])

    # Display spinnner or message
    with message_display_col:
        st.write("###")
        spinner_placeholder = st.empty()

    # Save Progress
    with save_progress_col:
        st.write("####")  # Padding
        save_progress_placeholder = st.empty()

    # Save page progress
    with spinner_placeholder, st.spinner("Saving Progress ..."):
        if save_progress_placeholder.button("Save Progress", use_container_width=True):
            # Update DB
            update_db(
                prj_id=st.session_state["project_number"],
                page_nam="Scenario Planner",
                file_nam="project_dct",
                pkl_obj=pickle.dumps(st.session_state["project_dct"]),
                schema=schema,
            )

            # Store the message details in session state
            with message_display_col:
                st.session_state.message_display = {
                    "type": "success",
                    "message": "Progress saved successfully!",
                    "icon": "💾",
                }
            st.toast("Progress saved successfully!", icon="💾")

    # Create columns for absolute text, slider, percentage number and bound type
    absolute_text_col, absolute_slider_col, percentage_number_col, all_bounds_col = (
        st.columns([2, 4, 2, 2])
    )

    # Dropdown for selecting optimization goal
    optimization_goal = optimization_goal_col.selectbox(
        "Fix", ["Spend", metrics_selected_formatted]
    )

    # Button columns with padding for alignment
    with button_col:
        st.write("##")  # Padding
        optimize_button_col, reset_button_col = st.columns(2)
        reset_button_col.button(
            "Reset",
            use_container_width=True,
            on_click=reset_scenario,
            args=(metrics_selected, panel_selected),
        )

    # Absolute value display
    if optimization_goal == "Spend":
        absolute_value = modified_scenario_data["actual_total_spends"]
        st.session_state.total_absolute_main_key = numerize(
            modified_scenario_data["modified_total_spends"]
            / st.session_state["multiplier"]
        )
    else:
        absolute_value = modified_scenario_data["actual_total_sales"]
        st.session_state.total_absolute_main_key = numerize(
            modified_scenario_data["modified_total_sales"]
            / st.session_state["multiplier"]
        )

    total_absolute = absolute_text_col.text_input(
        "Absolute",
        key="total_absolute_main_key",
        on_change=total_absolute_main_key_change,
        args=(
            metrics_selected,
            panel_selected,
            optimization_goal,
        ),
    )

    # Generate and process slider options
    slider_options = list(
        np.linspace(int(0.5 * absolute_value), int(1.5 * absolute_value), 50)
    )  # Generate range
    slider_options.append(
        modified_scenario_data["modified_total_spends"]
        if optimization_goal == "Spend"
        else modified_scenario_data["modified_total_sales"]
    )
    slider_options = sorted(slider_options)  # Sort the list
    numerized_slider_options = [
        numerize(value / st.session_state["multiplier"]) for value in slider_options
    ]  # Numerize each value

    # Slider for adjusting absolute value within a range
    st.session_state.total_absolute_key = numerize(
        modified_scenario_data["modified_total_spends"] / st.session_state["multiplier"]
        if optimization_goal == "Spend"
        else modified_scenario_data["modified_total_sales"]
        / st.session_state["multiplier"]
    )

    slider_value = absolute_slider_col.select_slider(
        "Absolute",
        numerized_slider_options,
        key="total_absolute_key",
        on_change=total_absolute_key_change,
        args=(
            metrics_selected,
            panel_selected,
            optimization_goal,
        ),
    )

    # Number input for percentage value
    if optimization_goal == "Spend":
        st.session_state.total_percentage_key = int(
            round(
                (
                    (
                        modified_scenario_data["modified_total_spends"]
                        - modified_scenario_data["actual_total_spends"]
                    )
                    / modified_scenario_data["actual_total_spends"]
                )
                * 100,
                0,
            )
        )
    else:
        st.session_state.total_percentage_key = int(
            round(
                (
                    (
                        modified_scenario_data["modified_total_sales"]
                        - modified_scenario_data["actual_total_sales"]
                    )
                    / modified_scenario_data["actual_total_sales"]
                )
                * 100,
                0,
            )
        )

    percentage_target = percentage_number_col.number_input(
        "Percentage",
        min_value=-50,
        max_value=50,
        key="total_percentage_key",
        on_change=total_percentage_key_change,
        args=(
            metrics_selected,
            panel_selected,
            absolute_value,
            optimization_goal,
        ),
    )

    # Toggle input for bound type
    st.session_state["bound_type_key"] = modified_scenario_data["bound_type"]
    with bounds_col:
        st.write("##")  # Padding

        # Columns for custom bounds toggle and apply all bounds button
        allow_custom_bounds_col, apply_all_bounds_col = st.columns(2)

        # Toggle for enabling/disabling custom bounds
        bound_type = allow_custom_bounds_col.toggle(
            "Bounds",
            on_change=bound_type_change,
            key="bound_type_key",
        )

        # Button to apply all bounds
        apply_all_bounds = apply_all_bounds_col.button(
            "Apply All",
            use_container_width=True,
            on_click=all_bound_change,
            args=(channel_list, True),
            disabled=not bound_type,
        )

    # Section for setting all lower and upper bounds
    with all_bounds_col:
        lower_bound_all, upper_bound_all = st.columns([1, 1])

        # Initialize session state keys for lower and upper bounds
        st.session_state["all_lower_key"] = (modified_scenario_data["bounds"])[0]
        st.session_state["all_upper_key"] = (modified_scenario_data["bounds"])[1]

        # Input for all lower bounds
        all_lower_bound = lower_bound_all.number_input(
            "All Lower Bounds",
            min_value=-100,
            max_value=100,
            key="all_lower_key",
            on_change=all_bound_change,
            args=(channel_list, False),
            disabled=not bound_type,
        )

        # Input for all upper bounds
        all_upper_bound = upper_bound_all.number_input(
            "All Upper Bounds",
            min_value=-100,
            max_value=100,
            key="all_upper_key",
            on_change=all_bound_change,
            args=(channel_list, False),
            disabled=not bound_type,
        )

    # Collect inputs from the user interface
    total_channel_spends, optimize_allow = 0, True
    bounds_dict = {}
    s_curve_params = {}
    channels_spends = {}
    channels_proportion = {}
    channels_conversion_ratio = {}
    channels_name_plot_placeholder = {}

    # Optimization Inputs UI
    with st.expander("Optimization Inputs", expanded=True):
        # Initialize total contributions for actual and optimized spends and metrics
        (
            total_actual_spend_contribution,
            total_actual_metric_contribution,
            total_optimized_spend_contribution,
            total_optimized_metric_contribution,
        ) = (
            0,
            sum(modified_scenario_data["constant"]),
            0,
            sum(modified_scenario_data["constant"]),
        )

        # Iterate over each channel in the channel list
        for channel in channel_list:
            # Accumulate actual total spends
            total_actual_spend_contribution += (
                modified_scenario_data["channels"][channel]["actual_total_spends"]
                * modified_scenario_data["channels"][channel]["conversion_rate"]
            )

            # Accumulate actual total sales (metrics)
            total_actual_metric_contribution += modified_scenario_data["channels"][
                channel
            ]["actual_total_sales"]

            # Accumulate optimized total spends
            total_optimized_spend_contribution += (
                modified_scenario_data["channels"][channel]["modified_total_spends"]
                * modified_scenario_data["channels"][channel]["conversion_rate"]
            )

            # Accumulate optimized total sales (metrics)
            total_optimized_metric_contribution += modified_scenario_data["channels"][
                channel
            ]["modified_total_sales"]

        for channel in channel_list:

            st.divider()

            # Channel key
            channel_key = f"{metrics_selected}_{panel_selected}_{channel}"

            # Create columns
            if st.session_state["bound_type_key"]:
                (
                    name_plot_col,
                    input_col,
                    spends_col,
                    metrics_col,
                    bounds_input_col,
                    bounds_display_col,
                    allow_col,
                ) = st.columns([3, 2, 2, 2, 2, 2, 1])
            else:
                (
                    name_plot_col,
                    input_col,
                    spends_col,
                    metrics_col,
                    bounds_display_col,
                    allow_col,
                ) = st.columns([1.5, 1, 1.5, 1.5, 1, 0.5])
                bounds_input_col = st.empty()

            # Display channel name and ROI/MROI plot
            with name_plot_col:
                # Placeholder for channel name
                channel_name_placeholder = st.empty()
                channel_name_placeholder.markdown(
                    display_channel_name_with_background_color(channel),
                    unsafe_allow_html=True,
                )

                # Placeholder for ROI and MROI plot
                channel_plot_placeholder = st.container()

                # Store placeholder for channel name and ROI/MROI plots
                channels_name_plot_placeholder[channel] = {
                    "channel_name_placeholder": channel_name_placeholder,
                    "channel_plot_placeholder": channel_plot_placeholder,
                }

            # Channel spends and sales
            channel_spends_actual = (
                original_scenario_data["channels"][channel]["actual_total_spends"]
                * original_scenario_data["channels"][channel]["conversion_rate"]
            )
            channel_metrics_actual = original_scenario_data["channels"][channel][
                "actual_total_sales"
            ]

            channel_spends_modified = (
                modified_scenario_data["channels"][channel]["modified_total_spends"]
                * original_scenario_data["channels"][channel]["conversion_rate"]
            )
            channel_metrics_modified = modified_scenario_data["channels"][channel][
                "modified_total_sales"
            ]

            # Channel spends input
            with input_col:
                # Absolute Spends Input
                st.session_state[f"{channel_key}_abs_spends_key"] = numerize(
                    modified_scenario_data["channels"][channel]["modified_total_spends"]
                    * original_scenario_data["channels"][channel]["conversion_rate"]
                    / st.session_state["multiplier"]
                )
                absolute_channel_spends = st.text_input(
                    "Absolute Spends",
                    key=f"{channel_key}_abs_spends_key",
                    on_change=absolute_channel_spends_change,
                    args=(
                        channel_key,
                        channel_spends_actual,
                        channel,
                        metrics_selected,
                        panel_selected,
                    ),
                )

                # Update Percentage Spends Input
                st.session_state[f"{channel_key}_per_spends_key"] = int(
                    round(
                        (
                            (
                                convert_to_float(
                                    st.session_state[f"{channel_key}_abs_spends_key"]
                                )
                                * st.session_state["multiplier"]
                                - float(channel_spends_actual)
                            )
                            / channel_spends_actual
                        )
                        * 100,
                        0,
                    )
                )

                # Percentage Spends Input
                percentage_channel_spends = st.number_input(
                    "Percentage Spends",
                    min_value=-1000,
                    max_value=1000,
                    key=f"{channel_key}_per_spends_key",
                    on_change=percentage_channel_spends_change,
                    args=(
                        channel_key,
                        channel_spends_actual,
                        channel,
                        metrics_selected,
                        panel_selected,
                    ),
                )

                # Store channel spends, conversion ratio and proportion list
                channels_spends[channel] = original_scenario_data["channels"][channel][
                    "actual_total_spends"
                ] * (1 + percentage_channel_spends / 100)

                channels_conversion_ratio[channel] = original_scenario_data["channels"][
                    channel
                ]["conversion_rate"]

                channels_proportion[channel] = original_scenario_data["channels"][
                    channel
                ]["spends"] / sum(original_scenario_data["channels"][channel]["spends"])

                # Calculate the percent contribution of actual spends for the channel
                channel_actual_spend_contribution = round(
                    (
                        modified_scenario_data["channels"][channel][
                            "actual_total_spends"
                        ]
                        * channels_conversion_ratio[channel]
                        / total_actual_spend_contribution
                    )
                    * 100,
                    1,
                )

                # Calculate the percent contribution of actual metrics (sales) for the channel
                channel_actual_metric_contribution = round(
                    (
                        modified_scenario_data["channels"][channel][
                            "actual_total_sales"
                        ]
                        / total_actual_metric_contribution
                    )
                    * 100,
                    1,
                )

                # Calculate the percent contribution of optimized spends for the channel
                channel_optimized_spend_contribution = round(
                    (
                        modified_scenario_data["channels"][channel][
                            "modified_total_spends"
                        ]
                        * channels_conversion_ratio[channel]
                        / total_optimized_spend_contribution
                    )
                    * 100,
                    1,
                )

                # Calculate the percent contribution of optimized metrics (sales) for the channel
                channel_optimized_metric_contribution = round(
                    (
                        modified_scenario_data["channels"][channel][
                            "modified_total_sales"
                        ]
                        / total_optimized_metric_contribution
                    )
                    * 100,
                    1,
                )

            # Channel metrics display
            with metrics_col:
                # Absolute Metrics
                st.metric(
                    f"Actual {name_formating(metrics_selected)}",
                    value=str(
                        numerize(
                            channel_metrics_actual / st.session_state["multiplier"]
                        )
                    )
                    + f"({channel_actual_metric_contribution}%)",
                )

                # Optimized Metrics
                optimized_metric = (
                    channel_metrics_modified / st.session_state["multiplier"]
                )
                actual_metric = channel_metrics_actual / st.session_state["multiplier"]
                delta_value = (
                    channel_metrics_modified - channel_metrics_actual
                ) / st.session_state["multiplier"]

                # Check if the delta is less than 0.1% in absolute terms
                if (
                    abs(delta_value) < 0.001 * actual_metric
                ):  # 0.1% of the actual metric
                    delta_display = "0"
                else:
                    delta_display = numerize(delta_value)

                st.metric(
                    f"Optimized {name_formating(metrics_selected)}",
                    value=str(numerize(optimized_metric))
                    + f"({channel_optimized_metric_contribution}%)",
                    delta=delta_display,
                )

            # Channel spends display
            with spends_col:
                # Absolute Spends
                st.metric(
                    "Actual Spend",
                    value=str(
                        numerize(channel_spends_actual / st.session_state["multiplier"])
                    )
                    + f"({channel_actual_spend_contribution}%)",
                )

                # Optimized Spends
                optimized_spends = (
                    channel_spends_modified / st.session_state["multiplier"]
                )
                actual_spends = channel_spends_actual / st.session_state["multiplier"]
                delta_spends_value = (
                    channel_spends_modified - channel_spends_actual
                ) / st.session_state["multiplier"]

                # Check if the delta is less than 0.1% in absolute terms
                if (
                    abs(delta_spends_value) < 0.001 * actual_spends
                ):  # 0.1% of the actual spend
                    delta_spends_display = "0"
                else:
                    delta_spends_display = numerize(delta_spends_value)

                st.metric(
                    "Optimized Spend",
                    value=str(numerize(optimized_spends))
                    + f"({channel_optimized_spend_contribution}%)",
                    delta=delta_spends_display,
                )

            # Channel allows optimize
            with allow_col:
                # Allow Optimize (Freeze)
                st.write("#")  # Padding
                st.session_state[f"{channel_key}_allow_optimize_key"] = (
                    modified_scenario_data["channels"][channel]["freeze"]
                )
                freeze = st.checkbox(
                    "Freeze",
                    key=f"{channel_key}_allow_optimize_key",
                    on_change=freeze_change,
                    args=(
                        metrics_selected,
                        panel_selected,
                        channel_key,
                        channel,
                        channel_list,
                    ),
                )

                # If channel is frozen, set bounds to keep the spend unchanged
                if freeze:
                    lower_bound, upper_bound = 0, 0  # Freeze the spend at current level

            # Channel bounds input
            if st.session_state["bound_type_key"]:
                with bounds_input_col:
                    # Channel upper bound
                    st.session_state[f"{channel_key}_upper_key"] = (
                        modified_scenario_data["channels"][channel]["bounds"]
                    )[1]
                    upper_bound = st.number_input(
                        "Upper bound (%)",
                        min_value=-100,
                        max_value=100,
                        key=f"{channel_key}_upper_key",
                        disabled=st.session_state[f"{channel_key}_allow_optimize_key"],
                        on_change=bound_change,
                        args=(
                            metrics_selected,
                            panel_selected,
                            channel_key,
                            channel,
                        ),
                    )

                    # Channel lower bound
                    st.session_state[f"{channel_key}_lower_key"] = (
                        modified_scenario_data["channels"][channel]["bounds"]
                    )[0]
                    lower_bound = st.number_input(
                        "Lower bound (%)",
                        min_value=-100,
                        max_value=100,
                        key=f"{channel_key}_lower_key",
                        disabled=st.session_state[f"{channel_key}_allow_optimize_key"],
                        on_change=bound_change,
                        args=(
                            metrics_selected,
                            panel_selected,
                            channel_key,
                            channel,
                        ),
                    )

                    # Check if lower bound is greater than upper bound
                    if lower_bound > upper_bound:
                        lower_bound = -10  # Default lower bound
                        upper_bound = 10  # Default upper bound

                    # Store bounds
                    bounds_dict[channel] = [lower_bound, upper_bound]

            else:
                # If channel is frozen, set bounds to keep the spend unchanged
                if freeze:
                    lower_bound, upper_bound = 0, 0  # Freeze the spend at current level
                else:
                    lower_bound = -10  # Default lower bound
                    upper_bound = 10  # Default upper bound

                # Store bounds
                bounds_dict[channel] = modified_scenario_data["channels"][channel][
                    "bounds"
                ]

            # Display the bounds for each channel's spend in the bounds_display_col
            with bounds_display_col:
                # Retrieve the actual spends for the channel from the original scenario data
                actual_spends = (
                    modified_scenario_data["channels"][channel]["modified_total_spends"]
                    * modified_scenario_data["channels"][channel]["conversion_rate"]
                )

                # Calculate the limit for spends
                upper_limit_spends = actual_spends * (1 + upper_bound / 100)
                lower_limit_spends = actual_spends * (1 + lower_bound / 100)

                # Display the upper limit spends
                st.metric(
                    "Upper Bound",
                    numerize(upper_limit_spends / st.session_state["multiplier"]),
                )
                st.metric(
                    "Lower Bound",
                    numerize(lower_limit_spends / st.session_state["multiplier"]),
                )

            # Store S-curve parameters
            s_curve_params[channel] = get_s_curve_params(
                metrics_selected,
                panel_selected,
                channel,
                original_rcs_data,
                modified_rcs_data,
            )

            # Total channel spends
            total_channel_spends += (
                convert_to_float(st.session_state[f"{channel_key}_abs_spends_key"])
                * st.session_state["multiplier"]
            )

        # Check if total channel spends are within the allowed range (±50% of the original total spends)
        if (
            total_channel_spends > 1.5 * original_scenario_data["actual_total_spends"]
            or total_channel_spends
            < 0.5 * original_scenario_data["actual_total_spends"]
        ):
            # Store the message details in session state
            st.session_state.message_display = {
                "type": "warning",
                "message": "Keep total spending within ±50% of the original value.",
                "icon": "⚠️",
            }

    if optimization_goal == "Spend":
        # Get maximum achievable spends
        lower_achievable_target, upper_achievable_target = 0, 0
        for channel in channel_list:
            channel_spends_actual = (
                channels_spends[channel] * channels_conversion_ratio[channel]
            )
            lower_achievable_target += channel_spends_actual * (
                1 + bounds_dict[channel][0] / 100
            )
            upper_achievable_target += channel_spends_actual * (
                1 + bounds_dict[channel][1] / 100
            )
    else:
        # Get maximum achievable target metric
        lower_achievable_target, upper_achievable_target = max_target_achievable(
            channels_spends,
            s_curve_params,
            channels_proportion,
            modified_scenario_data,
            bounds_dict,
        )

    # Total target of selected metric
    if optimization_goal == "Spend":
        total_absolute_target = modified_scenario_data["modified_total_spends"]
    else:
        total_absolute_target = modified_scenario_data["modified_total_sales"]

    # Check if the target is achievable within the specified bounds
    if optimize_allow:
        optimize_allow = check_target_achievability(
            optimize_allow,
            name_formating(optimization_goal),
            lower_achievable_target,
            upper_achievable_target,
            total_absolute_target,
        )

    # Perform the optimization
    if optimize_button_col.button(
        "Optimize",
        use_container_width=True,
        disabled=not optimize_allow,
        key="run_optimizer",
    ):
        with message_display_col:
            with spinner_placeholder, st.spinner("Optimizing ..."):
                # Call the optimizer function to get optimized spends
                optimized_spends, optimization_success = optimizer(
                    optimization_goal,
                    s_curve_params,
                    channels_spends,
                    channels_proportion,
                    channels_conversion_ratio,
                    total_absolute_target,
                    bounds_dict,
                    modified_scenario_data,
                )

                # Initialize dictionaries to store input and output channel spends
                input_channels_spends, output_channels_spends = {}, {}
                for channel in channel_list:
                    # Calculate input channel spends by converting spends using conversion ratio
                    input_channels_spends[channel] = (
                        channels_spends[channel] * channels_conversion_ratio[channel]
                    )
                    # Calculate output channel spends by converting optimized spends using conversion ratio
                    output_channels_spends[channel] = (
                        optimized_spends[channel] * channels_conversion_ratio[channel]
                    )

                # Calculate total actual and modified spends
                actual_total_spends = sum(list(input_channels_spends.values()))
                modified_total_spends = sum(list(output_channels_spends.values()))

                # Retrieve the actual total metrics from modified scenario data
                actual_total_metrics = modified_scenario_data["modified_total_sales"]
                modified_total_metrics = 0  # Initialize modified total metrics
                modified_channels_metrics = {}

                # Calculate modified metrics for each channel
                for channel in optimized_spends.keys():
                    channel_s_curve_params = s_curve_params[channel]
                    spend_proportion = (
                        optimized_spends[channel] * channels_proportion[channel]
                    )
                    # Calculate the metrics using the S-curve function
                    modified_channels_metrics[channel] = sum(
                        s_curve(
                            spend_proportion,
                            channel_s_curve_params["power"],
                            channel_s_curve_params["K"],
                            channel_s_curve_params["b"],
                            channel_s_curve_params["a"],
                            channel_s_curve_params["x0"],
                        )
                    ) + sum(
                        modified_scenario_data["channels"][channel]["correction"]
                    )  # correction for s-curve

                    modified_total_metrics += modified_channels_metrics[
                        channel
                    ]  # Add channel metrics to total metrics

                # Add the constant and correction term to the modified total metrics
                modified_total_metrics += sum(modified_scenario_data["constant"])

                # Retrieve the original total spends from modified scenario data
                original_total_spends = modified_scenario_data["actual_total_spends"]

                # Check the success of the optimization process
                success, message, icon = check_optimization_success(
                    channel_list,
                    input_channels_spends,
                    output_channels_spends,
                    bounds_dict,
                    optimization_goal,
                    modified_total_metrics,
                    actual_total_metrics,
                    modified_total_spends,
                    actual_total_spends,
                    original_total_spends,
                    optimization_success,
                )

                # Store the message details in session state
                st.session_state.message_display = {
                    "type": "success" if success else "error",
                    "message": message,
                    "icon": icon,
                }

                # Update data only if the optimization is successful
                if success:
                    # Update the modified spend and metrics for each channel in the scenario data
                    for channel in channel_list:
                        modified_scenario_data["channels"][channel][
                            "modified_total_spends"
                        ] = optimized_spends[channel]

                        # Update the modified metrics for each channel in the scenario data
                        modified_scenario_data["channels"][channel][
                            "modified_total_sales"
                        ] = modified_channels_metrics[channel]

                    # Update the total modified spends in the scenario data
                    modified_scenario_data["modified_total_spends"] = (
                        modified_total_spends
                    )

                    # Update the total modified metrics in the scenario data
                    modified_scenario_data["modified_total_sales"] = (
                        modified_total_metrics
                    )

                    # Load modified scenario data
                    data = st.session_state["project_dct"]["scenario_planner"][
                        "modified_metadata_file"
                    ]

                    # Update the specific section with the modified scenario data
                    data[metrics_selected][panel_selected] = modified_scenario_data

                    # Update modified scenario metadata
                    st.session_state["project_dct"]["scenario_planner"][
                        "modified_metadata_file"
                    ] = data

            # Reset optimizer button
            del st.session_state["run_optimizer"]

            # Rerun to update values
            st.rerun()

    ########################################## Response Curves ##########################################

    # Generate plots
    figures, channel_roi_mroi, region_start_end = generate_response_curve_plots(
        channel_list,
        s_curve_params,
        channels_proportion,
        original_scenario_data,
        st.session_state["multiplier"],
    )

    # Display Response Curves
    st.subheader(f"Response Curves (X: Spends Vs Y: {metrics_selected_formatted})")
    with st.expander("Response Curves", expanded=True):
        cols = st.columns(4)  # Create 4 columns for the first row
        for i, fig in enumerate(figures):
            col = cols[i % 4]  # Rotate through the columns
            with col:
                # Get channel parameters
                channel = channel_list[i]
                modified_total_spends = modified_scenario_data["channels"][channel][
                    "modified_total_spends"
                ]
                conversion_rate = modified_scenario_data["channels"][channel][
                    "conversion_rate"
                ]
                channel_correction = sum(
                    modified_scenario_data["channels"][channel]["correction"]
                )

                # Updated figure with modified metrics point
                roi_optimized, mroi_optimized, fig_updated = modified_metrics_point(
                    fig,
                    modified_total_spends,
                    s_curve_params[channel],
                    channels_proportion[channel],
                    conversion_rate,
                    channel_correction,
                )

                # Store data of each channel ROI and MROI
                channel_roi_mroi[channel]["optimized_roi"] = roi_optimized
                channel_roi_mroi[channel]["optimized_mroi"] = mroi_optimized

                st.plotly_chart(fig_updated, use_container_width=True)

            # Start a new row after every 4 plots
            if (i + 1) % 4 == 0 and i + 1 < len(figures):
                cols = st.columns(4)  # Create new row with 4 columns

    # Generate the plots
    channel_roi_mroi_plot = roi_mori_plot(channel_roi_mroi)

    # Display the plots and name with background color
    for channel in channel_list:
        with channels_name_plot_placeholder[channel]["channel_plot_placeholder"]:
            # Create subplots with 2 columns for ROI and MROI
            roi_plot_col, mroi_plot_col = st.columns(2)

            # Display ROI and MROI plots
            roi_plot_col.plotly_chart(channel_roi_mroi_plot[channel]["fig_roi"])
            mroi_plot_col.plotly_chart(channel_roi_mroi_plot[channel]["fig_mroi"])

        # Placeholder for the channel name
        channel_name_placeholder = channels_name_plot_placeholder[channel][
            "channel_name_placeholder"
        ]

        # Retrieve modified total spends and conversion rate for the channel
        modified_total_spends = modified_scenario_data["channels"][channel][
            "modified_total_spends"
        ]
        conversion_rate = modified_scenario_data["channels"][channel]["conversion_rate"]

        # Calculate the actual spend value for the channel
        channel_spends_value = modified_total_spends * conversion_rate

        # Calculate the RGBA color value for the channel based on its spend
        channel_rgba_value = calculate_rgba(
            channel_spends_value, region_start_end[channel]
        )

        # Display the channel name with the calculated background color
        channel_name_placeholder.markdown(
            display_channel_name_with_background_color(channel, channel_rgba_value),
            unsafe_allow_html=True,
        )

    # Input field for the scenario name
    st.text_input("Scenario Name", key="scenario_name")

    # Disable the "Save Scenario" button until a name is provided
    if (
        st.session_state["scenario_name"] is None
        or st.session_state["scenario_name"] == ""
    ):
        save_scenario_button_disabled = True
    else:
        save_scenario_button_disabled = False

    # Button to save the scenario
    save_button_placeholder = st.empty()
    with st.spinner("Saving ..."):
        save_button_placeholder.button(
            "Save Scenario",
            on_click=save_scenario,
            args=(
                modified_scenario_data,
                metrics_selected,
                panel_selected,
                optimization_goal,
                channel_roi_mroi,
                st.session_state["timeframe_selected_selectbox_sp"],
                st.session_state["multiplier"],
            ),
            disabled=save_scenario_button_disabled,
        )

    ########################################## Display Message ##########################################

    # Display all message
    with message_display_col:
        display_message()

except Exception as e:
    # Capture the error details
    exc_type, exc_value, exc_traceback = sys.exc_info()
    error_message = "".join(
        traceback.format_exception(exc_type, exc_value, exc_traceback)
    )

    # Log message
    log_message("error", f"An error occurred: {error_message}.", "Scenario Planner")

    # Display a warning message
    st.warning(
        "Oops! Something went wrong. Please try refreshing the tool or creating a new project.",
        icon="⚠️",
    )