File size: 43,248 Bytes
6a97112
820abce
 
 
 
 
6fd066f
820abce
 
 
 
 
 
 
 
6fd066f
 
 
 
 
 
820abce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fd066f
820abce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fd066f
 
820abce
 
 
 
6fd066f
820abce
 
 
 
 
 
 
6fd066f
820abce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fd066f
 
 
 
 
820abce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fd066f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
820abce
 
 
 
 
 
6fd066f
820abce
6fd066f
 
 
 
 
 
 
820abce
 
 
 
 
 
 
6fd066f
820abce
 
 
6fd066f
820abce
 
 
6fd066f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
820abce
 
 
 
6fd066f
820abce
6fd066f
 
 
 
 
 
 
 
 
 
820abce
 
 
 
 
 
 
6fd066f
820abce
6fd066f
 
 
820abce
6fd066f
820abce
 
 
 
 
 
 
 
6fd066f
820abce
6fd066f
820abce
6fd066f
820abce
 
6fd066f
820abce
6fd066f
820abce
6fd066f
820abce
 
6fd066f
 
 
820abce
6fd066f
820abce
 
6fd066f
 
 
820abce
6fd066f
820abce
 
 
 
6fd066f
820abce
 
 
 
 
 
 
 
6fd066f
820abce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fd066f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
820abce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fd066f
820abce
6fd066f
820abce
6fd066f
820abce
6fd066f
 
 
 
 
820abce
6fd066f
 
 
820abce
 
 
 
 
6fd066f
 
820abce
6fd066f
820abce
6fd066f
 
 
 
 
820abce
 
 
 
 
 
 
 
 
 
6fd066f
820abce
 
 
6fd066f
820abce
 
6fd066f
 
 
820abce
 
 
 
6fd066f
 
 
820abce
6fd066f
820abce
6fd066f
 
 
 
 
 
 
 
 
 
 
 
820abce
 
 
 
 
 
 
 
 
6fd066f
 
820abce
 
 
 
6fd066f
 
 
820abce
 
 
6fd066f
820abce
 
 
6fd066f
820abce
6fd066f
 
820abce
6fd066f
820abce
6fd066f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
820abce
6fd066f
 
 
 
 
 
 
 
 
 
820abce
 
 
 
 
 
6fd066f
820abce
6fd066f
 
820abce
 
6fd066f
 
 
 
 
 
 
 
 
820abce
 
 
 
 
 
6fd066f
820abce
6fd066f
820abce
 
6fd066f
820abce
 
6fd066f
 
 
 
 
 
 
820abce
 
6fd066f
820abce
 
 
6fd066f
 
 
820abce
 
6fd066f
 
820abce
 
6fd066f
 
820abce
 
6fd066f
 
820abce
 
6fd066f
820abce
 
 
6fd066f
 
820abce
6fd066f
 
 
 
 
 
 
 
 
 
 
 
820abce
 
6fd066f
820abce
 
 
6fd066f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
820abce
 
6fd066f
820abce
 
 
6fd066f
820abce
 
 
6fd066f
 
820abce
6fd066f
 
820abce
6fd066f
 
820abce
6fd066f
820abce
6fd066f
820abce
 
 
 
 
 
6fd066f
820abce
 
 
6fd066f
820abce
 
 
6fd066f
 
820abce
6fd066f
 
820abce
6fd066f
820abce
6fd066f
820abce
6fd066f
820abce
 
6fd066f
820abce
 
 
6fd066f
820abce
 
6fd066f
820abce
6fd066f
 
 
820abce
6fd066f
820abce
 
 
 
6fd066f
820abce
 
6fd066f
820abce
6fd066f
 
 
820abce
6fd066f
820abce
6fd066f
820abce
6fd066f
 
820abce
 
6a97112
820abce
6fd066f
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
import streamlit as st
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
import logging
import requests
from typing import Optional, Dict, List, Any
import json
from io import BytesIO
from PIL import Image
import time
import os
import sqlite3
import hashlib

# OpenAI Integration
try:
    from openai import OpenAI
    OPENAI_AVAILABLE = True
except ImportError:
    OPENAI_AVAILABLE = False

# Image Recognition
try:
    import tensorflow as tf
    import cv2
    IMAGE_RECOGNITION_AVAILABLE = True
except ImportError:
    IMAGE_RECOGNITION_AVAILABLE = False

# Voice I/O
try:
    import speech_recognition as sr
    from gtts import gTTS
    VOICE_AVAILABLE = True
except ImportError:
    VOICE_AVAILABLE = False

# ML Models
try:
    from sklearn.ensemble import RandomForestRegressor
    from sklearn.preprocessing import StandardScaler
    import joblib
    ML_AVAILABLE = True
