File size: 53,210 Bytes
cf8be6f
a08f424
cf8be6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f04285a
 
 
 
 
 
 
 
cf8be6f
6aa74d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f04285a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf8be6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6991b1
 
 
cf8be6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f04285a
 
 
 
 
cf8be6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
import os
import math
from functools import lru_cache

import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import streamlit as st
from sklearn.cluster import KMeans
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier, plot_tree
import matplotlib.pyplot as plt

st.set_page_config(
    page_title="FreshWise - Perishable Retail Optimization",
    page_icon="🥗",
    layout="wide",
    initial_sidebar_state="expanded",
)

DATA_CANDIDATES = [
    os.environ.get("DATA_PATH", ""),
    "perishable_goods_management.csv",
    "/app/perishable_goods_management.csv",
    "/data/perishable_goods_management.csv",
    "/mnt/data/perishable_goods_management.csv",
]

CATEGORY_COLORS = {
    "Produce": "#2E8B57",
    "Dairy": "#1E90FF",
    "Meat": "#B22222",
    "Seafood": "#20B2AA",
    "Bakery": "#D2691E",
    "Ready_to_Eat": "#8A2BE2",
}

FOCUS_CATEGORY = "Bakery"

REGION_COORDS = {
    "West": (34.05, -118.24),
    "Northeast": (40.71, -74.00),
    "Southeast": (33.75, -84.39),
    "Midwest": (41.88, -87.63),
    "Southwest": (32.78, -96.80),
}


COLUMN_GROUPS = {
    "Identity & network": [
        "record_id", "product_id", "product_name", "category", "store_id", "region", "supplier_id"
    ],
    "Time & expiry": [
        "transaction_date", "expiration_date", "shelf_life_days", "day_of_week", "month", "days_until_expiry"
    ],
    "Storage & handling": [
        "storage_temp", "temp_deviation", "temp_abuse_events", "handling_score", "packaging_score", "spoilage_risk"
    ],
    "Demand & inventory": [
        "initial_quantity", "daily_demand", "units_sold", "leftover_units", "stockout_flag", "lost_sales_units", "sell_through_pct"
    ],
    "Pricing & promotions": [
        "base_price", "cost_price", "selling_price", "discount_pct", "markdown_applied", "is_promoted"
    ],
    "Waste & profitability": [
        "units_wasted", "waste_pct", "profit", "profit_margin_pct"
    ]
}




def region_anchor(region: str):
    return REGION_COORDS.get(region, (39.0, -96.0))


def attach_store_locations(df: pd.DataFrame) -> pd.DataFrame:
    stores = sorted(df["store_id"].dropna().unique())
    rows = []
    for store in stores:
        sub = df[df["store_id"] == store]
        region = str(sub["region"].mode().iloc[0]) if not sub.empty else "West"
        base_lat, base_lon = region_anchor(region)
        seed = abs(hash(store)) % 10000
        rng = np.random.default_rng(seed)
        rows.append(
            {
                "store_id": store,
                "store_lat": base_lat + rng.uniform(-0.55, 0.55),
                "store_lon": base_lon + rng.uniform(-0.75, 0.75),
            }
        )
    loc = pd.DataFrame(rows)
    return df.merge(loc, on="store_id", how="left")


def find_data_path() -> str:
    for path in DATA_CANDIDATES:
        if path and os.path.exists(path):
            return path
    raise FileNotFoundError(
        "perishable_goods_management.csv not found. Put it next to app.py or set DATA_PATH."
    )


@st.cache_data(show_spinner=False)
def load_data() -> pd.DataFrame:
    path = find_data_path()
    df = pd.read_csv(path)

    df["transaction_date"] = pd.to_datetime(df["transaction_date"], errors="coerce")
    df["expiration_date"] = pd.to_datetime(df["expiration_date"], errors="coerce")

    df["sell_through_pct"] = np.where(
        df["initial_quantity"] > 0, df["units_sold"] / df["initial_quantity"], 0
    )
    df["stock_demand_ratio"] = np.where(
        df["daily_demand"] > 0, df["initial_quantity"] / df["daily_demand"], np.nan
    )
    df["gross_margin"] = df["selling_price"] - df["cost_price"]
    df["leftover_units"] = (df["initial_quantity"] - df["units_sold"]).clip(lower=0)
    df["stockout_flag"] = (df["daily_demand"] > df["initial_quantity"]).astype(int)
    df["lost_sales_units"] = (df["daily_demand"] - df["units_sold"]).clip(lower=0)
    if "waste_pct" in df.columns and df["waste_pct"].max() > 1:
        df["waste_pct"] = df["waste_pct"] / 100
    df["waste_pct"] = df["waste_pct"].clip(lower=0, upper=1)
    df["value_score"] = (
        (1 - df["waste_pct"].clip(0, 1)) * 0.35
        + df["profit_margin_pct"].clip(lower=0) / 100 * 0.25
        + (1 - df["days_until_expiry"].clip(upper=14) / 14) * 0.15
        + df["discount_pct"].clip(0, 0.5) * 0.25
    )
    df["expiry_bucket"] = pd.cut(
        df["days_until_expiry"],
        bins=[-1, 1, 3, 7, 30, 10_000],
        labels=["<=1d", "2-3d", "4-7d", "8-30d", ">30d"],
    )
    df["high_waste_flag"] = (df["waste_pct"] >= df["waste_pct"].quantile(0.75)).astype(int)
    df["waste_high"] = (df["waste_pct"] > df["waste_pct"].median()).astype(int)
    df["profit_high"] = (df["profit"] > df["profit"].median()).astype(int)
    df["promo_effective"] = ((df["is_promoted"] == 1) & (df["sell_through_pct"] > df["sell_through_pct"].median())).astype(int)
    return df


@st.cache_data(show_spinner=False)
def fit_segments(df: pd.DataFrame) -> pd.DataFrame:
    work = df[[
        "daily_demand",
        "initial_quantity",
        "waste_pct",
        "shelf_life_days",
        "stock_demand_ratio",
        "sell_through_pct",
    ]].replace([np.inf, -np.inf], np.nan).dropna().copy()

    sample_size = min(len(work), 20000)
    work = work.sample(sample_size, random_state=42)
    scaler = StandardScaler()
    X = scaler.fit_transform(work)
    km = KMeans(n_clusters=4, random_state=42, n_init=10)
    work["cluster"] = km.fit_predict(X)
    return work


