File size: 24,506 Bytes
0ddfcf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16e3779
0ddfcf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7bb281
0ddfcf7
 
 
 
f7bb281
0ddfcf7
 
 
 
 
 
 
 
 
2d928fb
 
 
 
0ddfcf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed0b93a
2d928fb
0ddfcf7
2d928fb
 
 
 
 
 
0ddfcf7
 
 
 
 
 
f7bb281
0ddfcf7
 
 
 
 
 
f7bb281
 
 
0ddfcf7
 
 
 
 
 
 
 
 
f7bb281
 
0ddfcf7
f7bb281
0ddfcf7
 
 
 
 
2d928fb
0ddfcf7
 
2d928fb
0ddfcf7
2d928fb
 
 
 
 
 
0ddfcf7
 
a7b3ed8
 
0ddfcf7
 
 
 
 
 
f7bb281
0ddfcf7
 
 
 
 
 
f7bb281
 
0ddfcf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7bb281
 
 
 
0ddfcf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7bb281
0ddfcf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7202334
 
 
 
 
0ddfcf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4e26b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ddfcf7
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
from numerize.numerize import numerize
import streamlit as st
import pandas as pd
import json
from classes import Channel, Scenario
import numpy as np
from plotly.subplots import make_subplots
import plotly.graph_objects as go
from classes import class_to_dict
from collections import OrderedDict
import io
import plotly
from pathlib import Path
import pickle
import yaml
from yaml import SafeLoader
from streamlit.components.v1 import html
import smtplib
from scipy.optimize import curve_fit
from sklearn.metrics import r2_score
from classes import class_from_dict
import os
import base64


color_palette = [
    "#F3F3F0",
    "#5E7D7E",
    "#2FA1FF",
    "#00EDED",
    "#00EAE4",
    "#304550",
    "#EDEBEB",
    "#7FBEFD",
    "#003059",
    "#A2F3F3",
    "#E1D6E2",
    "#B6B6B6",
]


CURRENCY_INDICATOR = '$'

import streamlit_authenticator as stauth


def load_authenticator():
    with open("config.yaml") as file:
        config = yaml.load(file, Loader=SafeLoader)
        st.session_state["config"] = config
    authenticator = stauth.Authenticate(
        credentials=config["credentials"],
        cookie_name=config["cookie"]["name"],
        key=config["cookie"]["key"],
        cookie_expiry_days=config["cookie"]["expiry_days"],
        preauthorized=config["preauthorized"],
    )
    st.session_state["authenticator"] = authenticator
    return authenticator


# Authentication
def authentication():
    with open("config.yaml") as file:
        config = yaml.load(file, Loader=SafeLoader)

        authenticator = stauth.Authenticate(
            config["credentials"],
            config["cookie"]["name"],
            config["cookie"]["key"],
            config["cookie"]["expiry_days"],
            config["preauthorized"],
        )

    name, authentication_status, username = authenticator.login("Login", "main")
    return authenticator, name, authentication_status, username


def nav_page(page_name, timeout_secs=3):
    nav_script = """
        <script type="text/javascript">
            function attempt_nav_page(page_name, start_time, timeout_secs) {
                var links = window.parent.document.getElementsByTagName("a");
                for (var i = 0; i < links.length; i++) {
                    if (links[i].href.toLowerCase().endsWith("/" + page_name.toLowerCase())) {
                        links[i].click();
                        return;
                    }
                }
                var elasped = new Date() - start_time;
                if (elasped < timeout_secs * 1000) {
                    setTimeout(attempt_nav_page, 100, page_name, start_time, timeout_secs);
                } else {
                    alert("Unable to navigate to page '" + page_name + "' after " + timeout_secs + " second(s).");
                }
            }
            window.addEventListener("load", function() {
                attempt_nav_page("%s", new Date(), %d);
            });
        </script>
    """ % (
        page_name,
        timeout_secs,
    )
    html(nav_script)


# def load_local_css(file_name):
#     with open(file_name) as f:
#         st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)


# def set_header():
#     return st.markdown(f"""<div class='main-header'>
#                     <h1>MMM LiME</h1>
#                     <img src="https://assets-global.website-files.com/64c8fffb0e95cbc525815b79/64df84637f83a891c1473c51_Vector%20(Stroke).svg   ">
#             </div>""", unsafe_allow_html=True)

path = os.path.dirname(__file__)