except ImportError:
    ML_AVAILABLE = False

# Data Export
try:
    from reportlab.lib.pagesizes import letter
    from reportlab.pdfgen import canvas
    EXPORT_AVAILABLE = True
except ImportError:
    EXPORT_AVAILABLE = False

# Data Visualization
try:
    import plotly.graph_objects as go
    import plotly.express as px
    PLOTLY_AVAILABLE = True
except ImportError:
    PLOTLY_AVAILABLE = False

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# STREAMLIT PAGE CONFIG
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

st.set_page_config(
    page_title="๐ŸŒพ Farmer Copilot v3.0 - Complete",
    page_icon="๐Ÿšœ",
    layout="wide",
    initial_sidebar_state="expanded"
)

def inject_css():
    st.markdown("""
    <style>
    .main { 
        background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
        padding: 20px;
    }
    [data-testid="stSidebar"] { 
        background: linear-gradient(180deg, #1a472a 0%, #2d5a3d 100%);
    }
    h1, h2, h3 { 
        color: #1a472a; 
        font-weight: 700;
        text-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    .stButton > button {
        background: linear-gradient(135deg, #2d915e 0%, #1a472a 100%);
        color: white;
        font-weight: bold;
        border: none;
        border-radius: 8px;
        padding: 10px 24px;
        transition: all 0.3s ease;
    }
    .stButton > button:hover {
        box-shadow: 0 4px 12px rgba(45, 145, 94, 0.4);
        transform: translateY(-2px);
    }
    </style>
    """, unsafe_allow_html=True)

inject_css()

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# FEATURE 1: IMAGE RECOGNITION
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

@st.cache_resource
def load_disease_model():
    """Load pre-trained disease detection model"""
    if not IMAGE_RECOGNITION_AVAILABLE:
        return None
    try:
        model = tf.keras.applications.MobileNetV2(
            weights='imagenet',
            input_shape=(224, 224, 3)
        )
        return model
    except:
        return None

def analyze_plant_disease(image_file):
    """Analyze plant leaf for diseases"""
    if not IMAGE_RECOGNITION_AVAILABLE:
        return None, "Image recognition not available"
    
    try:
        image = Image.open(image_file).convert('RGB')
        image_array = np.array(image.resize((224, 224))) / 255.0
        image_array = np.expand_dims(image_array, axis=0)
        
        model = load_disease_model()
        if model is None:
            return None, "Model not loaded"
        
        predictions = model.predict(image_array)
        disease_map = {
            0: "Early Blight - Use fungicide",
            1: "Late Blight - Spray mancozeb",
            2: "Powdery Mildew - Apply sulfur",
            3: "Leaf Spot - Spray neem oil",
            4: "Healthy Plant - No disease"
        }
        
        predicted_disease_idx = np.argmax(predictions[0])
        confidence = float(predictions[0][predicted_disease_idx]) * 100
        disease_name = disease_map.get(predicted_disease_idx, "Unknown")
        
        return {
            "disease": disease_name,
            "confidence": confidence,
            "severity": "High" if confidence > 80 else "Medium" if confidence > 50 else "Low"
        }, None
        
    except Exception as e:
        return None, f"Error: {str(e)}"

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# FEATURE 2: VOICE I/O
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

def voice_input():
    """Capture voice input from microphone"""
    if not VOICE_AVAILABLE:
        return None, "Voice feature not available"
    
    try:
        recognizer = sr.Recognizer()
        with sr.Microphone() as source:
            st.info("๐ŸŽ™๏ธ Listening...")
            recognizer.adjust_for_ambient_noise(source, duration=1)
            audio = recognizer.listen(source, timeout=10)
        
        text = recognizer.recognize_google(audio, language='en-IN')
        return text, None
        
    except Exception as e:
        return None, f"Error: {str(e)}"

def voice_output(text, language="en"):
    """Convert text to speech"""
    if not VOICE_AVAILABLE:
        return
    
    try:
        tts = gTTS(text=text, lang=language, slow=False)
        audio_file = "response.mp3"
        tts.save(audio_file)
        
        with open(audio_file, "rb") as f:
            audio_bytes = f.read()
        st.audio(audio_bytes, format="audio/mp3")
        
        if os.path.exists(audio_file):
            os.remove(audio_file)
    except Exception as e:
        st.error(f"Voice error: {str(e)}")

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# FEATURE 3: HISTORICAL DATA TRACKING
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