@st.cache_resource(show_spinner=False)
def fit_risk_model(df: pd.DataFrame):
    features = [
        "daily_demand",
        "initial_quantity",
        "shelf_life_days",
        "days_until_expiry",
        "temp_deviation",
        "temp_abuse_events",
        "handling_score",
        "packaging_score",
        "spoilage_risk",
        "discount_pct",
        "markdown_applied",
        "is_weekend",
        "supplier_score",
    ]
    X = df[features]
    y = df["high_waste_flag"]
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.2, random_state=42, stratify=y
    )
    model = RandomForestClassifier(
        n_estimators=120, random_state=42, n_jobs=-1, max_depth=10
    )
    model.fit(X_train, y_train)
    importances = pd.Series(model.feature_importances_, index=features).sort_values(ascending=False)
    return model, importances


@lru_cache(maxsize=1)
def cluster_name_map():
    return {
        0: "Stable performers",
        1: "Overstocked slow movers",
        2: "Short-life high risk",
        3: "High demand fast movers",
    }


def apply_filters(df: pd.DataFrame):
    st.sidebar.header("Filters")

    if "filter_regions" not in st.session_state:
        st.session_state["filter_regions"] = []
    if "filter_stores" not in st.session_state:
        st.session_state["filter_stores"] = []

    all_regions = sorted(df["region"].dropna().unique())
    all_stores = sorted(df["store_id"].dropna().unique())

    # If the user selected stores directly, infer the matching region(s).
    if st.session_state["filter_stores"] and not st.session_state["filter_regions"]:
        inferred_regions = sorted(
            df.loc[df["store_id"].isin(st.session_state["filter_stores"]), "region"]
            .dropna()
            .unique()
        )
        st.session_state["filter_regions"] = inferred_regions

    # Region selection drives store options.
    regions = st.sidebar.multiselect(
        "Region",
        all_regions,
        key="filter_regions",
    )

    available_stores = sorted(
        df.loc[df["region"].isin(regions), "store_id"].dropna().unique()
    ) if regions else all_stores

    # Keep only stores that still belong to the selected region(s).
    st.session_state["filter_stores"] = [
        s for s in st.session_state["filter_stores"] if s in available_stores
    ]

    stores = st.sidebar.multiselect(
        "Store",
        available_stores,
        key="filter_stores",
    )

    # If stores are selected, make region selection follow them exactly.
    if stores:
        inferred_regions = sorted(
            df.loc[df["store_id"].isin(stores), "region"].dropna().unique()
        )
        if inferred_regions != regions:
            st.session_state["filter_regions"] = inferred_regions
            regions = inferred_regions

    categories = st.sidebar.multiselect("Category", sorted(df["category"].dropna().unique()), default=[])
    expiry_range = st.sidebar.slider("Days until expiry", 0, int(df["days_until_expiry"].max()), (0, 30))
    weekend_choice = st.sidebar.selectbox("Day type", ["All", "Weekday", "Weekend"])

    filtered = df.copy()
    if regions:
        filtered = filtered[filtered["region"].isin(regions)]
    if stores:
        filtered = filtered[filtered["store_id"].isin(stores)]
    if categories:
        filtered = filtered[filtered["category"].isin(categories)]
    filtered = filtered[
        (filtered["days_until_expiry"] >= expiry_range[0])
        & (filtered["days_until_expiry"] <= expiry_range[1])
    ]
    if weekend_choice == "Weekday":
        filtered = filtered[filtered["is_weekend"] == 0]
    elif weekend_choice == "Weekend":
        filtered = filtered[filtered["is_weekend"] == 1]
    return filtered


def metric_row(df: pd.DataFrame):
    c1, c2, c3, c4, c5 = st.columns(5)
    c1.metric("Waste %", f"{df['waste_pct'].mean():.1%}")
    c2.metric("Profit", f"€{df['profit'].mean():.2f}")
    c3.metric("Sell-through", f"{df['sell_through_pct'].mean():.1%}")
    c4.metric("Units wasted", f"{df['units_wasted'].mean():.1f}")
    c5.metric("Markdown rate", f"{df['markdown_applied'].mean():.1%}")


def manager_dashboard(df: pd.DataFrame):
    st.subheader("Manager Mode")
    metric_row(df)

    a, b = st.columns([1.2, 1])
    with a:
        trend = df.groupby(df["transaction_date"].dt.to_period("M").astype(str))[["waste_pct", "profit"]].mean().reset_index()
        fig = go.Figure()
        fig.add_trace(go.Scatter(x=trend["transaction_date"], y=trend["waste_pct"], name="Waste %", mode="lines+markers"))
        fig.add_trace(go.Scatter(x=trend["transaction_date"], y=trend["profit"], name="Profit", mode="lines+markers", yaxis="y2"))
        fig.update_layout(
            title="Monthly Waste and Profit Trend",
            yaxis=dict(title="Waste %"),
            yaxis2=dict(title="Profit", overlaying="y", side="right"),
            legend=dict(orientation="h"),
            margin=dict(l=10, r=10, t=40, b=10),
        )
        st.plotly_chart(fig, use_container_width=True)
    with b:
        top_risk = (
            df.groupby("category")[["waste_pct", "profit", "stock_demand_ratio"]]
            .mean()
            .sort_values("waste_pct", ascending=False)
            .head(8)
            .reset_index()
        )
        fig = px.bar(top_risk, x="waste_pct", y="category", orientation="h", title="High Waste Categories")
        st.plotly_chart(fig, use_container_width=True)

    c1, c2 = st.columns(2)
    with c1:
        store_risk = (
            df.groupby("store_id")[["waste_pct", "profit", "temp_deviation"]]
            .mean()
            .sort_values(["waste_pct", "temp_deviation"], ascending=[False, False])
            .head(15)
            .reset_index()
        )
        st.dataframe(store_risk, use_container_width=True, hide_index=True)
    with c2:
        expiry = df.groupby("expiry_bucket")[["waste_pct", "profit", "discount_pct"]].mean().reset_index()
        fig = px.line(expiry, x="expiry_bucket", y=["waste_pct", "profit", "discount_pct"], markers=True, title="Expiry Stage Performance")
        st.plotly_chart(fig, use_container_width=True)



def forecast_region_demand(cat_df: pd.DataFrame, region: str) -> pd.DataFrame:
    d = cat_df[cat_df["region"] == region].copy()
    if d.empty:
        return pd.DataFrame()
    ts = d.groupby("transaction_date")["daily_demand"].mean().reset_index().sort_values("transaction_date")
    if len(ts) < 14:
        return pd.DataFrame()
    recent = ts.tail(56).copy()
    weekday_avg = recent.groupby(recent["transaction_date"].dt.dayofweek)["daily_demand"].mean().to_dict()
    last_date = ts["transaction_date"].max()
    future_dates = pd.date_range(last_date + pd.Timedelta(days=1), periods=14, freq="D")
    future = pd.DataFrame({
        "transaction_date": future_dates,
        "daily_demand": [weekday_avg.get(d.dayofweek, ts["daily_demand"].tail(14).mean()) for d in future_dates],
        "series": "Forecast"
    })
    hist = ts.tail(60).copy()
    hist["series"] = "Actual"
    return pd.concat([hist, future], ignore_index=True)