file_ = open(f"{path}/ALDI_2017.png", "rb")

contents = file_.read()

data_url = base64.b64encode(contents).decode("utf-8")

file_.close()


DATA_PATH = "./data"

IMAGES_PATH = "./data/images_224_224"


def load_local_css(file_name):

    with open(file_name) as f:

        st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)


# def set_header():

#     return st.markdown(f"""<div class='main-header'>

#                     <h1>H & M Recommendations</h1>

#                     <img src="data:image;base64,{data_url}", alt="Logo">

#             </div>""", unsafe_allow_html=True)
path1 = os.path.dirname(__file__)

file_1 = open(f"{path}/ALDI_2017.png", "rb")

contents1 = file_1.read()

data_url1 = base64.b64encode(contents1).decode("utf-8")

file_1.close()


DATA_PATH1 = "./data"

IMAGES_PATH1 = "./data/images_224_224"


def set_header():
    return st.markdown(
        f"""<div class='main-header'>
                    <!-- <h1></h1> -->
                       <div >
                    <img class='blend-logo' src="data:image;base64,{data_url1}", alt="Logo">
            </div>""",
        unsafe_allow_html=True,
    )


# def set_header():
#     logo_path = "./path/to/your/local/LIME_logo.png"  # Replace with the actual file path
#     text = "LiME"
#     return st.markdown(f"""<div class='main-header'>
#                     <img src="data:image/png;base64,{data_url}" alt="Logo" style="float: left; margin-right: 10px; width: 100px; height: auto;">
#                     <h1>{text}</h1>
#             </div>""", unsafe_allow_html=True)


def s_curve(x, K, b, a, x0):
    return K / (1 + b * np.exp(-a * (x - x0)))


def panel_level(input_df, date_column="Date"):
    # Ensure 'Date' is set as the index
    if date_column not in input_df.index.names:
        input_df = input_df.set_index(date_column)

    # Select numeric columns only (excluding 'Date' since it's now the index)
    numeric_columns_df = input_df.select_dtypes(include="number")

    # Group by 'Date' (which is the index) and sum the numeric columns
    aggregated_df = numeric_columns_df.groupby(input_df.index).sum()

    # Reset index if you want 'Date' back as a column
    aggregated_df = aggregated_df.reset_index()

    return aggregated_df