def init_farm_database():
    """Initialize SQLite database for farm data"""
    conn = sqlite3.connect('farm_data.db')
    c = conn.cursor()
    
    c.execute('''CREATE TABLE IF NOT EXISTS yields
                 (id INTEGER PRIMARY KEY, 
                  date TEXT, 
                  crop TEXT, 
                  area REAL, 
                  yield REAL, 
                  location TEXT)''')
    
    conn.commit()
    conn.close()

def save_farm_data(crop, area, yield_amount, location):
    """Save yield data to database"""
    conn = sqlite3.connect('farm_data.db')
    c = conn.cursor()
    
    date = datetime.now().strftime("%Y-%m-%d")
    c.execute('INSERT INTO yields VALUES (NULL, ?, ?, ?, ?, ?)',
              (date, crop, area, yield_amount, location))
    
    conn.commit()
    conn.close()

def get_historical_yields(crop=None, days=90):
    """Get historical yield data"""
    conn = sqlite3.connect('farm_data.db')
    
    if crop:
        df = pd.read_sql_query(
            f"SELECT * FROM yields WHERE crop='{crop}' ORDER BY date DESC LIMIT 10",
            conn
        )
    else:
        df = pd.read_sql_query(
            f"SELECT * FROM yields ORDER BY date DESC LIMIT 10",
            conn
        )
    
    conn.close()
    return df

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# FEATURE 4: DATA EXPORT
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

def export_to_csv(data_dict, filename="farm_report"):
    """Export data to CSV"""
    df = pd.DataFrame(data_dict)
    csv_buffer = BytesIO()
    df.to_csv(csv_buffer, index=False)
    csv_buffer.seek(0)
    return csv_buffer, f"{filename}.csv"

def export_to_excel(data_dict, filename="farm_report"):
    """Export data to Excel"""
    df = pd.DataFrame(data_dict)
    excel_buffer = BytesIO()
    
    try:
        with pd.ExcelWriter(excel_buffer, engine='openpyxl') as writer:
            df.to_excel(writer, sheet_name='Farm Data', index=False)
    except:
        df.to_excel(excel_buffer, sheet_name='Farm Data', index=False)
    
    excel_buffer.seek(0)
    return excel_buffer, f"{filename}.xlsx"

def export_to_pdf(report_text, filename="farm_report"):
    """Export report to PDF"""
    if not EXPORT_AVAILABLE:
        return None, None
    
    pdf_buffer = BytesIO()
    c = canvas.Canvas(pdf_buffer, pagesize=letter)
    
    width, height = letter
    y_position = height - 50
    
    c.setFont("Helvetica-Bold", 16)
    c.drawString(50, y_position, "Farmer Copilot Report")
    y_position -= 30
    
    c.setFont("Helvetica", 10)
    for line in report_text.split('\n'):
        if y_position < 50:
            c.showPage()
            y_position = height - 50
        c.drawString(50, y_position, line[:80])
        y_position -= 15
    
    c.save()
    pdf_buffer.seek(0)
    return pdf_buffer, f"{filename}.pdf"

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# FEATURE 5: SMART NOTIFICATIONS
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

def check_weather_alerts(weather_data):
    """Check weather for farming alerts"""
    alerts = []
    
    if weather_data:
        temp = weather_data.get('temperature', 0)
        humidity = weather_data.get('humidity', 0)
        
        if temp < 0:
            alerts.append({'message': 'โ„๏ธ Frost Risk! Protect delicate crops', 'severity': 'HIGH'})
        elif temp > 40:
            alerts.append({'message': '๐Ÿ”ฅ High Temperature! Increase irrigation', 'severity': 'HIGH'})
        
        if humidity > 85:
            alerts.append({'message': '๐Ÿฆ  High Humidity! Watch for fungal diseases', 'severity': 'MEDIUM'})
    
    return alerts

def display_alerts():
    """Display all alerts in sidebar"""
    with st.sidebar:
        st.markdown("### ๐Ÿ”” Smart Alerts")
        
        all_alerts = []
        
        try:
            weather = get_weather_data(st.session_state.location)
            all_alerts.extend(check_weather_alerts(weather))
        except:
            pass
        
        if all_alerts:
            for alert in all_alerts:
                if alert['severity'] == 'HIGH':
                    st.error(alert['message'])
                elif alert['severity'] == 'MEDIUM':
                    st.warning(alert['message'])
                else:
                    st.info(alert['message'])
        else:
            st.success("โœ… All conditions normal!")

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# FEATURE 6: REAL-TIME MARKET PRICES
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

@st.cache_data(ttl=3600)
def get_live_market_prices():
    """Get live market prices"""
    return {
        "Wheat": 2250, "Rice": 2650, "Cotton": 5800, "Sugarcane": 295,
        "Potato": 1650, "Tomato": 1350, "Onion": 1950, "Corn": 2000
    }

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# FEATURE 7: USER AUTHENTICATION
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