def build_weekpart_inventory_views(cat_df: pd.DataFrame):
    tmp = cat_df.copy()
    tmp["week_part"] = np.where(tmp["is_weekend"] == 1, "Weekend", "Weekday")

    store_view = (
        tmp.groupby(["store_id", "region", "week_part"])
        .agg(
            avg_inventory=("initial_quantity", "mean"),
            avg_remaining=("leftover_units", "mean"),
            avg_demand=("daily_demand", "mean"),
            avg_units_sold=("units_sold", "mean"),
            sell_through=("sell_through_pct", "mean"),
            stockout_rate=("stockout_flag", "mean"),
            unmet_demand=("lost_sales_units", "mean"),
            waste_pct=("waste_pct", "mean"),
            units_wasted=("units_wasted", "mean"),
            avg_profit=("profit", "mean"),
            avg_margin=("profit_margin_pct", "mean"),
            markdown_rate=("markdown_applied", "mean"),
            promo_rate=("is_promoted", "mean"),
            avg_days_until_expiry=("days_until_expiry", "mean"),
            temp_dev=("temp_deviation", "mean"),
            spoilage_risk=("spoilage_risk", "mean"),
        )
        .reset_index()
    )

    region_view = (
        tmp.groupby(["region", "week_part"])
        .agg(
            avg_inventory=("initial_quantity", "mean"),
            avg_remaining=("leftover_units", "mean"),
            avg_demand=("daily_demand", "mean"),
            avg_units_sold=("units_sold", "mean"),
            sell_through=("sell_through_pct", "mean"),
            stockout_rate=("stockout_flag", "mean"),
            unmet_demand=("lost_sales_units", "mean"),
            waste_pct=("waste_pct", "mean"),
            units_wasted=("units_wasted", "mean"),
            avg_profit=("profit", "mean"),
            avg_margin=("profit_margin_pct", "mean"),
            markdown_rate=("markdown_applied", "mean"),
            promo_rate=("is_promoted", "mean"),
            avg_days_until_expiry=("days_until_expiry", "mean"),
            temp_dev=("temp_deviation", "mean"),
            spoilage_risk=("spoilage_risk", "mean"),
        )
        .reset_index()
    )

    return store_view, region_view

