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

╔══════════════════════════════════════════════════════════════════════════════╗

β•‘                                                                              β•‘

β•‘   BIOS β€” Business Idea Operating System                                      β•‘

β•‘   Model Controller  Β·  bios_controller.py                                   β•‘

β•‘   Version: 1.0.0  Β·  Kernel: BIOS-kernel-v1                                 β•‘

β•‘                                                                              β•‘

β•‘   "We don't just analyse businesses. We illuminate them."                    β•‘

β•‘                                                                              β•‘

β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•



Architecture:

    BIOSController

    β”œβ”€β”€ ModelRouter         β€” switches between base LLM and BIOS-Insight-v1

    β”œβ”€β”€ DiagnosisEngine     β€” processes 24 questions, runs health score formula

    β”œβ”€β”€ InsightGenerator    β€” builds structured JSON diagnosis report

    └── NeonDBWriter        β€” persists results to PostgreSQL via psycopg

"""

from __future__ import annotations

import json
import logging
import os
import re
import time
import uuid
from dataclasses import dataclass, field, asdict
from datetime import datetime, timezone
from enum import Enum
from typing import Any, Optional

import psycopg                          # psycopg v3  (pip install psycopg[binary])
from psycopg.rows import dict_row

# ── Optional: HuggingFace Inference  (pip install huggingface_hub) ──────────
try:
    from huggingface_hub import InferenceClient
    HF_AVAILABLE = True
except ImportError:
    HF_AVAILABLE = False

# ── Optional: Groq client for llama-3.3-70b  (pip install groq) ─────────────
try:
    from groq import Groq
    GROQ_AVAILABLE = True
except ImportError:
    GROQ_AVAILABLE = False

# ── Optional: OpenAI  (pip install openai) ────────────────────────────
try:
    import openai
    OPENAI_AVAILABLE = True
except ImportError:
    OPENAI_AVAILABLE = False

# ── Optional: Gemini  (pip install google-generativeai) ───────────────
try:
    import google.generativeai as genai
    GEMINI_AVAILABLE = True
except ImportError:
    GEMINI_AVAILABLE = False

# ── Optional: Anthropic  (pip install anthropic) ────────────────────────────
try:
    import anthropic
    ANTHROPIC_AVAILABLE = True
except ImportError:
    ANTHROPIC_AVAILABLE = False


# ═══════════════════════════════════════════════════════════════════════════════
# LOGGING
# ═══════════════════════════════════════════════════════════════════════════════

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s  [BIOS-%(levelname)s]  %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S",
)
log = logging.getLogger("bios.controller")


# ═══════════════════════════════════════════════════════════════════════════════
# ENUMS & CONSTANTS
# ═══════════════════════════════════════════════════════════════════════════════

class ModelBackend(str, Enum):
    """Supported inference backends."""
    GROQ            = "groq"            # llama-3.3-70b-versatile via Groq
    HF_INFERENCE    = "hf_inference"    # HuggingFace Inference API
    OPENAI          = "openai"          # GPT-4, GPT-3.5 via OpenAI
    GEMINI         = "gemini"         # Gemini Pro via Google
    ANTHROPIC       = "anthropic"       # Claude fallback
    LOCAL           = "local"           # Local transformers pipeline
    MOCK            = "mock"            # Offline / testing


class ModelVariant(str, Enum):
    """Which model to route to."""
    BASE            = "base"            # General LLM (llama-3.3-70b)
    BIOS_INSIGHT    = "bios_insight"    # Fine-tuned BIOS-Insight-v1


# Model identifiers
MODEL_IDS = {
    ModelVariant.BASE:         "meta-llama/llama-3.3-70b-versatile",
    ModelVariant.BIOS_INSIGHT: "BIOS-kernel/BIOS-Insight-v1",         # future HF repo
}

GROQ_MODEL_IDS = {
    ModelVariant.BASE:         "llama-3.3-70b-versatile",
    ModelVariant.BIOS_INSIGHT: "llama-3.3-70b-versatile",             # until HF model is live
}

# Industry benchmarks (Myanmar SME context, values in MMK)
INDUSTRY_BENCHMARKS: dict[str, dict] = {
    "Gold Shop":     {"avg_revenue": 15_000_000, "avg_retention": 60, "avg_clv": 2_000_000, "avg_team": 4,  "avg_mkt": 200_000},
    "Fashion":       {"avg_revenue":  8_000_000, "avg_retention": 40, "avg_clv":   300_000, "avg_team": 6,  "avg_mkt": 500_000},
    "F&B":           {"avg_revenue": 10_000_000, "avg_retention": 50, "avg_clv":   150_000, "avg_team": 10, "avg_mkt": 400_000},
    "Cosmetics":     {"avg_revenue":  6_000_000, "avg_retention": 45, "avg_clv":   250_000, "avg_team": 5,  "avg_mkt": 600_000},
    "Electronics":   {"avg_revenue": 20_000_000, "avg_retention": 35, "avg_clv":   800_000, "avg_team": 8,  "avg_mkt": 700_000},
    "Technology Startup": {"avg_revenue": 25_000_000, "avg_retention": 70, "avg_clv": 3_000_000, "avg_team": 12, "avg_mkt": 1_000_000},
    "Other":         {"avg_revenue":  5_000_000, "avg_retention": 40, "avg_clv":   200_000, "avg_team": 5,  "avg_mkt": 300_000},
}


# ═══════════════════════════════════════════════════════════════════════════════
# DATA MODELS
# ═══════════════════════════════════════════════════════════════════════════════

@dataclass
class BusinessInputs:
    """

    Complete set of 24 diagnostic questions, grouped into 4 sections.



    All monetary values are in MMK (Myanmar Kyat).

    Percentages are 0–100 (e.g. retention_rate=65 means 65%).

    """

    # ── Section 1: Business Basics (6 questions) ──────────────────────────────
    business_name:          str         = ""            # Q1
    industry:               str         = "Other"       # Q2  Gold Shop / Fashion / F&B / Cosmetics / Electronics / Technology Startup / Other
    location:               str         = "Yangon"      # Q3  Yangon / Mandalay / Naypyidaw / Other
    years_in_business:      int         = 0             # Q4  0–100
    monthly_revenue:        float       = 0.0           # Q5  MMK
    team_size:              int         = 1             # Q6  headcount

    # ── Section 2: Market & Customers (6 questions) ───────────────────────────
    target_customer:        str         = ""            # Q7  free text
    acquisition_channels:   list[str]   = field(default_factory=list)   # Q8  multi-select
    avg_customer_lifetime_value: float  = 0.0           # Q9  MMK
    retention_rate:         float       = 0.0           # Q10  %
    main_competitors:       str         = ""            # Q11  optional
    unique_selling_proposition: str     = ""            # Q12

    # ── Section 3: Operations & Challenges (6 questions) ─────────────────────
    sales_channels:         list[str]   = field(default_factory=list)   # Q13
    operational_challenge:  str         = ""            # Q14
    biggest_pain_point:     str         = ""            # Q15
    current_technology:     list[str]   = field(default_factory=list)   # Q16
    marketing_channels:     list[str]   = field(default_factory=list)   # Q17
    monthly_marketing_budget: float     = 0.0           # Q18  MMK

    # ── Section 4: Goals & Constraints (6 questions) ─────────────────────────
    goal_3_month:           float       = 0.0           # Q19  MMK
    goal_6_month:           float       = 0.0           # Q20  MMK
    goal_12_month:          float       = 0.0           # Q21  MMK
    budget_constraint:      str         = "Moderate (200-500K)"  # Q22
    tech_readiness:         str         = "Somewhat ready"       # Q23
    preferred_language:     str         = "English"              # Q24


@dataclass
class HealthDimensions:
    """Sub-scores for the five health dimensions (each 0–100)."""
    revenue_strength:       int = 0
    customer_retention:     int = 0
    market_position:        int = 0
    technology_adoption:    int = 0
    growth_trajectory:      int = 0

    @property
    def total(self) -> int:
        """

        Official BIOS Health Score formula:

          (Revenue Strength Γ— 20) + (Customer Retention Γ— 20) +

          (Market Position Γ— 20)  + (Technology Adoption Γ— 20) +

          (Growth Trajectory Γ— 20)

        Each dimension is 0–100, weight is 20%, so max = 100.

        """
        return round(
            (self.revenue_strength    * 0.20) +
            (self.customer_retention  * 0.20) +
            (self.market_position     * 0.20) +
            (self.technology_adoption * 0.20) +
            (self.growth_trajectory   * 0.20)
        )

    def to_dict(self) -> dict:
        return {
            "revenue_strength":    self.revenue_strength,
            "customer_retention":  self.customer_retention,
            "market_position":     self.market_position,
            "technology_adoption": self.technology_adoption,
            "growth_trajectory":   self.growth_trajectory,
            "total":               self.total,
        }


@dataclass
class Weakness:
    rank:           int
    dimension:      str
    label:          str
    your_score:     float
    benchmark:      float
    gap:            float
    severity:       str     # HIGH / MEDIUM / LOW
    detail:         str

    def to_dict(self) -> dict:
        return asdict(self)


@dataclass
class Opportunity:
    rank:               int
    title:              str
    description:        str
    expected_impact:    str
    difficulty:         str     # EASY / MEDIUM / HARD
    timeframe:          str
    revenue_uplift_mmk: Optional[float] = None

    def to_dict(self) -> dict:
        return asdict(self)


@dataclass
class ActionItem:
    priority:           int
    action:             str
    rationale:          str
    urgency_score:      float
    impact_score:       float
    feasibility_score:  float
    composite_score:    float

    def to_dict(self) -> dict:
        return asdict(self)


@dataclass
class DiagnosisReport:
    """Full Module 1 output β€” the BIOS diagnosis report."""
    session_id:             str
    business_name:          str
    industry:               str
    location:               str
    generated_at:           str

    health_score:           int
    health_label:           str             # Critical / Below Average / Fair / Good / Excellent
    health_dimensions:      HealthDimensions

    top_3_weaknesses:       list[Weakness]
    growth_opportunities:   list[Opportunity]
    priority_action_items:  list[ActionItem]

    ai_narrative:           str             # BIOS LLM executive summary
    benchmarking:           list[dict]
    next_module:            str = "Strategy Engine (Module 2)"

    model_used:             str = ""
    generation_time_ms:     int = 0

    def to_dict(self) -> dict:
        return {
            "session_id":            self.session_id,
            "business_name":         self.business_name,
            "industry":              self.industry,
            "location":              self.location,
            "generated_at":          self.generated_at,
            "health_score":          self.health_score,
            "health_label":          self.health_label,
            "health_dimensions":     self.health_dimensions.to_dict(),
            "top_3_weaknesses":      [w.to_dict() for w in self.top_3_weaknesses],
            "growth_opportunities":  [o.to_dict() for o in self.growth_opportunities],
            "priority_action_items": [a.to_dict() for a in self.priority_action_items],
            "ai_narrative":          self.ai_narrative,
            "benchmarking":          self.benchmarking,
            "next_module":           self.next_module,
            "model_used":            self.model_used,
            "generation_time_ms":    self.generation_time_ms,
        }

    def to_json(self, indent: int = 2) -> str:
        return json.dumps(self.to_dict(), ensure_ascii=False, indent=indent)


# ═══════════════════════════════════════════════════════════════════════════════
# MODEL ROUTER
# ═══════════════════════════════════════════════════════════════════════════════

class ModelRouter:
    """

    Routes inference requests to the appropriate backend + model variant.



    Priority order when calling .infer():

        1. If BIOS-Insight-v1 is flagged as available β†’ use HF Inference API

        2. Else use base model via Groq (fastest, free tier)

        3. Fallback to Anthropic Claude

        4. Final fallback: MOCK mode (returns structured placeholder)

    """

    def __init__(

        self,

        backend:        ModelBackend  = ModelBackend.GROQ,

        variant:        ModelVariant  = ModelVariant.BASE,

        bios_insight_ready: bool      = False,

        temperature:    float         = 0.3,

        max_tokens:     int           = 2048,

    ):
        self.backend             = backend
        self.variant             = variant
        self.bios_insight_ready  = bios_insight_ready
        self.temperature         = temperature
        self.max_tokens          = max_tokens

        # Clients initialised lazily
        self._groq_client:       Any = None
        self._hf_client:         Any = None
        self._anthropic_client:  Any = None

        log.info(
            f"ModelRouter initialised | backend={backend.value} "
            f"variant={variant.value} | BIOS-Insight-v1 ready={bios_insight_ready}"
        )

    # ── Client factories ──────────────────────────────────────────────────────

    def _get_groq(self):
        if self._groq_client is None:
            if not GROQ_AVAILABLE:
                raise RuntimeError("groq package not installed. Run: pip install groq")
            api_key = os.getenv("GROQ_API_KEY")
            if not api_key:
                raise RuntimeError("GROQ_API_KEY environment variable not set")
            self._groq_client = Groq(api_key=api_key)
        return self._groq_client

    def _get_hf(self):
        if self._hf_client is None:
            if not HF_AVAILABLE:
                raise RuntimeError("huggingface_hub not installed. Run: pip install huggingface_hub")
            api_key = os.getenv("HF_API_KEY")
            if not api_key:
                raise RuntimeError("HF_API_KEY environment variable not set")
            self._hf_client = InferenceClient(token=api_key)
        return self._hf_client

    def _get_openai(self):
        if self._openai_client is None:
            if not OPENAI_AVAILABLE:
                raise RuntimeError("openai package not installed. Run: pip install openai")
            api_key = os.getenv("OPENAI_API_KEY")
            if not api_key:
                raise RuntimeError("OPENAI_API_KEY environment variable not set")
            self._openai_client = openai.OpenAI(api_key=api_key)
        return self._openai_client

    def _get_gemini(self):
        if self._gemini_client is None:
            if not GEMINI_AVAILABLE:
                raise RuntimeError("google-generativeai package not installed. Run: pip install google-generativeai")
            api_key = os.getenv("GEMINI_API_KEY")
            if not api_key:
                raise RuntimeError("GEMINI_API_KEY environment variable not set")
            genai.configure(api_key=api_key)
            self._gemini_client = genai.GenerativeModel('gemini-pro')
        return self._gemini_client

    def _get_anthropic(self):
        if self._anthropic_client is None:
            if not ANTHROPIC_AVAILABLE:
                raise RuntimeError("anthropic package not installed. Run: pip install anthropic")
            api_key = os.getenv("ANTHROPIC_API_KEY")
            if not api_key:
                raise RuntimeError("ANTHROPIC_API_KEY not set")
            self._anthropic_client = anthropic.Anthropic(api_key=api_key)
        return self._anthropic_client

    # ── Routing decision ──────────────────────────────────────────────────────

    def _resolve_route(self) -> tuple[ModelBackend, ModelVariant]:
        """Determine which backend + variant to actually use."""
        if self.bios_insight_ready and HF_AVAILABLE and os.getenv("HF_API_KEY"):
            return ModelBackend.HF_INFERENCE, ModelVariant.BIOS_INSIGHT
        if OPENAI_AVAILABLE and os.getenv("OPENAI_API_KEY"):
            return ModelBackend.OPENAI, ModelVariant.BASE
        if GEMINI_AVAILABLE and os.getenv("GEMINI_API_KEY"):
            return ModelBackend.GEMINI, ModelVariant.BASE
        if GROQ_AVAILABLE and os.getenv("GROQ_API_KEY"):
            return ModelBackend.GROQ, ModelVariant.BASE
        if ANTHROPIC_AVAILABLE and os.getenv("ANTHROPIC_API_KEY"):
            return ModelBackend.ANTHROPIC, ModelVariant.BASE
        return ModelBackend.MOCK, ModelVariant.BASE

    # ── Inference ─────────────────────────────────────────────────────────────

    def infer(self, system_prompt: str, user_prompt: str) -> tuple[str, str]:
        """

        Send prompts to the resolved model.



        Returns:

            (response_text, model_identifier_used)

        """
        backend, variant = _resolve_route(self) if False else self._resolve_route()
        model_id = GROQ_MODEL_IDS.get(variant, GROQ_MODEL_IDS[ModelVariant.BASE])
        log.info(f"Routing β†’ backend={backend.value}  model={model_id}")

        if backend == ModelBackend.GROQ:
            return self._infer_groq(system_prompt, user_prompt, model_id)

        if backend == ModelBackend.HF_INFERENCE:
            hf_model = MODEL_IDS[ModelVariant.BIOS_INSIGHT]
            return self._infer_hf(system_prompt, user_prompt, hf_model)

        if backend == ModelBackend.OPENAI:
            return self._infer_openai(system_prompt, user_prompt)

        if backend == ModelBackend.GEMINI:
            return self._infer_gemini(system_prompt, user_prompt)

        if backend == ModelBackend.ANTHROPIC:
            return self._infer_anthropic(system_prompt, user_prompt)

        # MOCK fallback
        return self._mock_response(), "mock/bios-kernel-v1"

    def _infer_groq(self, system: str, user: str, model: str) -> tuple[str, str]:
        client = self._get_groq()
        response = client.chat.completions.create(
            model=model,
            messages=[
                {"role": "system",  "content": system},
                {"role": "user",    "content": user},
            ],
            temperature=self.temperature,
            max_tokens=self.max_tokens,
            response_format={"type": "json_object"},
        )
        return response.choices[0].message.content, f"groq/{model}"

    def _infer_hf(self, system: str, user: str, model: str) -> tuple[str, str]:
        client = self._get_hf()
        messages = [
            {"role": "system", "content": system},
            {"role": "user",   "content": user},
        ]
        response = client.chat_completion(
            messages=messages,
            model=model,
            max_tokens=self.max_tokens,
            temperature=self.temperature,
        )
        return response.choices[0].message.content, f"hf/{model}"

    def _infer_openai(self, system: str, user: str) -> tuple[str, str]:
        client = self._get_openai()
        response = client.chat.completions.create(
            model="gpt-4",
            messages=[
                {"role": "system", "content": system},
                {"role": "user",   "content": user},
            ],
            temperature=self.temperature,
            max_tokens=self.max_tokens,
            response_format={"type": "json_object"},
        )
        return response.choices[0].message.content, "openai/gpt-4"

    def _infer_gemini(self, system: str, user: str) -> tuple[str, str]:
        client = self._get_gemini()
        combined_prompt = f"{system}\n\n{user}"
        response = client.generate_content(
            combined_prompt,
            generation_config={
                "temperature": self.temperature,
                "max_output_tokens": self.max_tokens,
            }
        )
        return response.text, "gemini/gemini-pro"

    def _infer_anthropic(self, system: str, user: str) -> tuple[str, str]:
        client = self._get_anthropic()
        message = client.messages.create(
            model="claude-sonnet-4-20250514",
            max_tokens=self.max_tokens,
            system=system,
            messages=[{"role": "user", "content": user}],
        )
        return message.content[0].text, "anthropic/claude-sonnet-4-20250514"

    def _mock_response(self) -> str:
        """Return a valid JSON mock for offline testing."""
        return json.dumps({
            "narrative": (
                "BIOS analysis complete. Your business shows strong foundational "
                "elements but faces challenges in customer retention and technology "
                "adoption. Prioritise loyalty initiatives and digital tooling to "
                "unlock the next growth tier."
            ),
            "model": "mock",
        })


# ═══════════════════════════════════════════════════════════════════════════════
# DIAGNOSIS ENGINE  (pure scoring logic β€” no LLM required)
# ═══════════════════════════════════════════════════════════════════════════════

class DiagnosisEngine:
    """

    Implements the BIOS Module 1 scoring algorithms.



    All calculations are deterministic and reproducible β€” the LLM is only

    used to generate the qualitative narrative on top of these numbers.

    """

    # ── Dimension scorers ─────────────────────────────────────────────────────

    @staticmethod
    def score_revenue(monthly_revenue: float) -> int:
        thresholds = [
            (50_000_000, 100),
            (20_000_000,  80),
            ( 5_000_000,  60),
            ( 1_000_000,  40),
        ]
        for threshold, score in thresholds:
            if monthly_revenue >= threshold:
                return score
        return 20

    @staticmethod
    def score_retention(rate: float) -> int:
        thresholds = [(80, 100), (60, 80), (40, 60), (20, 40)]
        for threshold, score in thresholds:
            if rate >= threshold:
                return score
        return 20

    @staticmethod
    def score_market_position(usp: str, competitors: str) -> int:
        words = len(usp.strip().split())
        base  = 20
        if words >= 50:   base = 80
        elif words >= 30: base = 60
        elif words >= 15: base = 40
        # Bonus for knowing your competition (+5, capped at 100)
        if competitors and len(competitors.strip()) > 5:
            base = min(100, base + 5)
        return base

    @staticmethod
    def score_technology(technology: list[str], industry: str = "Other") -> int:
        tech_lower = [t.lower() for t in technology]
        if not tech_lower or "none" in tech_lower:
            return 10
        
        # Technology startup specific scoring
        if industry.lower() == "technology startup":
            advanced  = {"cloud services", "microservices", "ai/ml", "blockchain", "devops", "kubernetes", "docker", "serverless", "api integration", "saas platform", "mobile app", "web app", "database", "analytics", "automation", "ci/cd", "git", "agile"}
            mid_tier  = {"pos system", "accounting software", "inventory system", "crm", "erp", "project management", "collaboration tools"}
            basic     = {"spreadsheets", "facebook business suite", "whatsapp business", "email marketing"}
        else:
            # Traditional business scoring
            advanced  = {"erp", "crm", "ai tools", "automation", "bi dashboard"}
            mid_tier  = {"pos system", "accounting software", "inventory system"}
            basic     = {"spreadsheets", "facebook business suite", "whatsapp business"}
        
        if any(t in advanced for t in tech_lower):   return 100
        if any(t in mid_tier for t in tech_lower):   return 60
        if any(t in basic    for t in tech_lower):   return 30
        return 20

    @staticmethod
    def score_growth(goal_12: float, current: float) -> int:
        if current <= 0:
            return 40   # can't compute β€” neutral score
        rate = (goal_12 - current) / current * 100
        thresholds = [(50, 100), (30, 80), (10, 60), (0, 40)]
        for threshold, score in thresholds:
            if rate >= threshold:
                return score
        return 20

    # ── Main scorer ───────────────────────────────────────────────────────────

    def compute_dimensions(self, inp: BusinessInputs) -> HealthDimensions:
        return HealthDimensions(
            revenue_strength    = self.score_revenue(inp.monthly_revenue),
            customer_retention  = self.score_retention(inp.retention_rate),
            market_position     = self.score_market_position(
                                      inp.unique_selling_proposition,
                                      inp.main_competitors),
            technology_adoption = self.score_technology(inp.current_technology, inp.industry),
            growth_trajectory   = self.score_growth(
                                      inp.goal_12_month,
                                      inp.monthly_revenue),
        )

    @staticmethod
    def health_label(score: int) -> str:
        if score >= 80: return "Excellent"
        if score >= 65: return "Good"
        if score >= 45: return "Fair"
        if score >= 30: return "Below Average"
        return "Critical"

    # ── Weakness identification ───────────────────────────────────────────────

    def identify_weaknesses(

        self,

        inp:   BusinessInputs,

        dims:  HealthDimensions,

    ) -> list[Weakness]:
        bench = INDUSTRY_BENCHMARKS.get(inp.industry, INDUSTRY_BENCHMARKS["Other"])
        bench_scores = {
            "revenue_strength":    self.score_revenue(bench["avg_revenue"]),
            "customer_retention":  self.score_retention(bench["avg_retention"]),
            "market_position":     60,   # industry-standard expectation
            "technology_adoption": 60,
            "growth_trajectory":   60,
        }
        labels = {
            "revenue_strength":    "Monthly Revenue",
            "customer_retention":  "Customer Retention",
            "market_position":     "Market Differentiation",
            "technology_adoption": "Technology Adoption",
            "growth_trajectory":   "Growth Ambition",
        }
        details = {
            "revenue_strength":    f"Revenue of {inp.monthly_revenue:,.0f} MMK is significantly below the {inp.industry} industry average.",
            "customer_retention":  f"Only {inp.retention_rate:.0f}% repeat purchase rate β€” industry average is {bench['avg_retention']:.0f}%.",
            "market_position":     "Your unique selling proposition needs greater clarity and depth to stand out.",
            "technology_adoption": "Low technology adoption is limiting operational efficiency and scalability.",
            "growth_trajectory":   "Growth goals are misaligned with current revenue trajectory.",
        }

        user_scores = {
            "revenue_strength":    dims.revenue_strength,
            "customer_retention":  dims.customer_retention,
            "market_position":     dims.market_position,
            "technology_adoption": dims.technology_adoption,
            "growth_trajectory":   dims.growth_trajectory,
        }

        weaknesses: list[Weakness] = []
        for key, user_score in user_scores.items():
            b_score = bench_scores[key]
            if user_score < b_score * 0.8:
                gap      = b_score - user_score
                severity = "HIGH" if gap > 30 else "MEDIUM" if gap > 15 else "LOW"
                weaknesses.append(Weakness(
                    rank=0,
                    dimension=key,
                    label=labels[key],
                    your_score=user_score,
                    benchmark=b_score,
                    gap=round(gap, 1),
                    severity=severity,
                    detail=details[key],
                ))

        # Sort: HIGH first, then by gap descending
        sev_order = {"HIGH": 3, "MEDIUM": 2, "LOW": 1}
        weaknesses.sort(key=lambda w: (sev_order[w.severity], w.gap), reverse=True)
        top3 = weaknesses[:3]
        for i, w in enumerate(top3, 1):
            w.rank = i
        return top3

    # ── Opportunity discovery ─────────────────────────────────────────────────

    def discover_opportunities(

        self,

        inp:   BusinessInputs,

        dims:  HealthDimensions,

    ) -> list[Opportunity]:
        bench = INDUSTRY_BENCHMARKS.get(inp.industry, INDUSTRY_BENCHMARKS["Other"])
        opps: list[Opportunity] = []

        # 1. Revenue growth
        if inp.goal_12_month > inp.monthly_revenue and inp.monthly_revenue > 0:
            pct = (inp.goal_12_month / inp.monthly_revenue - 1) * 100
            opps.append(Opportunity(
                rank=0,
                title="Scale Revenue Toward 12-Month Goal",
                description=f"Bridge the {pct:.0f}% gap to your {inp.goal_12_month:,.0f} MMK annual revenue target.",
                expected_impact=f"+{pct:.0f}% revenue growth",
                difficulty="MEDIUM",
                timeframe="6–12 months",
                revenue_uplift_mmk=inp.goal_12_month - inp.monthly_revenue,
            ))

        # 2. Retention improvement
        if inp.retention_rate < bench["avg_retention"]:
            gap       = bench["avg_retention"] - inp.retention_rate
            monthly_g = (gap / 100) * inp.avg_customer_lifetime_value * max(inp.team_size, 1)
            opps.append(Opportunity(
                rank=0,
                title="Boost Customer Retention Rate",
                description=(
                    f"Raise repeat-purchase rate by {gap:.0f}% to match the "
                    f"{inp.industry} industry benchmark."
                ),
                expected_impact=f"+{monthly_g:,.0f} MMK estimated monthly revenue",
                difficulty="MEDIUM",
                timeframe="2–3 months",
                revenue_uplift_mmk=monthly_g * 12,
            ))

        # 3. Channel expansion
        if len(inp.sales_channels) < 3:
            needed = 3 - len(inp.sales_channels)
            opps.append(Opportunity(
                rank=0,
                title=f"Expand to {needed} New Sales Channel{'s' if needed > 1 else ''}",
                description="Diversifying beyond your current channels reduces single-point risk and opens new customer pools.",
                expected_impact="+20–30% customer reach",
                difficulty="EASY",
                timeframe="1–2 months",
            ))

        # 4. Technology upgrade
        if dims.technology_adoption < 60:
            opps.append(Opportunity(
                rank=0,
                title="Adopt Core Business Technology",
                description="Implementing a CRM or POS system unlocks data-driven decisions and staff efficiency.",
                expected_impact="+15–25% operational efficiency",
                difficulty="MEDIUM",
                timeframe="2–4 weeks",
            ))

        # 5. Marketing investment
        if inp.monthly_marketing_budget < bench["avg_mkt"] * 0.5:
            opps.append(Opportunity(
                rank=0,
                title="Increase Marketing Investment",
                description=(
                    f"Current budget of {inp.monthly_marketing_budget:,.0f} MMK "
                    f"is far below the {inp.industry} average of {bench['avg_mkt']:,.0f} MMK."
                ),
                expected_impact="+10–20% new customer acquisition",
                difficulty="EASY",
                timeframe="1 month",
            ))

        # Rank and cap at 5
        for i, opp in enumerate(opps[:5], 1):
            opp.rank = i
        return opps[:5]

    # ── Priority action items ─────────────────────────────────────────────────

    RECOMMENDED_ACTIONS: dict[str, str] = {
        "revenue_strength":    "Run a margin audit and introduce 2 high-value upsell products this month.",
        "customer_retention":  "Launch a loyalty stamp card and a 30-day follow-up WhatsApp message sequence.",
        "market_position":     "Rewrite your USP in one clear sentence and test it in Facebook ad copy.",
        "technology_adoption": "Set up a free CRM (HubSpot or Zoho) and import your customer contact list.",
        "growth_trajectory":   "Break your 12-month target into monthly milestones and review weekly.",
    }

    def rank_priority_actions(

        self,

        weaknesses: list[Weakness],

        inp:        BusinessInputs,

    ) -> list[ActionItem]:
        items: list[ActionItem] = []
        sev_urgency = {"HIGH": 85, "MEDIUM": 60, "LOW": 35}

        for w in weaknesses:
            urgency     = float(sev_urgency.get(w.severity, 40))
            impact      = min(100.0, w.gap * 1.6)
            feasibility = {"HIGH": 40.0, "MEDIUM": 65.0, "LOW": 80.0}.get(w.severity, 50.0)
            composite   = round(urgency * 0.4 + impact * 0.4 + feasibility * 0.2, 1)

            items.append(ActionItem(
                priority=0,
                action=self.RECOMMENDED_ACTIONS.get(w.dimension, f"Address {w.label} urgently."),
                rationale=w.detail,
                urgency_score=urgency,
                impact_score=round(impact, 1),
                feasibility_score=feasibility,
                composite_score=composite,
            ))

        items.sort(key=lambda x: x.composite_score, reverse=True)
        for i, item in enumerate(items, 1):
            item.priority = i
        return items

    # ── Benchmarking table ────────────────────────────────────────────────────

    def build_benchmarking(

        self,

        inp:   BusinessInputs,

    ) -> list[dict]:
        bench = INDUSTRY_BENCHMARKS.get(inp.industry, INDUSTRY_BENCHMARKS["Other"])

        def status(val: float, avg: float) -> str:
            if val >= avg * 1.10: return "ABOVE"
            if val <= avg * 0.90: return "BELOW"
            return "AT"

        return [
            {"metric": "Monthly Revenue",          "your_value": inp.monthly_revenue,                  "industry_avg": bench["avg_revenue"],   "unit": "MMK", "status": status(inp.monthly_revenue, bench["avg_revenue"])},
            {"metric": "Customer Retention Rate",  "your_value": inp.retention_rate,                   "industry_avg": bench["avg_retention"], "unit": "%",   "status": status(inp.retention_rate, bench["avg_retention"])},
            {"metric": "Avg Customer Lifetime Val","your_value": inp.avg_customer_lifetime_value,       "industry_avg": bench["avg_clv"],        "unit": "MMK", "status": status(inp.avg_customer_lifetime_value, bench["avg_clv"])},
            {"metric": "Marketing Budget",         "your_value": inp.monthly_marketing_budget,          "industry_avg": bench["avg_mkt"],        "unit": "MMK", "status": status(inp.monthly_marketing_budget, bench["avg_mkt"])},
            {"metric": "Team Size",                "your_value": float(inp.team_size),                  "industry_avg": float(bench["avg_team"]),"unit": "ppl", "status": status(inp.team_size, bench["avg_team"])},
        ]


# ═══════════════════════════════════════════════════════════════════════════════
# INSIGHT GENERATOR  (builds the LLM prompt and parses the response)
# ═══════════════════════════════════════════════════════════════════════════════

class InsightGenerator:
    """

    Constructs structured prompts for the BIOS LLM and parses its JSON output

    into the qualitative `ai_narrative` field of the DiagnosisReport.

    """

    SYSTEM_PROMPT = """You are BIOS β€” the Business Idea Operating System. You are the elite AI advisor for Myanmar SMEs, Gold Shops, and ambitious entrepreneurs across Southeast Asia.