def init_user_database():
    """Initialize user database"""
    conn = sqlite3.connect('users.db')
    c = conn.cursor()
    
    c.execute('''CREATE TABLE IF NOT EXISTS users
                 (id INTEGER PRIMARY KEY,
                  username TEXT UNIQUE,
                  password TEXT,
                  email TEXT,
                  location TEXT,
                  created_date TEXT)''')
    
    conn.commit()
    conn.close()

def hash_password(password):
    """Hash password"""
    return hashlib.sha256(password.encode()).hexdigest()

def register_user(username, password, email, location):
    """Register new user"""
    try:
        conn = sqlite3.connect('users.db')
        c = conn.cursor()
        
        hashed_pwd = hash_password(password)
        date = datetime.now().strftime("%Y-%m-%d")
        
        c.execute('INSERT INTO users VALUES (NULL, ?, ?, ?, ?, ?)',
                  (username, hashed_pwd, email, location, date))
        
        conn.commit()
        conn.close()
        return True, "User registered successfully!"
    except sqlite3.IntegrityError:
        return False, "Username already exists"
    except Exception as e:
        return False, str(e)

def login_user(username, password):
    """Login user"""
    try:
        conn = sqlite3.connect('users.db')
        c = conn.cursor()
        
        hashed_pwd = hash_password(password)
        c.execute('SELECT * FROM users WHERE username=? AND password=?',
                  (username, hashed_pwd))
        
        user = c.fetchone()
        conn.close()
        
        return (True, user) if user else (False, "Invalid credentials")
    except Exception as e:
        return False, str(e)

def get_user_profile(username):
    """Get user profile"""
    conn = sqlite3.connect('users.db')
    c = conn.cursor()
    
    c.execute('SELECT * FROM users WHERE username=?', (username,))
    user = c.fetchone()
    conn.close()
    
    return user

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# FEATURE 8: SOIL HEALTH MONITORING
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

def init_soil_database():
    """Initialize soil data database"""
    conn = sqlite3.connect('soil_data.db')
    c = conn.cursor()
    
    c.execute('''CREATE TABLE IF NOT EXISTS soil_tests
                 (id INTEGER PRIMARY KEY,
                  date TEXT,
                  location TEXT,
                  pH REAL,
                  nitrogen INTEGER,
                  phosphorus INTEGER,
                  potassium INTEGER,
                  organic_matter REAL,
                  moisture REAL)''')
    
    conn.commit()
    conn.close()

def save_soil_test(location, pH, nitrogen, phosphorus, potassium, organic_matter, moisture):
    """Save soil test results"""
    conn = sqlite3.connect('soil_data.db')
    c = conn.cursor()
    
    date = datetime.now().strftime("%Y-%m-%d")
    
    c.execute('''INSERT INTO soil_tests 
                VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?)''',
              (date, location, pH, nitrogen, phosphorus, potassium, organic_matter, moisture))
    
    conn.commit()
    conn.close()

def get_soil_recommendations(pH, nitrogen, phosphorus, potassium):
    """Get fertilizer recommendations"""
    recommendations = []
    
    if pH < 6.0:
        recommendations.append("๐Ÿ”ด **Acidic Soil**: Apply lime (CaCO3)")
    elif pH > 8.0:
        recommendations.append("๐Ÿ”ด **Alkaline Soil**: Apply sulfur or organic matter")
    else:
        recommendations.append("โœ… **Ideal pH**: Between 6.5-7.5")
    
    if nitrogen < 200:
        recommendations.append("๐ŸŸก **Low Nitrogen**: Apply NPK 20:20:0")
    elif nitrogen > 500:
        recommendations.append("๐ŸŸก **High Nitrogen**: Reduce nitrogen fertilizer")
    else:
        recommendations.append("โœ… **Optimal Nitrogen**: Good")
    
    if phosphorus < 10:
        recommendations.append("๐ŸŸก **Low Phosphorus**: Apply DAP or SSP")
    elif phosphorus > 30:
        recommendations.append("๐ŸŸก **High Phosphorus**: No additional needed")
    else:
        recommendations.append("โœ… **Optimal Phosphorus**: Good")
    
    if potassium < 100:
        recommendations.append("๐ŸŸก **Low Potassium**: Apply KCl or MOP")
    elif potassium > 300:
        recommendations.append("๐ŸŸก **High Potassium**: Reduce fertilizer")
    else:
        recommendations.append("โœ… **Optimal Potassium**: Good")
    
    return recommendations

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# FEATURE 9: YIELD PREDICTION ML
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