def manager_category_intelligence(df: pd.DataFrame):
    st.subheader("Category Intelligence")
    categories = sorted(df["category"].dropna().unique())
    default_idx = categories.index(FOCUS_CATEGORY) if FOCUS_CATEGORY in categories else 0
    focus = st.selectbox("Focus category", categories, index=default_idx)
    cat_df = df[df["category"] == focus].copy()

    if cat_df.empty:
        st.warning("No data for the selected category.")
        return

    st.markdown(
        f"Selected category: **{focus}**. This page compares regional operations, inventory, profitability, demand, stockout and waste trade-offs for a distinctive perishable category."
    )

    c1, c2, c3, c4, c5, c6 = st.columns(6)
    c1.metric("Avg demand", f"{cat_df['daily_demand'].mean():.1f}")
    c2.metric("Avg stock", f"{cat_df['initial_quantity'].mean():.1f}")
    c3.metric("Avg remaining", f"{cat_df['leftover_units'].mean():.1f}")
    c4.metric("Sell-through", f"{cat_df['sell_through_pct'].mean():.1%}")
    c5.metric("Stockout rate", f"{cat_df['stockout_flag'].mean():.1%}")
    c6.metric("Waste rate", f"{cat_df['waste_pct'].mean():.1%}")

    with st.expander("42-column feature map grouped into business themes"):
        for group, cols in COLUMN_GROUPS.items():
            st.markdown(f"**{group}**")
            st.code(", ".join(cols), language=None)

    region_summary = (
        cat_df.groupby("region")
        .agg(
            avg_demand=("daily_demand", "mean"),
            avg_stock=("initial_quantity", "mean"),
            avg_remaining=("leftover_units", "mean"),
            avg_units_sold=("units_sold", "mean"),
            sell_through=("sell_through_pct", "mean"),
            avg_profit=("profit", "mean"),
            avg_margin=("profit_margin_pct", "mean"),
            waste_pct=("waste_pct", "mean"),
            units_wasted=("units_wasted", "mean"),
            markdown_rate=("markdown_applied", "mean"),
            promo_rate=("is_promoted", "mean"),
            temp_dev=("temp_deviation", "mean"),
            shelf_life=("shelf_life_days", "mean"),
            days_until_expiry=("days_until_expiry", "mean"),
            stockout_rate=("stockout_flag", "mean"),
            lost_sales=("lost_sales_units", "mean"),
            spoilage_risk=("spoilage_risk", "mean"),
        )
        .reset_index()
    )

    store_summary = (
        cat_df.groupby(["store_id", "region"])
        .agg(
            avg_inventory=("initial_quantity", "mean"),
            avg_remaining=("leftover_units", "mean"),
            avg_demand=("daily_demand", "mean"),
            avg_units_sold=("units_sold", "mean"),
            sell_through=("sell_through_pct", "mean"),
            avg_profit=("profit", "mean"),
            avg_margin=("profit_margin_pct", "mean"),
            waste_pct=("waste_pct", "mean"),
            units_wasted=("units_wasted", "mean"),
            stockout_rate=("stockout_flag", "mean"),
            lost_sales=("lost_sales_units", "mean"),
            markdown_rate=("markdown_applied", "mean"),
            promo_rate=("is_promoted", "mean"),
            avg_expiry_days=("days_until_expiry", "mean"),
            temp_dev=("temp_deviation", "mean"),
        )
        .reset_index()
    )

    a, b = st.columns([1.2, 1])
    with a:
        melt = region_summary.melt(
            id_vars="region",
            value_vars=["avg_demand", "avg_stock", "avg_profit"],
            var_name="metric",
            value_name="value",
        )
        fig = px.bar(
            melt,
            x="region",
            y="value",
            color="metric",
            barmode="group",
            title=f"{focus}: regional operations, inventory and profit comparison",
        )
        st.plotly_chart(fig, use_container_width=True)
    with b:
        fig = px.scatter(
            region_summary,
            x="stockout_rate",
            y="waste_pct",
            size="avg_profit",
            color="region",
            hover_data=[
                "avg_demand", "avg_stock", "avg_remaining", "lost_sales", "days_until_expiry", "spoilage_risk"
            ],
            title=f"{focus}: stockout vs waste trade-off by region",
        )
        st.plotly_chart(fig, use_container_width=True)

    c1, c2 = st.columns([1, 1.2])
    with c1:
        st.markdown("### Regional KPI table")
        st.dataframe(
            region_summary.sort_values("avg_profit", ascending=False),
            use_container_width=True,
            hide_index=True,
        )
    with c2:
        region_choice = st.selectbox("Forecast region", sorted(cat_df["region"].dropna().unique()))
        forecast_df = forecast_region_demand(cat_df, region_choice)
        if not forecast_df.empty:
            fig = px.line(
                forecast_df,
                x="transaction_date",
                y="daily_demand",
                color="series",
                title=f"{focus}: 60-day actual + 14-day demand forecast for {region_choice}",
            )
            st.plotly_chart(fig, use_container_width=True)

    st.markdown("### Same-store / same-region weekday vs weekend inventory analysis")
    store_weekpart, region_weekpart = build_weekpart_inventory_views(cat_df)

    selected_store = st.selectbox(
        "Select store for within-store weekday/weekend comparison",
        sorted(store_weekpart["store_id"].unique())
    )

    d1, d2 = st.columns([1.2, 1])
    with d1:
        region_week_melt = region_weekpart.melt(
            id_vars=["region", "week_part"],
            value_vars=[
                "avg_inventory", "avg_demand", "avg_remaining",
                "avg_profit", "stockout_rate", "waste_pct"
            ],
            var_name="metric",
            value_name="value",
        )
        fig = px.bar(
            region_week_melt,
            x="region",
            y="value",
            color="week_part",
            facet_row="metric",
            barmode="group",
            title=f"{focus}: same-region weekday vs weekend comparison",
            height=1100,
        )
        fig.update_yaxes(matches=None)
        st.plotly_chart(fig, use_container_width=True)
    with d2:
        store_focus = store_weekpart[store_weekpart["store_id"] == selected_store].copy()
        if not store_focus.empty:
            fig = px.bar(
                store_focus.melt(
                    id_vars=["store_id", "region", "week_part"],
                    value_vars=[
                        "avg_inventory", "avg_demand", "avg_remaining",
                        "unmet_demand", "stockout_rate", "waste_pct", "avg_profit"
                    ],
                    var_name="metric",
                    value_name="value",
                ),
                x="metric",
                y="value",
                color="week_part",
                barmode="group",
                title=f"{selected_store}: weekday vs weekend store comparison",
            )
            st.plotly_chart(fig, use_container_width=True)

    e1, e2 = st.columns([1.05, 1.15])
    with e1:
        region_week_pivot = region_weekpart.pivot(
            index="region",
            columns="week_part",
            values=[
                "avg_inventory", "avg_remaining", "avg_demand", "avg_units_sold",
                "sell_through", "stockout_rate", "unmet_demand", "waste_pct",
                "units_wasted", "avg_profit", "avg_margin", "markdown_rate",
                "promo_rate", "avg_days_until_expiry", "temp_dev", "spoilage_risk"
            ],
        )
        region_week_pivot.columns = [f"{a}_{b}" for a, b in region_week_pivot.columns]
        region_week_pivot = region_week_pivot.reset_index()

        delta_metrics = [
            "avg_inventory", "avg_remaining", "avg_demand", "avg_units_sold",
            "sell_through", "stockout_rate", "unmet_demand", "waste_pct",
            "units_wasted", "avg_profit", "avg_margin", "markdown_rate",
            "promo_rate", "avg_days_until_expiry", "temp_dev", "spoilage_risk"
        ]
        for metric in delta_metrics:
            wd = f"{metric}_Weekday"
            we = f"{metric}_Weekend"
            if wd in region_week_pivot.columns and we in region_week_pivot.columns:
                region_week_pivot[f"{metric}_weekend_minus_weekday"] = region_week_pivot[we] - region_week_pivot[wd]

        st.markdown("### Region weekday/weekend delta table")
        st.dataframe(region_week_pivot, use_container_width=True, hide_index=True)
    with e2:
        store_focus_table = store_weekpart[store_weekpart["store_id"] == selected_store].copy()
        if not store_focus_table.empty:
            st.markdown(f"### {selected_store} detailed weekday/weekend indicators")
            st.dataframe(store_focus_table, use_container_width=True, hide_index=True)

    st.markdown("### Important extracted indicators")
    weekday_view = region_weekpart[region_weekpart["week_part"] == "Weekday"].set_index("region")
    weekend_view = region_weekpart[region_weekpart["week_part"] == "Weekend"].set_index("region")
    common_regions = sorted(set(weekday_view.index).intersection(set(weekend_view.index)))

    if common_regions:
        for region in common_regions:
            wd = weekday_view.loc[region]
            we = weekend_view.loc[region]
            pieces = []
            demand_delta = we["avg_demand"] - wd["avg_demand"]
            inventory_delta = we["avg_inventory"] - wd["avg_inventory"]
            remaining_delta = we["avg_remaining"] - wd["avg_remaining"]
            stockout_delta = we["stockout_rate"] - wd["stockout_rate"]
            waste_delta = we["waste_pct"] - wd["waste_pct"]
            profit_delta = we["avg_profit"] - wd["avg_profit"]
            markdown_delta = we["markdown_rate"] - wd["markdown_rate"]
            promo_delta = we["promo_rate"] - wd["promo_rate"]

            if demand_delta > 0:
                pieces.append(f"weekend demand is higher by {demand_delta:.1f}")
            else:
                pieces.append(f"weekday demand is higher by {abs(demand_delta):.1f}")

            if inventory_delta > 0:
                pieces.append(f"weekend inventory is higher by {inventory_delta:.1f}")
            else:
                pieces.append(f"weekday inventory is higher by {abs(inventory_delta):.1f}")

            if remaining_delta > 0:
                pieces.append(f"weekend leftover stock rises by {remaining_delta:.1f}")
            elif remaining_delta < 0:
                pieces.append(f"weekday leftover stock rises by {abs(remaining_delta):.1f}")

            if stockout_delta > 0.01:
                pieces.append(f"weekend stockout risk is worse by {stockout_delta:.1%}")
            elif stockout_delta < -0.01:
                pieces.append(f"weekday stockout risk is worse by {abs(stockout_delta):.1%}")

            if waste_delta > 0.01:
                pieces.append(f"weekend waste is higher by {waste_delta:.1%}")
            elif waste_delta < -0.01:
                pieces.append(f"weekday waste is higher by {abs(waste_delta):.1%}")

            pieces.append(f"profit shifts by €{profit_delta:.2f} from weekday to weekend")
            pieces.append(f"markdown changes by {markdown_delta:.1%} on weekends")
            pieces.append(f"promotion rate changes by {promo_delta:.1%} on weekends")

            st.markdown(f"- **{region}**: " + "; ".join(pieces) + ".")

    st.markdown("### Regional recommendations")
    mean_stockout = region_summary["stockout_rate"].mean()
    mean_waste = region_summary["waste_pct"].mean()
    mean_margin = region_summary["avg_margin"].mean()
    mean_temp = region_summary["temp_dev"].mean()
    mean_expiry = region_summary["days_until_expiry"].mean()

    for _, r in region_summary.iterrows():
        advice = []
        if r["stockout_rate"] > mean_stockout:
            advice.append("raise replenishment and morning safety stock")
        if r["waste_pct"] > mean_waste:
            advice.append("start markdown earlier")
        if r["avg_margin"] < mean_margin:
            advice.append("use bundles instead of deeper discounts")
        if r["temp_dev"] > mean_temp:
            advice.append("tighten storage handling")
        if r["days_until_expiry"] < mean_expiry:
            advice.append("prioritize fresher inbound allocation")
        if not advice:
            advice.append("maintain and scale current playbook")
        st.markdown(f"- **{r['region']}**: " + "; ".join(advice) + ".")

    st.markdown("### Marketing design simulator")
    m1, m2, m3, m4 = st.columns(4)
    promo_region = m1.selectbox("Target region", sorted(cat_df["region"].dropna().unique()), key="cat_region")
    promo_type = m2.selectbox("Promo type", ["Early markdown", "Breakfast bundle", "Happy-hour discount", "Loyalty coupon"])
    discount = m3.slider("Discount %", 0, 40, 15, key="cat_discount")
    duration = m4.slider("Duration (days)", 1, 10, 4, key="cat_duration")

    base = cat_df[cat_df["region"] == promo_region].copy()
    base_sales = base["units_sold"].mean()
    base_waste = base["waste_pct"].mean()
    base_profit = base["profit"].mean()
    promo_factor = {"Early markdown": 0.12, "Breakfast bundle": 0.16, "Happy-hour discount": 0.10, "Loyalty coupon": 0.08}[promo_type]
    sales_lift = promo_factor + discount / 180 + min(duration / 60, 0.10)
    waste_drop = min(0.42, promo_factor + discount / 200)
    margin_drag = discount / 160 * (0.75 if promo_type == "Breakfast bundle" else 1.0)
    est_sales = base_sales * (1 + sales_lift)
    est_waste = max(base_waste * (1 - waste_drop), 0)
    est_profit = base_profit * (1 + sales_lift - margin_drag)

    x1, x2, x3 = st.columns(3)
    x1.metric("Estimated avg units sold", f"{est_sales:.2f}", delta=f"+{(est_sales-base_sales):.2f}")
    x2.metric("Estimated waste", f"{est_waste:.1%}", delta=f"-{(base_waste-est_waste):.1%}")
    x3.metric("Estimated avg profit", f"€{est_profit:.2f}", delta=f"€{(est_profit-base_profit):.2f}")