def initialize_data(
    target_file, panel=None,  updated_rcs=None, metrics=None
):
    # uopx_conv_rates = {'streaming_impressions' : 0.007,'digital_impressions' : 0.007,'search_clicks' : 0.00719,'tv_impressions' : 0.000173,
    #                    "digital_clicks":0.005,"streaming_clicks":0.004,'streaming_spends':1,"tv_spends":1,"search_spends":1,
    #                    "digital_spends":1}
    # # print('State initialized')

    excel = pd.read_excel(target_file, sheet_name=None)

    # Extract dataframes for raw data, spend input, and contribution MMM
    raw_df = excel["RAW DATA MMM"]
    spend_df = excel["SPEND INPUT"]
    contri_df = excel["CONTRIBUTION MMM"]

    # Check if the panel is not None
    
    raw_df = panel_level(raw_df, date_column="Date")
    spend_df = panel_level(spend_df, date_column="Week")
    contri_df = panel_level(contri_df, date_column="Date")

    # Revenue_df = excel['Revenue']

    ## remove sesonalities, indices etc ...
    exclude_columns = [
        "Date",
        "Region",
        "Controls_Grammarly_Index_SeasonalAVG",
        "Controls_Quillbot_Index",
        "Daily_Positive_Outliers",
        "External_RemoteClass_Index",
        "Intervals ON 20190520-20190805 | 20200518-20200803 | 20210517-20210802",
        "Intervals ON 20190826-20191209 | 20200824-20201207 | 20210823-20211206",
        "Intervals ON 20201005-20201019",
        "Promotion_PercentOff",
        "Promotion_TimeBased",
        "Seasonality_Indicator_Chirstmas",
        "Seasonality_Indicator_NewYears_Days",
        "Seasonality_Indicator_Thanksgiving",
        "Trend 20200302 / 20200803",
    ]
    raw_df["Date"] = pd.to_datetime(raw_df["Date"])
    contri_df["Date"] = pd.to_datetime(contri_df["Date"])
    input_df = raw_df.sort_values(by="Date")
    output_df = contri_df.sort_values(by="Date")
    spend_df["Week"] = pd.to_datetime(
        spend_df["Week"], format="%Y-%m-%d", errors="coerce"
    )
    spend_df.sort_values(by="Week", inplace=True)

    # spend_df['Week'] = pd.to_datetime(spend_df['Week'], errors='coerce')
    # spend_df = spend_df.sort_values(by='Week')

    channel_list = [col for col in input_df.columns if col not in exclude_columns]
    channel_list = list(set(channel_list) - set(["fb_level_achieved_tier_1", "ga_app"]))

    response_curves = {}
    mapes = {}
    rmses = {}
    upper_limits = {}
    powers = {}
    r2 = {}
    conv_rates = {}
    output_cols = []
    channels = {}
    sales = None
    dates = input_df.Date.values
    actual_output_dic = {}
    actual_input_dic = {}

    for inp_col in channel_list:
        # st.write(inp_col)
        spends = input_df[inp_col].values
        x = spends.copy()
        # upper limit for penalty
        upper_limits[inp_col] = 2 * x.max()

        # contribution
        out_col = [_col for _col in output_df.columns if _col.startswith(inp_col)][0]
        y = output_df[out_col].values.copy()
        actual_output_dic[inp_col] = y.copy()
        actual_input_dic[inp_col] = x.copy()
        ##output cols aggregation
        output_cols.append(out_col)

        params = pd.read_excel("response_curves_parameters.xlsx",index_col = "channel")
        param_dicts = {col: params[col].to_dict() for col in params.columns}
        response_curves[inp_col] = {
            "Kd": param_dicts["Kd"][inp_col],
            "n": param_dicts["n"][inp_col],
            "x_min": param_dicts["x_min"][inp_col],
            "x_max": param_dicts["x_max"][inp_col],
            "y_min": param_dicts["y_min"][inp_col],
            "y_max": param_dicts["y_max"][inp_col]
        }

        updated_rcs_key = f"{metrics}#@{panel}#@{inp_col}"
        if updated_rcs is not None and updated_rcs_key in list(updated_rcs.keys()):
            response_curves[inp_col] = updated_rcs[updated_rcs_key]

        # # print(response_curves)
        ## conversion rates
        spend_col = [
            _col
            for _col in spend_df.columns
            if _col.startswith(inp_col.rsplit("_", 1)[0])
        ][0]
        # # print(spend_col)
        # # print('## printing spendssss')
        # # print(spend_col)
        conv = (
            spend_df.set_index("Week")[spend_col]
            / input_df.set_index("Date")[inp_col].clip(lower=1)
        ).reset_index()
        conv.rename(columns={"index": "Week"}, inplace=True)
        conv["year"] = conv.Week.dt.year
        conv_rates[inp_col] = list(conv.drop("Week", axis=1).mean().to_dict().values())[
            0
        ]
        # # print(conv_rates)
        ### print('Before',conv_rates[inp_col])
        # conv_rates[inp_col] = uopx_conv_rates[inp_col]
        ### print('After',(conv_rates[inp_col]))

        channel = Channel(
            name=inp_col,
            dates=dates,
            spends=spends,
            sales= y.copy(),
            # conversion_rate = np.mean(list(conv_rates[inp_col].values())),
            conversion_rate=conv_rates[inp_col],
            response_curve_type="hill-eq",
            response_curve_params={
                "Kd": param_dicts["Kd"][inp_col],
                "n": param_dicts["n"][inp_col],
                "x_min": param_dicts["x_min"][inp_col],
                "x_max": param_dicts["x_max"][inp_col],
                "y_min": param_dicts["y_min"][inp_col],
                "y_max": param_dicts["y_max"][inp_col]
            },
            bounds=np.array([-10, 10]),
            channel_bounds_min = 10,
            channel_bounds_max = 10
        )
        channels[inp_col] = channel
        if sales is None:
            sales = channel.actual_sales
        else:
            sales += channel.actual_sales
    # # print(actual_output_dic)
    other_contributions = (
        output_df.drop([*output_cols], axis=1).sum(axis=1, numeric_only=True).values
    )
    correction = output_df.drop("Date", axis=1).sum(axis=1).values - (
        sales + other_contributions
    )
    # # print(other_contributions)
    # # print(correction)
    scenario = Scenario(
        name="default",
        channels=channels,
        constant=other_contributions,
        correction=correction,
    )
    ## setting session variables
    st.session_state["initialized"] = True
    st.session_state["actual_df"] = input_df
    st.session_state["raw_df"] = raw_df
    st.session_state["contri_df"] = output_df
    default_scenario_dict = class_to_dict(scenario)
    st.session_state["default_scenario_dict"] = default_scenario_dict
    st.session_state["scenario"] = scenario
    st.session_state["channels_list"] = channel_list
    st.session_state["optimization_channels"] = {
        channel_name: False for channel_name in channel_list
    }
    st.session_state["rcs"] = response_curves

    st.session_state["powers"] = powers
    st.session_state["actual_contribution_df"] = pd.DataFrame(actual_output_dic)
    st.session_state["actual_input_df"] = pd.DataFrame(actual_input_dic)

    for channel in channels.values():
        st.session_state[channel.name] = numerize(
            channel.actual_total_spends * channel.conversion_rate, 1
        )

    st.session_state["xlsx_buffer"] = io.BytesIO()

    if Path("../saved_scenarios.pkl").exists():
        with open("../saved_scenarios.pkl", "rb") as f:
            st.session_state["saved_scenarios"] = pickle.load(f)
    else:
        st.session_state["saved_scenarios"] = OrderedDict()

    # st.session_state["total_spends_change"] = 0
    st.session_state["optimization_channels"] = {
        channel_name: False for channel_name in channel_list
    }
    st.session_state["disable_download_button"] = True
    # if target_file == :
    #     st.session_state["dividing_parameter"] =  
    # else :