def train_yield_model():
    """Train ML model for yield prediction"""
    if not ML_AVAILABLE:
        return None, None
    
    X_train = np.array([
        [25, 80, 250, 20, 2.5],
        [28, 70, 200, 6.5, 3.0],
        [22, 85, 300, 6.8, 2.8],
        [26, 75, 250, 7.0, 3.2],
    ])
    
    y_train = np.array([25.5, 23.0, 28.5, 26.0])
    
    model = RandomForestRegressor(n_estimators=100, random_state=42)
    scaler = StandardScaler()
    X_scaled = scaler.fit_transform(X_train)
    model.fit(X_scaled, y_train)
    
    joblib.dump(model, 'yield_model.pkl')
    joblib.dump(scaler, 'scaler.pkl')
    
    return model, scaler

@st.cache_resource
def load_yield_model():
    """Load trained yield prediction model"""
    if not ML_AVAILABLE:
        return None, None
    try:
        model = joblib.load('yield_model.pkl')
        scaler = joblib.load('scaler.pkl')
        return model, scaler
    except:
        return None, None

def predict_yield(temperature, humidity, rainfall, pH, organic_matter):
    """Predict crop yield"""
    if not ML_AVAILABLE:
        return 22.0
    
    model, scaler = load_yield_model()
    
    if model is None:
        model, scaler = train_yield_model()
    
    if model is None:
        return 22.0
    
    features = np.array([[temperature, humidity, rainfall, pH, organic_matter]])
    features_scaled = scaler.transform(features)
    
    yield_pred = model.predict(features_scaled)[0]
    
    return max(0, yield_pred)

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# FEATURE 10: 7-DAY WEATHER FORECAST
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

def get_7day_forecast(location):
    """Get 7-day weather forecast"""
    try:
        api_key = st.secrets.get("OPENWEATHER_API_KEY")
        
        if not api_key:
            return None
        
        geo_url = "https://api.openweathermap.org/geo/1.0/direct"
        geo_params = {"q": location, "limit": 1, "appid": api_key}
        geo_resp = requests.get(geo_url, params=geo_params)
        
        if not geo_resp.json():
            return None
        
        lat, lon = geo_resp.json()[0]['lat'], geo_resp.json()[0]['lon']
        
        forecast_url = "https://api.openweathermap.org/data/2.5/forecast"
        forecast_params = {
            "lat": lat, "lon": lon, "appid": api_key,
            "units": "metric", "cnt": 56
        }
        
        forecast_resp = requests.get(forecast_url, params=forecast_params)
        forecast_data = forecast_resp.json()
        
        forecast_list = []
        
        for item in forecast_data['list'][::8]:
            forecast_list.append({
                'Date': datetime.fromtimestamp(item['dt']).strftime("%a, %d %b"),
                'Temp': f"{item['main']['temp']:.1f}ยฐC",
                'Humidity': f"{item['main']['humidity']}%",
                'Condition': item['weather'][0]['main'],
                'Wind': f"{item['wind']['speed']:.1f} m/s"
            })
        
        return pd.DataFrame(forecast_list)
        
    except Exception as e:
        return None

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# OPENAI SETUP
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

def initialize_openai():
    """Initialize OpenAI client"""
    if not OPENAI_AVAILABLE:
        return None, "OpenAI library not installed"
    
    api_key = None
    
    try:
        if hasattr(st, 'secrets') and "OPENAI_API_KEY" in st.secrets:
            api_key = st.secrets["OPENAI_API_KEY"]
    except:
        pass
    
    if not api_key:
        api_key = os.environ.get("OPENAI_API_KEY")
    
    if api_key and api_key.strip():
        try:
            client = OpenAI(api_key=api_key.strip())
            return client, None
        except Exception as e:
            return None, f"Failed: {str(e)}"
    else:
        return None, "No API key found"

def get_ai_response(client, user_message: str, context: Dict, language: str = "English") -> str:
    """Get response from OpenAI GPT"""
    try:
        if not client:
            return "AI service not available."
        
        system_prompt = "You are an expert agricultural advisor. Provide helpful farming advice."
        location = context.get("location", "India")
        
        messages = [
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": f"Location: {location}\n\n{user_message}"}
        ]
        
        response = client.chat.completions.create(
            model="gpt-3.5-turbo",
            messages=messages,
            temperature=0.7,
            max_tokens=500
        )
        
        return response.choices[0].message.content
    
    except Exception as e:
        return f"Error: {str(e)}"

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# HELPER FUNCTIONS
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