def generate_summary(df: pd.DataFrame) -> str:
    waste = df["waste_pct"].mean()
    profit = df["profit"].mean()
    stockout = (df["daily_demand"] > df["initial_quantity"]).mean()
    worst_region = df.groupby("region")["waste_pct"].mean().idxmax()
    best_region = df.groupby("region")["profit"].mean().idxmax()
    return f"""
- Average waste rate is **{waste:.1%}**, indicating {'high inefficiency' if waste > 0.2 else 'acceptable performance'}.
- Average profit is **EUR {profit:.2f}**, with strongest performance in **{best_region}**.
- Stockout rate is **{stockout:.1%}**, suggesting {'understocking risk' if stockout > 0.2 else 'balanced supply'}.

Key issue:
- Highest waste occurs in **{worst_region}**.

Recommended actions:
- Advance markdown timing for short-life products.
- Rebalance inventory using demand signals.
- Use bundles instead of deeper discounts where possible.
"""


def generate_slide_insights(df: pd.DataFrame):
    insights = []
    if df["waste_pct"].mean() > 0.2:
        insights.append("High waste is driven by short shelf-life items and delayed markdown timing.")
    if (df["daily_demand"] > df["initial_quantity"]).mean() > 0.2:
        insights.append("Frequent stockouts indicate under-forecasting of demand in key regions.")
    if df["discount_pct"].mean() > 0.25:
        insights.append("Over-reliance on discounting is reducing margin quality.")
    if df["temp_deviation"].mean() > 2:
        insights.append("Temperature deviation is materially contributing to spoilage risk.")
    if not insights:
        insights.append("Current performance is stable, with room to optimize promotion quality and inventory precision.")
    return insights


def build_transfer_suggestions(view_df: pd.DataFrame, full_df: pd.DataFrame) -> tuple[pd.DataFrame, pd.DataFrame]:
    if "store_lat" not in view_df.columns or "store_lon" not in view_df.columns:
        view_df = attach_store_locations(view_df.copy())
    if "store_lat" not in full_df.columns or "store_lon" not in full_df.columns:
        full_df = attach_store_locations(full_df.copy())

    store_summary = (
        view_df.groupby(["store_id", "region"])
        .agg(
            total_inventory=("initial_quantity", "sum"),
            units_sold=("units_sold", "sum"),
            remaining_inventory=("leftover_units", "sum"),
            total_demand=("daily_demand", "sum"),
            unmet_demand=("lost_sales_units", "sum"),
        )
        .reset_index()
    )
    store_summary["inventory_gap"] = store_summary["remaining_inventory"] - store_summary["unmet_demand"]

    receiving_need = (
        view_df.groupby(["store_id", "region", "category", "store_lat", "store_lon"])
        .agg(
            remaining_inventory=("leftover_units", "sum"),
            demand=("daily_demand", "sum"),
            unmet_demand=("lost_sales_units", "sum"),
            receiver_days_until_expiry=("days_until_expiry", "mean"),
        )
        .reset_index()
    )
    receiving_need = receiving_need[receiving_need["unmet_demand"] > 0].copy()
    if receiving_need.empty:
        return store_summary, pd.DataFrame(columns=[
            "store_id", "region", "category", "remaining_inventory", "demand", "unmet_demand",
            "recommended_transfer_qty", "same_region_options", "cross_region_options", "best_route"
        ])

    donor_pool = (
        full_df.groupby(["store_id", "region", "category", "store_lat", "store_lon"])
        .agg(
            donor_remaining=("leftover_units", "sum"),
            donor_demand=("daily_demand", "sum"),
            donor_days_until_expiry=("days_until_expiry", "mean"),
        )
        .reset_index()
    )
    donor_pool["surplus_qty"] = donor_pool["donor_remaining"] - donor_pool["donor_demand"]
    donor_pool = donor_pool[donor_pool["surplus_qty"] > 0].copy()

    def distance_km(lat1, lon1, lat2, lon2):
        # simple equirectangular approximation, good enough for prioritization
        x = (math.radians(lon2) - math.radians(lon1)) * math.cos((math.radians(lat1) + math.radians(lat2)) / 2)
        y = math.radians(lat2) - math.radians(lat1)
        return 6371 * math.sqrt(x * x + y * y)

    rows = []
    for _, r in receiving_need.iterrows():
        donors = donor_pool[
            (donor_pool["category"] == r["category"]) &
            (donor_pool["store_id"] != r["store_id"]) &
            (donor_pool["surplus_qty"] > 0)
        ].copy()

        if donors.empty:
            row = r.to_dict()
            row["recommended_transfer_qty"] = 0
            row["same_region_options"] = "No same-region donor"
            row["cross_region_options"] = "No cross-region donor"
            row["best_route"] = "No feasible transfer"
            rows.append(row)
            continue

        donors["priority_rank"] = (donors["region"] != r["region"]).astype(int)  # same region first
        donors["distance_km"] = donors.apply(
            lambda d: distance_km(r["store_lat"], r["store_lon"], d["store_lat"], d["store_lon"]),
            axis=1
        )
        # Prefer donors with more remaining shelf life after distance/region
        donors = donors.sort_values(
            ["priority_rank", "distance_km", "donor_days_until_expiry", "surplus_qty"],
            ascending=[True, True, False, False]
        )

        same_region = donors[donors["priority_rank"] == 0].head(3)
        cross_region = donors[donors["priority_rank"] == 1].head(3)

        best = donors.iloc[0]
        transfer_qty = int(min(r["unmet_demand"], max(best["surplus_qty"], 0)))

        def donor_label(d):
            tier = "same-region" if d["priority_rank"] == 0 else "cross-region"
            return f"{d['store_id']} ({tier}, {d['distance_km']:.0f} km, expiry {d['donor_days_until_expiry']:.1f}d, surplus {int(d['surplus_qty'])})"

        same_region_text = "; ".join(donor_label(d) for _, d in same_region.iterrows()) if not same_region.empty else "No same-region donor"
        cross_region_text = "; ".join(donor_label(d) for _, d in cross_region.iterrows()) if not cross_region.empty else "No cross-region donor"
        best_route = donor_label(best)

        row = r.to_dict()
        row["recommended_transfer_qty"] = transfer_qty
        row["same_region_options"] = same_region_text
        row["cross_region_options"] = cross_region_text
        row["best_route"] = best_route
        rows.append(row)

    transfer_df = pd.DataFrame(rows)
    return store_summary, transfer_df