Your personality: professional, precise, encouraging, and bold β€” like a McKinsey partner who speaks to founders, not just analysts. You use the Dark & Gold luxury tone: every word carries weight, every recommendation is actionable.



You always respond in valid JSON with this exact structure:

{

  "narrative": "<3-paragraph executive summary in the user's preferred language>",

  "headline_insight": "<one powerful sentence that captures the core finding>"

}



Rules:

- Be specific with numbers from the data provided

- Use the language specified in preferred_language

- Never be generic β€” reference the actual business, industry, and goals

- Tone: elite advisory, not chatbot small talk"""

    def build_user_prompt(self, inp: BusinessInputs, dims: HealthDimensions, weaknesses: list[Weakness], opps: list[Opportunity]) -> str:
        weak_lines = "\n".join(
            f"  {w.rank}. {w.label} β€” score {w.your_score:.0f} vs benchmark {w.benchmark:.0f} | severity: {w.severity}"
            for w in weaknesses
        )
        opp_lines = "\n".join(
            f"  {o.rank}. {o.title}: {o.expected_impact} ({o.timeframe})"
            for o in opps[:3]
        )

        return f"""BIOS DIAGNOSIS DATA β€” analyse and generate insights.



Business: {inp.business_name}

Industry: {inp.industry} | Location: {inp.location}