def create_channel_summary(scenario):

    # Provided data
    data = {
        "Channel": [
            "Paid Search",
            "Ga will cid baixo risco",
            "Digital tactic others",
            "Fb la tier 1",
            "Fb la tier 2",
            "Paid social others",
            "Programmatic",
            "Kwai",
            "Indicacao",
            "Infleux",
            "Influencer",
        ],
        "Spends": [
            "$ 11.3K",
            "$ 155.2K",
            "$ 50.7K",
            "$ 125.4K",
            "$ 125.2K",
            "$ 105K",
            "$ 3.3M",
            "$ 47.5K",
            "$ 55.9K",
            "$ 632.3K",
            "$ 48.3K",
        ],
        "Revenue": [
            "558.0K",
            "3.5M",
            "5.2M",
            "3.1M",
            "3.1M",
            "2.1M",
            "20.8M",
            "1.6M",
            "728.4K",
            "22.9M",
            "4.8M",
        ],
    }

    # Create DataFrame
    df = pd.DataFrame(data)

    # Convert currency strings to numeric values
    df["Spends"] = (
        df["Spends"]
        .replace({"\$": "", "K": "*1e3", "M": "*1e6"}, regex=True)
        .map(pd.eval)
        .astype(int)
    )
    df["Revenue"] = (
        df["Revenue"]
        .replace({"\$": "", "K": "*1e3", "M": "*1e6"}, regex=True)
        .map(pd.eval)
        .astype(int)
    )

    # Calculate ROI
    df["ROI"] = (df["Revenue"] - df["Spends"]) / df["Spends"]

    # Format columns
    format_currency = lambda x: f"${x:,.1f}"
    format_roi = lambda x: f"{x:.1f}"

    df["Spends"] = [
        "$ 11.3K",
        "$ 155.2K",
        "$ 50.7K",
        "$ 125.4K",
        "$ 125.2K",
        "$ 105K",
        "$ 3.3M",
        "$ 47.5K",
        "$ 55.9K",
        "$ 632.3K",
        "$ 48.3K",
    ]
    df["Revenue"] = [
        "$ 536.3K",
        "$ 3.4M",
        "$ 5M",
        "$ 3M",
        "$ 3M",
        "$ 2M",
        "$ 20M",
        "$ 1.5M",
        "$ 7.1M",
        "$ 22M",
        "$ 4.6M",
    ]
    df["ROI"] = df["ROI"].apply(format_roi)

    return df