def train_decision_tree(df: pd.DataFrame):
    features = ["daily_demand", "initial_quantity", "days_until_expiry", "temp_deviation", "discount_pct"]
    X = df[features]
    y = df["high_waste_flag"]
    model = DecisionTreeClassifier(max_depth=4, random_state=42)
    model.fit(X, y)
    return model, features


def manager_summary(df: pd.DataFrame):
    st.subheader("Executive Summary")
    st.markdown(generate_summary(df))
    st.markdown("### Slide-ready insights")
    for ins in generate_slide_insights(df):
        st.success(ins)


def manager_diagnose(df: pd.DataFrame):
    st.subheader("Diagnose")
    st.markdown("Use the custom thresholds below to define what counts as **high waste** and **high profit** for the current filtered data.")
    w1, w2 = st.columns(2)
    with w1:
        waste_threshold = st.number_input(
            "High waste threshold (waste_pct)",
            min_value=0.0,
            value=float(df["waste_pct"].median()),
            step=0.01,
            format="%.3f",
            help="Rows with waste_pct above this value are classified as High Waste.",
        )
    with w2:
        profit_threshold = st.number_input(
            "High profit threshold (profit)",
            value=float(df["profit"].median()),
            step=1.0,
            format="%.2f",
            help="Rows with profit above this value are classified as High Profit.",
        )

    diag = df.copy()
    diag["waste_high_custom"] = (diag["waste_pct"] > waste_threshold).astype(int)
    diag["profit_high_custom"] = (diag["profit"] > profit_threshold).astype(int)

    st.info(
        f"Current rule: High Waste = waste_pct > {waste_threshold:.3f}; High Profit = profit > {profit_threshold:.2f}. "
        f"Promotion effectiveness remains defined as promoted items whose sell-through is above the filtered median."
    )

    c1, c2, c3 = st.columns(3)
    c1.metric("High waste share", f"{diag['waste_high_custom'].mean():.1%}")
    c2.metric("High profit share", f"{diag['profit_high_custom'].mean():.1%}")
    c3.metric("Effective promo share", f"{diag['promo_effective'].mean():.1%}")

    tree_df = diag.copy()
    tree_df["high_waste_flag"] = tree_df["waste_high_custom"]
    model, features = train_decision_tree(tree_df)
    fig, ax = plt.subplots(figsize=(12, 6))
    plot_tree(model, feature_names=features, class_names=["Low Waste", "High Waste"], filled=True, ax=ax)
    st.pyplot(fig)
    plt.close(fig)

    importance_df = pd.DataFrame({"feature": features, "importance": model.feature_importances_}).sort_values("importance", ascending=False)
    fig2 = px.bar(importance_df, x="importance", y="feature", orientation="h", title="Decision Tree Split Importance")
    st.plotly_chart(fig2, use_container_width=True)

    st.markdown("### Classification views")
    c4, c5 = st.columns(2)
    with c4:
        waste_by_region = diag.groupby("region")[["waste_high_custom", "profit_high_custom"]].mean().reset_index()
        melt = waste_by_region.melt(id_vars="region", var_name="label", value_name="rate")
        fig3 = px.bar(melt, x="region", y="rate", color="label", barmode="group", title="High Waste vs High Profit by Region")
        st.plotly_chart(fig3, use_container_width=True)
    with c5:
        promo_by_cat = diag.groupby("category")["promo_effective"].mean().sort_values(ascending=False).reset_index()
        fig4 = px.bar(promo_by_cat, x="promo_effective", y="category", orientation="h", title="Promotion Effectiveness by Category")
        st.plotly_chart(fig4, use_container_width=True)