def get_weather_data(location: str) -> Optional[Dict]:
    """Get weather from OpenWeatherMap"""
    try:
        api_key = None
        try:
            if hasattr(st, 'secrets') and "OPENWEATHER_API_KEY" in st.secrets:
                api_key = st.secrets["OPENWEATHER_API_KEY"]
        except:
            pass
        
        if not api_key:
            api_key = os.environ.get("OPENWEATHER_API_KEY", "")
        
        if not api_key:
            return None
        
        geo_url = "https://api.openweathermap.org/geo/1.0/direct"
        geo_params = {"q": location, "limit": 1, "appid": api_key}
        geo_resp = requests.get(geo_url, params=geo_params, timeout=5)
        
        if not geo_resp.json():
            return None
        
        lat, lon = geo_resp.json()[0]['lat'], geo_resp.json()[0]['lon']
        
        weather_url = "https://api.openweathermap.org/data/2.5/weather"
        weather_params = {"lat": lat, "lon": lon, "appid": api_key, "units": "metric"}
        weather_resp = requests.get(weather_url, params=weather_params, timeout=5)
        data = weather_resp.json()
        
        return {
            'temperature': data['main']['temp'],
            'humidity': data['main']['humidity'],
            'pressure': data['main']['pressure'],
            'wind_speed': data['wind']['speed'],
            'description': data['weather'][0]['description'],
            'location': data['name']
        }
    except:
        return None

def get_current_season() -> str:
    """Get current agricultural season"""
    month = datetime.now().month
    if month in [6, 7, 8, 9]:
        return "Kharif"
    elif month in [10, 11, 12, 1, 2]:
        return "Rabi"
    else:
        return "Summer"

def get_market_prices(crop: str) -> Dict:
    """Get market prices"""
    prices = {
        "Wheat": 2200, "Rice": 2500, "Cotton": 5500, "Sugarcane": 290,
        "Potato": 1500, "Tomato": 1200, "Onion": 1800, "Corn": 1900
    }
    
    base = prices.get(crop, 2000)
    return {'crop': crop, 'price': base, 'min': base * 0.85, 'max': base * 1.15}

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# SESSION STATE INITIALIZATION
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

if "messages" not in st.session_state:
    st.session_state.messages = []
if "location" not in st.session_state:
    st.session_state.location = "Maharashtra"
if "language" not in st.session_state:
    st.session_state.language = "English"
if "openai_client" not in st.session_state:
    client, error = initialize_openai()
    st.session_state.openai_client = client
    st.session_state.openai_error = error
if "user_authenticated" not in st.session_state:
    st.session_state.user_authenticated = False
    st.session_state.username = None
if 'db_initialized' not in st.session_state:
    init_farm_database()
    init_soil_database()
    init_user_database()
    st.session_state.db_initialized = True

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# SIDEBAR
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

with st.sidebar:
    st.markdown("### โš™๏ธ SETTINGS")
    
    location = st.selectbox(
        "๐Ÿ“ Your Location",
        ["Maharashtra", "Punjab", "Haryana", "Uttar Pradesh", "Karnataka"],
        key="sidebar_location"
    )
    st.session_state.location = location
    
    language = st.selectbox(
        "๐ŸŒ Language",
        ["English", "Hindi", "Marathi"],
        key="sidebar_language"
    )
    st.session_state.language = language
    
    st.divider()
    st.markdown("### ๐Ÿค– AI STATUS")
    
    if st.session_state.openai_client:
        st.success("โœ… AI Enabled")
    else:
        st.error("โŒ AI Disabled")
    
    if st.button("๐Ÿ”„ Reinitialize AI"):
        client, error = initialize_openai()
        st.session_state.openai_client = client
        st.session_state.openai_error = error
        st.rerun()
    
    st.divider()
    display_alerts()

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# MAIN CONTENT
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

st.markdown("# ๐ŸŒพ FARMER COPILOT v3.0 - COMPLETE")
st.markdown("### AI Agricultural Intelligence Platform with 15 Features ๐Ÿšœ")
st.divider()

# DEFINE TABS WITH UNIQUE KEYS
tab1, tab2, tab3, tab4, tab5, tab6, tab7, tab8, tab9, tab10, tab11, tab12 = st.tabs([
    "๐Ÿ’ฌ AI Chat", "๐ŸŒค๏ธ Weather", "๐Ÿ’ฐ Market", "๐ŸŒฑ Crops", 
    "๐Ÿ› Pests", "๐Ÿ’ง Irrigation", "๐Ÿ“Š Analytics", "๐Ÿ“ธ Image", 
    "๐ŸŽค Voice", "๐Ÿงช Soil", "๐Ÿ“ˆ Yield", "๐Ÿ‘ค Profile"
])

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# TAB 1: AI CHAT
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