def create_contribution_pie():
    color_palette = [
        "#F3F3F0",
        "#5E7D7E",
        "#2FA1FF",
        "#00EDED",
        "#00EAE4",
        "#304550",
        "#EDEBEB",
        "#7FBEFD",
        "#003059",
        "#A2F3F3",
        "#E1D6E2",
        "#B6B6B6",
    ]
    total_contribution_fig = make_subplots(
        rows=1,
        cols=2,
        subplot_titles=["Spends", "Revenue"],
        specs=[[{"type": "pie"}, {"type": "pie"}]],
    )

    channels_list = [
        "Paid Search",
        "Ga will cid baixo risco",
        "Digital tactic others",
        "Fb la tier 1",
        "Fb la tier 2",
        "Paid social others",
        "Programmatic",
        "Kwai",
        "Indicacao",
        "Infleux",
        "Influencer",
        "Non Media",
    ]

    # Assign colors from the limited palette to channels
    colors_map = {
        col: color_palette[i % len(color_palette)]
        for i, col in enumerate(channels_list)
    }
    colors_map["Non Media"] = color_palette[
        5
    ]  # Assign fixed green color for 'Non Media'

    # Hardcoded values for Spends and Revenue
    spends_values = [0.5, 3.36, 1.1, 2.7, 2.7, 2.27, 70.6, 1, 1, 13.7, 1, 0]
    revenue_values = [1, 4, 5, 3, 3, 2, 50.8, 1.5, 0.7, 13, 0, 16]

    # Add trace for Spends pie chart
    total_contribution_fig.add_trace(
        go.Pie(
            labels=[channel_name for channel_name in channels_list],
            values=spends_values,
            marker=dict(
                colors=[colors_map[channel_name] for channel_name in channels_list]
            ),
            hole=0.3,
        ),
        row=1,
        col=1,
    )

    # Add trace for Revenue pie chart
    total_contribution_fig.add_trace(
        go.Pie(
            labels=[channel_name for channel_name in channels_list],
            values=revenue_values,
            marker=dict(
                colors=[colors_map[channel_name] for channel_name in channels_list]
            ),
            hole=0.3,
        ),
        row=1,
        col=2,
    )

    total_contribution_fig.update_traces(
        textposition="inside", texttemplate="%{percent:.1%}"
    )
    total_contribution_fig.update_layout(
        uniformtext_minsize=12, title="Channel contribution", uniformtext_mode="hide"
    )
    return total_contribution_fig


def create_contribuion_stacked_plot(scenario):
    weekly_contribution_fig = make_subplots(
        rows=1,
        cols=2,
        subplot_titles=["Spends", "Revenue"],
        specs=[[{"type": "bar"}, {"type": "bar"}]],
    )
    raw_df = st.session_state["raw_df"]
    df = raw_df.sort_values(by="Date")
    x = df.Date
    weekly_spends_data = []
    weekly_sales_data = []

    for i, channel_name in enumerate(st.session_state["channels_list"]):
        color = color_palette[i % len(color_palette)]

        weekly_spends_data.append(
            go.Bar(
                x=x,
                y=scenario.channels[channel_name].actual_spends
                * scenario.channels[channel_name].conversion_rate,
                name=channel_name_formating(channel_name),
                hovertemplate="Date:%{x}<br>Spend:%{y:$.2s}",
                legendgroup=channel_name,
                marker_color=color,
            )
        )

        weekly_sales_data.append(
            go.Bar(
                x=x,
                y=scenario.channels[channel_name].actual_sales,
                name=channel_name_formating(channel_name),
                hovertemplate="Date:%{x}<br>Revenue:%{y:$.2s}",
                legendgroup=channel_name,
                showlegend=False,
                marker_color=color,
            )
        )

    for _d in weekly_spends_data:
        weekly_contribution_fig.add_trace(_d, row=1, col=1)
    for _d in weekly_sales_data:
        weekly_contribution_fig.add_trace(_d, row=1, col=2)

    weekly_contribution_fig.add_trace(
        go.Bar(
            x=x,
            y=scenario.constant + scenario.correction,
            name="Non Media",
            hovertemplate="Date:%{x}<br>Revenue:%{y:$.2s}",
            marker_color=color_palette[-1],
        ),
        row=1,
        col=2,
    )

    weekly_contribution_fig.update_layout(
        barmode="stack", title="Channel contribution by week", xaxis_title="Date"
    )
    weekly_contribution_fig.update_xaxes(showgrid=False)
    weekly_contribution_fig.update_yaxes(showgrid=False)
    return weekly_contribution_fig