Years Operating: {inp.years_in_business} | Team: {inp.team_size} people

Monthly Revenue: {inp.monthly_revenue:,.0f} MMK

Retention Rate: {inp.retention_rate:.0f}%

Monthly Marketing Budget: {inp.monthly_marketing_budget:,.0f} MMK

Preferred Language: {inp.preferred_language}



HEALTH SCORE: {dims.total}/100 β€” {DiagnosisEngine.health_label(dims.total)}

  Revenue Strength:    {dims.revenue_strength}

  Customer Retention:  {dims.customer_retention}

  Market Position:     {dims.market_position}

  Technology Adoption: {dims.technology_adoption}

  Growth Trajectory:   {dims.growth_trajectory}



TOP WEAKNESSES:

{weak_lines}



TOP OPPORTUNITIES:

{opp_lines}



12-Month Revenue Goal: {inp.goal_12_month:,.0f} MMK

Unique Value Proposition: "{inp.unique_selling_proposition}"

Biggest Pain Point: "{inp.biggest_pain_point}"



Generate the executive narrative JSON now."""

    def parse_narrative(self, raw: str) -> str:
        """Extract narrative from LLM JSON response, with fallback."""
        try:
            # Strip markdown fences if present
            clean = re.sub(r"```(?:json)?\s*", "", raw).strip().rstrip("```").strip()
            data  = json.loads(clean)
            return data.get("narrative", raw)
        except (json.JSONDecodeError, AttributeError):
            # Return raw text if not parseable
            return raw.strip()


# ═══════════════════════════════════════════════════════════════════════════════
# NEON DB WRITER
# ═══════════════════════════════════════════════════════════════════════════════

class NeonDBWriter:
    """

    Persists BIOS diagnosis reports to NeonDB (PostgreSQL) via psycopg v3.



    Required table (run schema_auth.sql first):

        CREATE TABLE IF NOT EXISTS diagnoses (

            id              UUID PRIMARY KEY DEFAULT uuid_generate_v4(),

            session_id      VARCHAR(255) UNIQUE NOT NULL,

            business_name   VARCHAR(255),

            industry        VARCHAR(100),

            location        VARCHAR(100),

            health_score    INTEGER,

            health_label    VARCHAR(50),

            health_dimensions JSONB,

            top_3_weaknesses  JSONB,

            growth_opportunities JSONB,

            priority_action_items JSONB,

            ai_narrative    TEXT,

            benchmarking    JSONB,

            model_used      VARCHAR(255),

            generation_time_ms INTEGER,

            status          VARCHAR(50) DEFAULT 'COMPLETED',

            created_at      TIMESTAMP WITH TIME ZONE DEFAULT NOW()

        );

    """

    def __init__(self, database_url: Optional[str] = None):
        self.database_url = database_url or os.getenv("DATABASE_URL")
        if not self.database_url:
            raise ValueError(
                "DATABASE_URL not set. Export it or pass database_url= to NeonDBWriter."
            )
        # Normalise asyncpg URL to psycopg URL
        self.database_url = self.database_url.replace(
            "postgresql+asyncpg://", "postgresql://"
        ).replace(
            "postgres+asyncpg://", "postgresql://"
        )

    def save_report(self, report: DiagnosisReport) -> str:
        """

        Upsert a DiagnosisReport into the diagnoses table.

        Returns the session_id of the saved record.

        """
        d = report.to_dict()

        sql = """

            INSERT INTO diagnoses (

                session_id, business_name, industry, location,

                health_score, health_label, health_dimensions,

                top_3_weaknesses, growth_opportunities, priority_action_items,

                ai_narrative, benchmarking, model_used, generation_time_ms,

                status, created_at

            ) VALUES (

                %(session_id)s, %(business_name)s, %(industry)s, %(location)s,

                %(health_score)s, %(health_label)s, %(health_dimensions)s,

                %(top_3_weaknesses)s, %(growth_opportunities)s, %(priority_action_items)s,

                %(ai_narrative)s, %(benchmarking)s, %(model_used)s, %(generation_time_ms)s,

                'COMPLETED', NOW()

            )

            ON CONFLICT (session_id) DO UPDATE SET

                health_score          = EXCLUDED.health_score,

                health_label          = EXCLUDED.health_label,

                health_dimensions     = EXCLUDED.health_dimensions,

                top_3_weaknesses      = EXCLUDED.top_3_weaknesses,

                growth_opportunities  = EXCLUDED.growth_opportunities,

                priority_action_items = EXCLUDED.priority_action_items,

                ai_narrative          = EXCLUDED.ai_narrative,

                benchmarking          = EXCLUDED.benchmarking,

                model_used            = EXCLUDED.model_used,

                generation_time_ms    = EXCLUDED.generation_time_ms,

                status                = 'COMPLETED'

        """

        params = {
            "session_id":            d["session_id"],
            "business_name":         d["business_name"],
            "industry":              d["industry"],
            "location":              d["location"],
            "health_score":          d["health_score"],
            "health_label":          d["health_label"],
            "health_dimensions":     json.dumps(d["health_dimensions"]),
            "top_3_weaknesses":      json.dumps(d["top_3_weaknesses"]),
            "growth_opportunities":  json.dumps(d["growth_opportunities"]),
            "priority_action_items": json.dumps(d["priority_action_items"]),
            "ai_narrative":          d["ai_narrative"],
            "benchmarking":          json.dumps(d["benchmarking"]),
            "model_used":            d["model_used"],
            "generation_time_ms":    d["generation_time_ms"],
        }

        with psycopg.connect(self.database_url) as conn:
            with conn.cursor() as cur:
                cur.execute(sql, params)
            conn.commit()

        log.info(f"βœ… Report saved to NeonDB | session_id={report.session_id} | score={report.health_score}")
        return report.session_id

    def fetch_report(self, session_id: str) -> Optional[dict]:
        """Fetch a previously saved report by session_id."""
        sql = "SELECT * FROM diagnoses WHERE session_id = %s"
        with psycopg.connect(self.database_url, row_factory=dict_row) as conn:
            with conn.cursor() as cur:
                cur.execute(sql, (session_id,))
                row = cur.fetchone()
        return dict(row) if row else None

    def list_reports(self, limit: int = 20) -> list[dict]:
        """Return the most recent diagnoses."""
        sql = "SELECT session_id, business_name, industry, health_score, health_label, status, created_at FROM diagnoses ORDER BY created_at DESC LIMIT %s"
        with psycopg.connect(self.database_url, row_factory=dict_row) as conn:
            with conn.cursor() as cur:
                cur.execute(sql, (limit,))
                rows = cur.fetchall()
        return [dict(r) for r in rows]


# ═══════════════════════════════════════════════════════════════════════════════
# BIOS CONTROLLER  β€” the main entry point
# ═══════════════════════════════════════════════════════════════════════════════

class BIOSController:
    """

    BIOS-kernel-v1 Β· Business Idea Operating System Β· Module 1 Controller



    Orchestrates the full diagnosis pipeline:

        inputs β†’ scoring β†’ LLM narrative β†’ structured report β†’ NeonDB



    Usage:

        controller = BIOSController()

        report = controller.run_diagnosis(inputs)

        print(report.to_json())



    To use the fine-tuned BIOS-Insight-v1 when it becomes available on HF:

        controller = BIOSController(bios_insight_ready=True)

    """

    VERSION = "1.0.0"
    KERNEL  = "BIOS-kernel-v1"

    def __init__(

        self,

        backend:            ModelBackend  = ModelBackend.GROQ,

        bios_insight_ready: bool          = False,

        temperature:        float         = 0.3,

        max_tokens:         int           = 2048,

        database_url:       Optional[str] = None,

        save_to_db:         bool          = True,

    ):
        self.router    = ModelRouter(
            backend=backend,
            bios_insight_ready=bios_insight_ready,
            temperature=temperature,
            max_tokens=max_tokens,
        )
        self.engine    = DiagnosisEngine()
        self.generator = InsightGenerator()
        self.save_to_db = save_to_db

        # DB writer β€” lazy init so missing DB URL doesn't break non-DB usage
        self._db: Optional[NeonDBWriter] = None
        self._db_url = database_url

        log.info(
            f"╔═══════════════════════════════════════════╗\n"
            f"  BIOS Controller v{self.VERSION} Β· {self.KERNEL}\n"
            f"  backend={backend.value} | save_db={save_to_db}\n"
            f"β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•"
        )

    @property
    def db(self) -> NeonDBWriter:
        if self._db is None:
            self._db = NeonDBWriter(self._db_url)
        return self._db

    # ── Main pipeline ─────────────────────────────────────────────────────────

    def run_diagnosis(self, inputs: BusinessInputs) -> DiagnosisReport:
        """

        Full Module 1 pipeline.



        Args:

            inputs: Completed BusinessInputs with all 24 question answers.



        Returns:

            DiagnosisReport with health_score, top_3_weaknesses,

            growth_opportunities, priority_action_items, and ai_narrative.

        """
        t_start = time.perf_counter()
        session_id = str(uuid.uuid4())

        log.info(f"β–Ά Starting BIOS diagnosis | business='{inputs.business_name}' | session={session_id}")

        # ── Step 1: Compute health dimensions and score ──────────────────────
        dims   = self.engine.compute_dimensions(inputs)
        score  = dims.total
        label  = self.engine.health_label(score)
        log.info(f"  Health Score: {score}/100 ({label})")
        log.info(f"  Dimensions: Rev={dims.revenue_strength} Ret={dims.customer_retention} Mkt={dims.market_position} Tech={dims.technology_adoption} Grow={dims.growth_trajectory}")

        # ── Step 2: Identify weaknesses ──────────────────────────────────────
        weaknesses = self.engine.identify_weaknesses(inputs, dims)
        log.info(f"  Weaknesses identified: {[w.label for w in weaknesses]}")

        # ── Step 3: Discover opportunities ───────────────────────────────────
        opportunities = self.engine.discover_opportunities(inputs, dims)
        log.info(f"  Opportunities found: {len(opportunities)}")

        # ── Step 4: Rank priority actions ────────────────────────────────────
        actions = self.engine.rank_priority_actions(weaknesses, inputs)

        # ── Step 5: Build benchmarking table ─────────────────────────────────
        benchmarking = self.engine.build_benchmarking(inputs)

        # ── Step 6: Generate AI narrative via LLM ────────────────────────────
        narrative, model_used = self._generate_narrative(inputs, dims, weaknesses, opportunities)
        log.info(f"  Narrative generated | model={model_used}")

        t_ms = int((time.perf_counter() - t_start) * 1000)

        # ── Step 7: Assemble report ───────────────────────────────────────────
        report = DiagnosisReport(
            session_id           = session_id,
            business_name        = inputs.business_name,
            industry             = inputs.industry,
            location             = inputs.location,
            generated_at         = datetime.now(timezone.utc).isoformat(),
            health_score         = score,
            health_label         = label,
            health_dimensions    = dims,
            top_3_weaknesses     = weaknesses,
            growth_opportunities = opportunities,
            priority_action_items= actions,
            ai_narrative         = narrative,
            benchmarking         = benchmarking,
            model_used           = model_used,
            generation_time_ms   = t_ms,
        )

        log.info(f"βœ” Diagnosis complete | score={score} | {t_ms}ms")

        # ── Step 8: Save to NeonDB ────────────────────────────────────────────
        if self.save_to_db:
            try:
                self.db.save_report(report)
            except Exception as e:
                log.warning(f"DB save failed (non-fatal): {e}")

        return report

    def _generate_narrative(

        self,

        inp:    BusinessInputs,

        dims:   HealthDimensions,

        weak:   list[Weakness],

        opps:   list[Opportunity],

    ) -> tuple[str, str]:
        """Call the LLM and return (narrative_text, model_identifier)."""
        system = self.generator.SYSTEM_PROMPT
        user   = self.generator.build_user_prompt(inp, dims, weak, opps)
        try:
            raw, model_id = self.router.infer(system, user)
            narrative     = self.generator.parse_narrative(raw)
            return narrative, model_id
        except Exception as e:
            log.warning(f"LLM call failed, using fallback narrative: {e}")
            fallback = (
                f"{inp.business_name} received a BIOS Health Score of {dims.total}/100 ({self.engine.health_label(dims.total)}). "
                f"Key areas for immediate attention: {', '.join(w.label for w in weak[:2])}. "
                f"Top opportunity: {opps[0].title if opps else 'revenue diversification'}."
            )
            return fallback, "fallback/static"

    # ── Convenience helpers ───────────────────────────────────────────────────

    def switch_to_bios_insight(self):
        """Activate BIOS-Insight-v1 once it is published on HuggingFace."""
        self.router.bios_insight_ready = True
        self.router.variant = ModelVariant.BIOS_INSIGHT
        log.info("🌟 Switched to BIOS-Insight-v1 (fine-tuned model)")

    def switch_to_base(self):
        """Revert to base llama-3.3-70b model."""
        self.router.bios_insight_ready = False
        self.router.variant = ModelVariant.BASE
        log.info("Reverted to base model (llama-3.3-70b)")

    def get_report(self, session_id: str) -> Optional[dict]:
        """Retrieve a saved report from NeonDB."""
        return self.db.fetch_report(session_id)

    def list_reports(self, limit: int = 20) -> list[dict]:
        """List recent diagnosis reports from NeonDB."""
        return self.db.list_reports(limit)


# ═══════════════════════════════════════════════════════════════════════════════
# CLI / DEMO RUNNER
# ═══════════════════════════════════════════════════════════════════════════════

def _demo_inputs() -> BusinessInputs:
    """Sample Technology Startup business for demonstration."""
    return BusinessInputs(
        # Section 1
        business_name           = "MyanmarTech Solutions",
        industry                = "Technology Startup",
        location                = "Yangon",
        years_in_business       = 2,
        monthly_revenue         = 8_500_000,
        team_size               = 12,
        # Section 2
        target_customer         = "SMEs in Myanmar seeking digital transformation and automation solutions.",
        acquisition_channels    = ["LinkedIn", "Tech Meetups", "Referrals", "Online Ads"],
        avg_customer_lifetime_value = 3_200_000,
        retention_rate          = 75.0,
        main_competitors        = "Digital Myanmar, TechHub Asia, CloudBase Solutions",
        unique_selling_proposition = "We provide AI-powered business automation specifically designed for Myanmar SMEs, with local language support and compliance.",
        # Section 3
        sales_channels          = ["SaaS Platform", "Direct Sales", "Partners"],
        operational_challenge   = "Scaling infrastructure while maintaining service quality",
        biggest_pain_point      = "Customer onboarding complexity - need streamlined setup process",
        current_technology      = ["Cloud Services", "AI/ML", "Microservices", "DevOps", "CI/CD"],
        marketing_channels      = ["LinkedIn", "Google Ads", "Content Marketing", "Tech Events"],
        monthly_marketing_budget= 1_200_000,
        # Section 4
        goal_3_month            = 12_000_000,
        goal_6_month            = 18_000_000,
        goal_12_month           = 35_000_000,
        budget_constraint       = "Flexible (1M+)",
        tech_readiness          = "Very ready",
        preferred_language      = "English",
    )


if __name__ == "__main__":
    print("\n" + "═" * 60)
    print("  BIOS β€” Business Idea Operating System")
    print("  BIOS-kernel-v1  Β·  Module 1: Business Diagnosis")
    print("═" * 60 + "\n")

    # ── Instantiate controller ────────────────────────────────────────────────
    # Set save_to_db=True and export DATABASE_URL to persist to NeonDB.
    controller = BIOSController(
        backend      = ModelBackend.GROQ,
        save_to_db   = bool(os.getenv("DATABASE_URL")),
    )

    # ── Run diagnosis ─────────────────────────────────────────────────────────
    inputs = _demo_inputs()
    report = controller.run_diagnosis(inputs)

    # ── Print structured JSON output ──────────────────────────────────────────
    print("\n" + "─" * 60)
    print("  BIOS DIAGNOSIS REPORT")
    print("─" * 60)
    print(report.to_json())

    print("\n" + "═" * 60)
    print(f"  Health Score : {report.health_score}/100  ({report.health_label})")
    print(f"  Session ID   : {report.session_id}")
    print(f"  Model Used   : {report.model_used}")
    print(f"  Generated in : {report.generation_time_ms}ms")
    print("═" * 60 + "\n")