def manager_inventory(df: pd.DataFrame, full_df: pd.DataFrame):
    st.subheader("Inventory & Replenishment")
    store_summary, transfer_df = build_transfer_suggestions(df, full_df)

    overstock = df.copy()
    overstock["recommended_order_qty"] = (
        1.2 * overstock["daily_demand"] * (1 + overstock["demand_variability"])
        - overstock["leftover_units"]
    )
    overstock.loc[overstock["shelf_life_days"] <= 7, "recommended_order_qty"] *= 0.7
    overstock.loc[overstock["spoilage_risk"] >= overstock["spoilage_risk"].quantile(0.75), "recommended_order_qty"] *= 0.8
    overstock["recommended_order_qty"] = overstock["recommended_order_qty"].clip(lower=0).round()

    c1, c2 = st.columns([1.3, 1])
    with c1:
        category_summary = overstock.groupby("category")[["initial_quantity", "recommended_order_qty", "waste_pct", "profit"]].mean().reset_index()
        category_summary["order_reduction_pct"] = 1 - category_summary["recommended_order_qty"] / category_summary["initial_quantity"]
        fig = px.bar(
            category_summary.sort_values("order_reduction_pct", ascending=False),
            x="order_reduction_pct",
            y="category",
            orientation="h",
            title="Recommended Order Reduction by Category",
        )
        st.plotly_chart(fig, use_container_width=True)
    with c2:
        st.markdown("**Action shortlist**")
        shortlist = overstock.sort_values(["waste_pct", "stock_demand_ratio"], ascending=[False, False])[[
            "store_id", "product_name", "category", "initial_quantity", "daily_demand",
            "days_until_expiry", "waste_pct", "recommended_order_qty"
        ]].head(20)
        st.dataframe(shortlist, use_container_width=True, hide_index=True)

    st.markdown("### What-if Simulator")
    col1, col2, col3 = st.columns(3)
    selected_category = col1.selectbox("Category for simulation", sorted(df["category"].unique()))
    order_cut = col2.slider("Reduce order quantity by %", 0, 40, 10)
    markdown_shift = col3.slider("Advance markdown trigger by days", 0, 5, 2)

    sim = df[df["category"] == selected_category].copy()
    current_waste = sim["waste_pct"].mean()
    current_profit = sim["profit"].mean()

    waste_reduction = 0.35 * (order_cut / 100) + 0.015 * markdown_shift
    sim_waste = max(current_waste * (1 - waste_reduction), 0)
    sim_profit = current_profit * (1 + 0.08 * (order_cut / 100) + 0.03 * markdown_shift)

    s1, s2, s3 = st.columns(3)
    s1.metric("Current waste", f"{current_waste:.1%}")
    s2.metric("Simulated waste", f"{sim_waste:.1%}", delta=f"-{(current_waste-sim_waste):.1%}")
    s3.metric("Simulated avg profit", f"€{sim_profit:.2f}", delta=f"€{(sim_profit-current_profit):.2f}")

    st.markdown("### Store inventory balance")
    st.dataframe(store_summary.sort_values(["unmet_demand", "remaining_inventory"], ascending=[False, False]), use_container_width=True, hide_index=True)

    st.markdown("### Transfer suggestions for stores where demand exceeds available inventory")
    st.caption("Routing logic: same-region donors are prioritized first, cross-region donors are used as second-best options, and donors are ranked by transport distance then remaining shelf life.")
    if transfer_df.empty:
        st.success("No filtered store currently shows unmet demand that needs transfer support.")
    else:
        show_cols = [
            "store_id", "region", "category", "remaining_inventory", "demand", "unmet_demand",
            "recommended_transfer_qty", "best_route", "same_region_options", "cross_region_options"
        ]
        st.dataframe(
            transfer_df.sort_values(["unmet_demand", "recommended_transfer_qty"], ascending=[False, False])[show_cols],
            use_container_width=True,
            hide_index=True,
        )


def manager_promotions(df: pd.DataFrame):
    st.subheader("Promotion Designer")
    left, right = st.columns([1, 1.2])
    with left:
        promo_category = st.selectbox("Promotion category", sorted(df["category"].unique()), key="promo_cat")
        expiry_target = st.selectbox("Target expiry bucket", ["<=1d", "2-3d", "4-7d", "8-30d", ">30d"])
        discount = st.slider("Discount %", 0, 50, 18)
        bundle = st.checkbox("Bundle with complementary items", value=True)
        weekend_only = st.checkbox("Weekend campaign only", value=False)

        sub = df[(df["category"] == promo_category) & (df["expiry_bucket"].astype(str) == expiry_target)].copy()
        if weekend_only:
            sub = sub[sub["is_weekend"] == 1]

        demand_lift = 0.08 + discount / 200
        if bundle:
            demand_lift += 0.06

        est_sales_uplift = sub["units_sold"].mean() * demand_lift if len(sub) else 0
        est_waste_drop = sub["waste_pct"].mean() * min(0.35, demand_lift) if len(sub) else 0
        est_profit = sub["profit"].mean() * (1 + demand_lift - discount / 150) if len(sub) else 0

        st.metric("Estimated sales uplift", f"{est_sales_uplift:.2f} units")
        st.metric("Estimated waste reduction", f"{est_waste_drop:.1%}")
        st.metric("Estimated avg profit", f"€{est_profit:.2f}")

    with right:
        promo_base = df.groupby(["expiry_bucket"])[["discount_pct", "waste_pct", "profit"]].mean().reset_index()
        fig = px.bar(promo_base, x="expiry_bucket", y=["discount_pct", "waste_pct"], barmode="group", title="Current Discount vs Waste by Expiry")
        st.plotly_chart(fig, use_container_width=True)

        st.markdown("**Recommended promotion copy**")
        st.info(
            f"Run a {discount}% {promo_category} campaign for {expiry_target} items"
            + (" on weekends" if weekend_only else "")
            + (" with bundle offers" if bundle else " as single-item markdown")
            + ". Position the offer at high-traffic display zones and highlight value + freshness."
        )


def manager_risk(df: pd.DataFrame):
    st.subheader("Risk & Store Operations")
    _, importances = fit_risk_model(df)
    c1, c2 = st.columns([1.1, 1])
    with c1:
        fig = px.bar(importances.head(10).sort_values(), orientation="h", title="Top Drivers of High Waste Risk")
        st.plotly_chart(fig, use_container_width=True)
    with c2:
        heat = df.groupby(["region", "category"])["temp_deviation"].mean().reset_index()
        fig = px.density_heatmap(heat, x="category", y="region", z="temp_deviation", title="Temperature Deviation Heatmap")
        st.plotly_chart(fig, use_container_width=True)

    alerts = (
        df.groupby("store_id")[["temp_deviation", "temp_abuse_events", "waste_pct", "profit"]]
        .mean()
        .assign(alert_score=lambda x: 0.35 * x["temp_deviation"] + 0.25 * x["temp_abuse_events"] + 0.4 * x["waste_pct"] * 10)
        .sort_values("alert_score", ascending=False)
        .head(15)
        .reset_index()
    )
    st.markdown("### Automated store alerts")
    st.dataframe(alerts, use_container_width=True, hide_index=True)