def create_channel_spends_sales_plot(channel):
    if channel is not None:
        x = channel.dates
        _spends = channel.actual_spends * channel.conversion_rate
        _sales = channel.actual_sales
        channel_sales_spends_fig = make_subplots(specs=[[{"secondary_y": True}]])
        channel_sales_spends_fig.add_trace(
            go.Bar(
                x=x,
                y=_sales,
                marker_color=color_palette[
                    3
                ],  # You can choose a color from the palette
                name="Revenue",
                hovertemplate="Date:%{x}<br>Revenue:%{y:$.2s}",
            ),
            secondary_y=False,
        )

        channel_sales_spends_fig.add_trace(
            go.Scatter(
                x=x,
                y=_spends,
                line=dict(
                    color=color_palette[2]
                ),  # You can choose another color from the palette
                name="Spends",
                hovertemplate="Date:%{x}<br>Spend:%{y:$.2s}",
            ),
            secondary_y=True,
        )

        channel_sales_spends_fig.update_layout(
            xaxis_title="Date",
            yaxis_title="Revenue",
            yaxis2_title="Spends ($)",
            title="Channel spends and Revenue week-wise",
        )
        channel_sales_spends_fig.update_xaxes(showgrid=False)
        channel_sales_spends_fig.update_yaxes(showgrid=False)
    else:
        raw_df = st.session_state["raw_df"]
        df = raw_df.sort_values(by="Date")
        x = df.Date
        scenario = class_from_dict(st.session_state["default_scenario_dict"])
        _sales = 0 #scenario.constant + scenario.correction
        channel_sales_spends_fig = make_subplots(specs=[[{"secondary_y": True}]])
        channel_sales_spends_fig.add_trace(
            go.Bar(
                x=x,
                y=_sales,
                marker_color=color_palette[
                    0
                ],  # You can choose a color from the palette
                name="Revenue",
                hovertemplate="Date:%{x}<br>Revenue:%{y:$.2s}",
            ),
            secondary_y=False,
        )

        channel_sales_spends_fig.update_layout(
            xaxis_title="Date",
            yaxis_title="Revenue",
            yaxis2_title="Spends ($)",
            title="Channel spends and Revenue week-wise",
        )
        channel_sales_spends_fig.update_xaxes(showgrid=False)
        channel_sales_spends_fig.update_yaxes(showgrid=False)

    return channel_sales_spends_fig


def format_numbers(value, n_decimals=1, include_indicator=True):
    if include_indicator:
        return f"{CURRENCY_INDICATOR} {numerize(value,n_decimals)}"
    else:
        return f"{numerize(value,n_decimals)}"

def format_numbers_f(value, n_decimals=1, include_indicator=False):
    if include_indicator:
        return f"{CURRENCY_INDICATOR} {numerize(value,n_decimals)}"
    else:
        return f"{numerize(value,n_decimals)}"

def decimal_formater(num_string, n_decimals=1):
    parts = num_string.split(".")
    if len(parts) == 1:
        return num_string + "." + "0" * n_decimals
    else:
        to_be_padded = n_decimals - len(parts[-1])
        if to_be_padded > 0:
            return num_string + "0" * to_be_padded
        else:
            return num_string


def channel_name_formating(channel_name):
    name_mod = channel_name.replace("_", " ")
    if name_mod.lower().endswith(" imp"):
        name_mod = name_mod.replace("Imp", "Spend")
    elif name_mod.lower().endswith(" clicks"):
        name_mod = name_mod.replace("Clicks", "Spend")
    # st.write(channel_name)
    key_dict = my_dict = {
        "DisplayProspecting" :"Display Prospecting",
        "CableTV" :"Cable TV",
        "SocialProspecting": "Social Prospecting",
        "Connected&OTTTV"  :"Connected & OTTTV",
        "SocialRetargeting" : "Social Retargeting",
        "DigitalPartners" :"Digital Partners",
        "Audio" :"Audio",
        "BroadcastTV": "Broadcast TV",
        "SearchNon-brand": "Search Non-brand",
        "Email" :"Email" ,
        "SearchBrand": "Search Brand",
        "DisplayRetargeting" :  "Display Retargeting" ,
        "\xa0Video":"Video"
    }
    return key_dict[channel_name]


def send_email(email, message):
    s = smtplib.SMTP("smtp.gmail.com", 587)
    s.starttls()
    s.login("[email protected]", "jgydhpfusuremcol")
    s.sendmail("[email protected]", email, message)
    s.quit()


if __name__ == "__main__":
    initialize_data()