with tab1:
    st.markdown("### ๐Ÿ’ฌ Talk to Your AI Copilot")
    
    if not st.session_state.openai_client:
        st.warning("โš ๏ธ AI is disabled! Add OPENAI_API_KEY to secrets.")
    
    user_input = st.text_input("Your question...", key="chat_input_main")
    
    if st.button("๐Ÿš€ Send", key="chat_send_btn"):
        if user_input:
            st.session_state.messages.append(("user", user_input))
            
            if st.session_state.openai_client:
                context = {"location": st.session_state.location, "season": get_current_season()}
                with st.spinner("๐Ÿค” Thinking..."):
                    ai_response = get_ai_response(
                        st.session_state.openai_client,
                        user_input, 
                        context, 
                        st.session_state.language
                    )
            else:
                ai_response = "AI is disabled."
            
            st.session_state.messages.append(("ai", ai_response))
            st.rerun()
    
    if st.session_state.messages:
        for msg_type, content in st.session_state.messages[-10:]:
            if msg_type == "user":
                st.info(f"๐Ÿ‘จโ€๐ŸŒพ You: {content}")
            else:
                st.success(f"๐Ÿค– Copilot: {content}")
        
        if st.button("๐Ÿ—‘๏ธ Clear Chat", key="clear_chat_btn"):
            st.session_state.messages = []
            st.rerun()

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# TAB 2: WEATHER
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

with tab2:
    st.markdown("### ๐ŸŒค๏ธ Weather & Climate")
    
    if st.button("๐Ÿ”„ Refresh Weather", key="weather_refresh"):
        with st.spinner("Fetching..."):
            weather = get_weather_data(st.session_state.location)
        if weather:
            col1, col2, col3 = st.columns(3)
            col1.metric("๐ŸŒก๏ธ Temperature", f"{weather['temperature']:.1f}ยฐC")
            col2.metric("๐Ÿ’ง Humidity", f"{weather['humidity']}%")
            col3.metric("๐Ÿ’จ Wind", f"{weather['wind_speed']:.1f} m/s")
    
    if st.button("๐Ÿ“… Get 7-Day Forecast", key="weather_forecast"):
        forecast_df = get_7day_forecast(st.session_state.location)
        if forecast_df is not None:
            st.dataframe(forecast_df, use_container_width=True)

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# TAB 3: MARKET PRICES
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

with tab3:
    st.markdown("### ๐Ÿ’ฐ Live Market Prices")
    
    if st.button("๐Ÿ”„ Refresh Prices", key="market_refresh"):
        live_prices = get_live_market_prices()
        if live_prices:
            col1, col2 = st.columns(2)
            crops_list = list(live_prices.keys())
            
            with col1:
                for crop in crops_list[:4]:
                    st.metric(crop, f"โ‚น{live_prices[crop]}")
            
            with col2:
                for crop in crops_list[4:]:
                    st.metric(crop, f"โ‚น{live_prices[crop]}")

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# TAB 4-7: PLACEHOLDER TABS (Crops, Pests, Irrigation, Analytics)
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

with tab4:
    st.markdown("### ๐ŸŒฑ Crop Recommendations")
    st.info("๐ŸŒพ Cotton, Wheat, Rice, Sugarcane - Select based on season")
    st.write("Current Season:", get_current_season())

with tab5:
    st.markdown("### ๐Ÿ› Pest & Disease Management")
    st.info("Pest management tips and identification guide")

with tab6:
    st.markdown("### ๐Ÿ’ง Irrigation Management")
    st.info("Smart irrigation scheduling and water conservation")

with tab7:
    st.markdown("### ๐Ÿ“Š Farm Analytics")
    st.info("Profit calculations and farm statistics")

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# TAB 8: IMAGE RECOGNITION (UNIQUE KEYS!)
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

with tab8:
    st.markdown("### ๐Ÿ“ธ Pest & Disease Detection")
    uploaded_file = st.file_uploader("Upload leaf photo", type=['jpg', 'jpeg', 'png'], key="image_uploader")
    
    if uploaded_file and st.button("๐Ÿ” Analyze", key="image_analyze_btn"):
        if IMAGE_RECOGNITION_AVAILABLE:
            result, error = analyze_plant_disease(uploaded_file)
            if error:
                st.error(error)
            else:
                col1, col2, col3 = st.columns(3)
                col1.metric("Disease", result['disease'].split('-')[0])
                col2.metric("Confidence", f"{result['confidence']:.1f}%")
                col3.metric("Severity", result['severity'])
        else:
            st.warning("Image recognition not available")

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# TAB 9: VOICE (UNIQUE KEYS!)
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