def consumer_deals(df: pd.DataFrame):
    st.subheader("Consumer Mode")
    c1, c2, c3 = st.columns(3)
    max_budget = c1.slider("Budget (€)", 5, 60, 20)
    preferred_category = c2.selectbox("Preferred category", ["All"] + sorted(df["category"].unique()))
    max_expiry = c3.slider("Maximum days until expiry", 1, 14, 5)

    deals = df[df["days_until_expiry"] <= max_expiry].copy()
    if preferred_category != "All":
        deals = deals[deals["category"] == preferred_category]
    deals = deals.assign(
        savings=lambda x: x["base_price"] - x["selling_price"],
        deal_score=lambda x: x["discount_pct"] * 0.5 + x["value_score"] * 0.35 + (x["profit_margin_pct"].clip(lower=0) / 100) * 0.15,
    ).sort_values(["deal_score", "savings"], ascending=False)

    display = deals[[
        "product_name", "category", "store_id", "days_until_expiry",
        "base_price", "selling_price", "discount_pct", "savings"
    ]].head(25)
    st.dataframe(display, use_container_width=True, hide_index=True)

    fig = px.scatter(
        deals.head(500), x="selling_price", y="discount_pct", color="category",
        hover_data=["product_name", "store_id", "days_until_expiry"],
        title="Discounted Items Map"
    )
    st.plotly_chart(fig, use_container_width=True)

    affordable = deals[deals["selling_price"] <= max_budget].head(10)
    if not affordable.empty:
        st.markdown("### Best picks for your budget")
        for _, row in affordable.iterrows():
            st.success(
                f"Now €{row['selling_price']:.2f} (save €{row['base_price'] - row['selling_price']:.2f}) · expires in {int(row['days_until_expiry'])} day(s)"
            )
            st.markdown(
                f"""
                🛒 **{row['product_name']}**  
                📦 Category: {row['category']}  
                🏪 Store: {row['store_id']}  
                💸 Discount: {row['discount_pct']*100:.0f}%  
                ⏳ Expiry: {row['days_until_expiry']} days
                """
            )


def build_bundle(df: pd.DataFrame, budget: float, people: int, theme: str):
    work = df.copy()
    work = work[work["days_until_expiry"] <= 7].copy()
    work["score"] = work["value_score"] + work["discount_pct"]

    theme_map = {
        "Quick dinner": ["Ready_to_Eat", "Produce", "Bakery", "Dairy"],
        "Healthy protein": ["Meat", "Seafood", "Dairy", "Produce"],
        "Family breakfast": ["Bakery", "Dairy", "Beverages", "Produce"],
        "Budget saver": list(work["category"].unique()),
    }
    cats = theme_map.get(theme, list(work["category"].unique()))
    work = work[work["category"].isin(cats)].sort_values(["score", "selling_price"], ascending=[False, True])

    chosen = []
    remaining = budget
    target_items = min(max(people + 1, 3), 6)
    used_categories = set()

    for _, row in work.iterrows():
        if row["selling_price"] <= remaining:
            if theme != "Budget saver" and row["category"] in used_categories:
                continue
            chosen.append(row)
            remaining -= row["selling_price"]
            used_categories.add(row["category"])
            if len(chosen) >= target_items:
                break

    if not chosen:
        return pd.DataFrame(), 0.0, 0.0
    bundle = pd.DataFrame(chosen)
    total = bundle["selling_price"].sum()
    saved = (bundle["base_price"] - bundle["selling_price"]).sum()
    return bundle, total, saved


def consumer_bundles(df: pd.DataFrame):
    st.subheader("Bundle Builder")
    c1, c2, c3 = st.columns(3)
    budget = c1.slider("Bundle budget (€)", 8, 80, 25)
    people = c2.slider("People", 1, 6, 2)
    theme = c3.selectbox("Bundle theme", ["Quick dinner", "Healthy protein", "Family breakfast", "Budget saver"])

    bundle, total, saved = build_bundle(df, budget, people, theme)
    if bundle.empty:
        st.warning("No bundle found for the current filters.")
        return

    k1, k2, k3 = st.columns(3)
    k1.metric("Bundle total", f"€{total:.2f}")
    k2.metric("You save", f"€{saved:.2f}")
    k3.metric("Items", f"{len(bundle)}")

    st.dataframe(bundle[[
        "product_name", "category", "store_id", "selling_price", "base_price", "discount_pct", "days_until_expiry"
    ]], use_container_width=True, hide_index=True)

    st.info(
        "Suggested marketing use: turn these bundles into one-click promotions for end customers or pre-designed campaign packs for store managers."
    )


def consumer_personal(df: pd.DataFrame):
    st.subheader("Personalized Promotions")
    favorite = st.selectbox("Favorite category", sorted(df["category"].unique()))
    price_cap = st.slider("Max item price (€)", 1, 30, 10)
    not_too_close = st.checkbox("Hide items expiring within 1 day", value=False)

    recs = df[df["category"] == favorite].copy()
    recs = recs[recs["selling_price"] <= price_cap]
    if not_too_close:
        recs = recs[recs["days_until_expiry"] > 1]
    recs = recs.assign(score=lambda x: x["discount_pct"] * 0.55 + x["value_score"] * 0.45).sort_values("score", ascending=False).head(12)

    cols = st.columns(3)
    for i, (_, row) in enumerate(recs.iterrows()):
        with cols[i % 3]:
            st.markdown(f"### {row['product_name']}")
            st.write(f"{row['category']} · {row['store_id']}")
            st.write(f"Now **€{row['selling_price']:.2f}** | Save **€{(row['base_price'] - row['selling_price']):.2f}**")
            st.write(f"Expires in {int(row['days_until_expiry'])} day(s)")
            st.button("Add to shortlist", key=f"short_{i}")


def main():
    st.title("🥗 FreshWise")
    st.caption("Perishable retail optimization for managers and consumers")

    try:
        df = load_data()
    except Exception as e:
        st.error(str(e))
        st.stop()

    filtered = apply_filters(df)
    if filtered.empty:
        st.warning("No data left after filtering.")
        st.stop()

    role = st.radio("Choose your mode", ["Manager", "Consumer"], horizontal=True)

    if role == "Manager":
        tabs = st.tabs([
            "Overview",
            "Executive Summary",
            "Category Intelligence",
            "Inventory & Replenishment",
            "Promotion Designer",
            "Diagnose",
        ])
        with tabs[0]:
            manager_dashboard(filtered)
        with tabs[1]:
            manager_summary(filtered)
        with tabs[2]:
            manager_category_intelligence(filtered)
        with tabs[3]:
            manager_inventory(filtered, df)
        with tabs[4]:
            manager_promotions(filtered)
        with tabs[5]:
            manager_diagnose(filtered)
    else:
        tabs = st.tabs([
            "Deal Finder",
            "Bundle Builder",
            "Personalized Promotions",
        ])
        with tabs[0]:
            consumer_deals(filtered)
        with tabs[1]:
            consumer_bundles(filtered)
        with tabs[2]:
            consumer_personal(filtered)

    with st.expander("About this app"):
        st.markdown(
            """
            - **Manager mode** turns data into inventory, markdown, and operational decisions.
            - **Consumer mode** surfaces discounted products, smart bundles, and personalized promotions.
            - Built for deployment on Hugging Face Docker Spaces with Streamlit.
            """
        )


if __name__ == "__main__":
    main()