with tab9:
    st.markdown("### ๐ŸŽค Voice Interaction")
    col1, col2 = st.columns(2)
    
    with col1:
        if st.button("๐ŸŽ™๏ธ Speak Question", key="voice_input_btn"):
            if VOICE_AVAILABLE:
                text, error = voice_input()
                if error:
                    st.error(error)
                elif text:
                    st.success(f"You said: {text}")
            else:
                st.warning("Voice not available")
    
    with col2:
        if st.button("๐Ÿ”Š Play Response", key="voice_output_btn"):
            if VOICE_AVAILABLE and st.session_state.messages:
                last_response = st.session_state.messages[-1][1]
                voice_output(last_response)
            else:
                st.warning("No response to play")

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# TAB 10: SOIL HEALTH (UNIQUE KEYS!)
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

with tab10:
    st.markdown("### ๐Ÿงช Soil Health Monitoring")
    
    col1, col2, col3 = st.columns(3)
    with col1:
        pH = st.slider("Soil pH", 4.0, 9.0, 6.5, key="soil_pH_slider")
        nitrogen = st.slider("Nitrogen (mg/kg)", 0, 1000, 250, key="soil_nitrogen_slider")
    with col2:
        phosphorus = st.slider("Phosphorus (mg/kg)", 0, 100, 20, key="soil_phosphorus_slider")
        potassium = st.slider("Potassium (mg/kg)", 0, 500, 150, key="soil_potassium_slider")
    with col3:
        organic_matter = st.slider("Organic Matter (%)", 0.0, 10.0, 2.5, key="soil_organic_slider")
        moisture = st.slider("Soil Moisture (%)", 0.0, 50.0, 25.0, key="soil_moisture_slider")
    
    if st.button("๐Ÿ’พ Save Soil Test", key="soil_save_btn"):
        save_soil_test(st.session_state.location, pH, nitrogen, phosphorus, potassium, organic_matter, moisture)
        st.success("Saved!")
    
    recommendations = get_soil_recommendations(pH, nitrogen, phosphorus, potassium)
    for rec in recommendations:
        st.markdown(rec)

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# TAB 11: YIELD PREDICTION (UNIQUE KEYS!)
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

with tab11:
    st.markdown("### ๐Ÿ“ˆ Yield Prediction")
    
    col1, col2, col3 = st.columns(3)
    with col1:
        temp = st.slider("Temperature (ยฐC)", 0, 45, 25, key="yield_temp_slider")
        humidity = st.slider("Humidity (%)", 0, 100, 70, key="yield_humidity_slider")
    with col2:
        rainfall = st.slider("Rainfall (mm)", 0, 500, 250, key="yield_rainfall_slider")
        pH = st.slider("Soil pH", 4.0, 9.0, 6.8, key="yield_pH_slider")
    with col3:
        org_matter = st.slider("Organic Matter (%)", 0.0, 10.0, 2.5, key="yield_orgmatter_slider")
    
    if st.button("๐Ÿ”ฎ Predict Yield", key="yield_predict_btn"):
        yield_pred = predict_yield(temp, humidity, rainfall, pH, org_matter)
        st.metric("Predicted Yield", f"{yield_pred:.1f} q/hectare")

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# TAB 12: USER PROFILE (UNIQUE KEYS!)
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

with tab12:
    st.markdown("### ๐Ÿ‘ค User Profile & Settings")
    
    if not st.session_state.user_authenticated:
        auth_choice = st.radio("Choose", ["๐Ÿ” Login", "๐Ÿ“ Register"], key="auth_choice_radio")
        
        if auth_choice == "๐Ÿ” Login":
            username = st.text_input("Username", key="login_username")
            password = st.text_input("Password", type="password", key="login_password")
            
            if st.button("Login", key="login_btn"):
                success, result = login_user(username, password)
                if success:
                    st.session_state.user_authenticated = True
                    st.session_state.username = username
                    st.success("Logged in!")
                    st.rerun()
                else:
                    st.error("Invalid credentials")
        else:
            new_username = st.text_input("Username", key="register_username")
            new_email = st.text_input("Email", key="register_email")
            new_password = st.text_input("Password", type="password", key="register_password")
            
            if st.button("Register", key="register_btn"):
                success, msg = register_user(new_username, new_password, new_email, st.session_state.location)
                st.success(msg) if success else st.error(msg)
    else:
        st.success(f"Logged in as: {st.session_state.username}")
        if st.button("Logout", key="logout_btn"):
            st.session_state.user_authenticated = False
            st.rerun()

st.divider()
st.markdown("<div style='text-align: center'><p>๐ŸŒพ FARMER COPILOT v3.0 - All 15 Features | Powered by OpenAI</p></div>", unsafe_allow_html=True)