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
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
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
import logging
import os
import sys
import traceback
from datetime import datetime
import streamlit as st
from jira import JIRA
from dotenv import load_dotenv
from datetime import datetime, timedelta
import pandas as pd
import requests
import json
from groq import Groq
from difflib import SequenceMatcher
import time

# Configure logging based on environment
try:
    # Try to create logs directory and file
    log_dir = "logs"
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)
    log_file = os.path.join(log_dir, f"jira_debug_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log")

    # Configure root logger with file handler
    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        handlers=[logging.FileHandler(log_file)]
    )
except (OSError, IOError):
    # If file logging fails (e.g., in Hugging Face Spaces), configure logging without file handler
    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        handlers=[logging.NullHandler()]
    )

logger = logging.getLogger("jira_integration")
logger.info("Jira integration module loaded")

# Load environment variables
load_dotenv()

# Get API keys and configuration with default values for development
JIRA_SERVER = os.getenv("JIRA_SERVER")
GROQ_API_KEY = os.getenv("GROQ_API_KEY")

# Validate required environment variables
if not JIRA_SERVER:
    st.error("JIRA_SERVER not found in environment variables. Please check your .env file.")
if not GROQ_API_KEY:
    st.error("GROQ_API_KEY not found in environment variables. Please check your .env file.")

def init_jira_session():
    """Initialize Jira session state variables"""
    if 'jira_client' not in st.session_state:
        st.session_state.jira_client = None
    if 'projects' not in st.session_state:
        st.session_state.projects = None

def get_projects():
    """Fetch all accessible projects"""
    if not st.session_state.jira_client:
        return None
    
    try:
        projects = st.session_state.jira_client.projects()
        # Sort projects by key
        return sorted(projects, key=lambda x: x.key)
    except Exception as e:
        st.error(f"Error fetching projects: {str(e)}")
        return None

def get_board_configuration(board_id):
    """Fetch board configuration including estimation field"""
    if not st.session_state.jira_client:
        return None
    
    try:
        url = f"{JIRA_SERVER}/rest/agile/1.0/board/{board_id}/configuration"
        response = st.session_state.jira_client._session.get(url)
        if response.status_code == 200:
            config = response.json()
            return config
        return None
    except Exception as e:
        st.error(f"Error fetching board configuration: {str(e)}")
        return None

def get_boards(project_key):
    """Fetch all boards for a project"""
    if not st.session_state.jira_client:
        return None
    
    try:
        boards = st.session_state.jira_client.boards(projectKeyOrID=project_key)
        board_details = []
        for board in boards:
            config = get_board_configuration(board.id)
            board_type = config.get('type', 'Unknown') if config else 'Unknown'
            estimation_field = None
            if config and 'estimation' in config:
                estimation_field = config['estimation'].get('field', {}).get('fieldId')
            board_details.append({
                'id': board.id,
                'name': board.name,
                'type': board_type,
                'estimation_field': estimation_field
            })
        return board_details
    except Exception as e:
        st.error(f"Error fetching boards: {str(e)}")
        return None

def get_current_sprint(board_id):
    """Fetch the current sprint for a board"""
    if not st.session_state.jira_client:
        return None
    
    try:
        # Get all active and future sprints
        sprints = st.session_state.jira_client.sprints(board_id, state='active,future')
        if sprints:
            # Look for sprints starting with 'RS'
            rs_sprints = [sprint for sprint in sprints if sprint.name.startswith('RS')]
            if rs_sprints:
                # Sort sprints by name to get the latest one
                latest_sprint = sorted(rs_sprints, key=lambda x: x.name, reverse=True)[0]
                return latest_sprint
            else:
                st.warning("No RS sprints found. Available sprints: " + ", ".join([s.name for s in sprints]))
        return None
    except Exception as e:
        if "board does not support sprints" in str(e).lower():
            return None
        st.error(f"Error fetching current sprint: {str(e)}")
        return None

def get_board_issues(board_id, estimation_field=None):
    """Fetch all issues on the board using Agile REST API"""
    if not st.session_state.jira_client:
        return None
        
    try:
        url = f"{JIRA_SERVER}/rest/agile/1.0/board/{board_id}/issue"
        fields = ['summary', 'status', 'created', 'description', 'issuetype', 'assignee']
        
        if estimation_field:
            fields.append(estimation_field)
        
        params = {
            'maxResults': 200,
            'fields': fields,
            'jql': f'assignee = currentUser()'
        }
            
        response = st.session_state.jira_client._session.get(url, params=params)
        if response.status_code != 200:
            st.error(f"Error fetching board issues: {response.text}")
            return None
            
        data = response.json()
        issues = []
        for issue_data in data['issues']:
            issue = st.session_state.jira_client.issue(issue_data['key'])
            issues.append(issue)
        return issues
    except Exception as e:
        st.error(f"Error fetching board issues: {str(e)}")
        return None

def get_sprint_issues(board_id, sprint_id, estimation_field=None):
    """Fetch all issues in the current sprint using Agile REST API"""
    if not st.session_state.jira_client:
        return None
        
    try:
        # Cache key for sprint issues
        cache_key = f"sprint_issues_{sprint_id}"
        if cache_key in st.session_state and (datetime.now() - st.session_state.get('last_refresh', datetime.min)).total_seconds() < 60:
            return st.session_state[cache_key]
        
        # Use the Agile REST API endpoint to get issues from the sprint
        url = f"{JIRA_SERVER}/rest/agile/1.0/board/{board_id}/sprint/{sprint_id}/issue"
        params = {
            'maxResults': 200,
            'fields': [
                'summary', 
                'status', 
                'created', 
                'description', 
                'issuetype', 
                'assignee'
            ],
            'jql': 'assignee = currentUser()'  # Filter for current user's issues
        }
        
        # Add estimation field if provided
        if estimation_field:
            params['fields'].append(estimation_field)
            
        # Make a single API call with all required fields
        response = st.session_state.jira_client._session.get(url, params=params)
        if response.status_code != 200:
            st.error(f"Error fetching sprint issues: {response.text}")
            return None
            
        # Process all issues in one go
        data = response.json()
        issues = [st.session_state.jira_client.issue(issue['key']) for issue in data['issues']]
        
        # Cache the results
        st.session_state[cache_key] = issues
        st.session_state['last_refresh'] = datetime.now()
        
        return issues
    except Exception as e:
        st.error(f"Error fetching sprint issues: {str(e)}")
        return None

def calculate_points(issues, estimation_field):
    """Calculate story points from issues"""
    if not estimation_field:
        return [], 0, 0, 0
        
    try:
        # Process all issues at once
        field_id = estimation_field.replace('customfield_', '')
        issues_data = []
        total_points = completed_points = in_progress_points = 0
        
        # Create status mappings for faster lookup
        done_statuses = {'done', 'completed', 'closed'}
        progress_statuses = {'in progress', 'development', 'in development'}
        
        for issue in issues:
            try:
                # Get points value efficiently
                points = getattr(issue.fields, field_id, None) or getattr(issue.fields, estimation_field, 0)
                points = float(points) if points is not None else 0
                
                # Get status efficiently
                status_name = issue.fields.status.name.lower()
                
                # Update points
                total_points += points
                if status_name in done_statuses:
                    completed_points += points
                elif status_name in progress_statuses:
                    in_progress_points += points
                    
                # Build issue data
                issues_data.append({
                    "Key": issue.key,
                    "Type": issue.fields.issuetype.name,
                    "Summary": issue.fields.summary,
                    "Status": issue.fields.status.name,
                    "Story Points": points,
                    "Assignee": issue.fields.assignee.displayName if issue.fields.assignee else "Unassigned"
                })
            except Exception as e:
                st.error(f"Error processing points for issue {issue.key}: {str(e)}")
                continue
        
        return issues_data, total_points, completed_points, in_progress_points
    except Exception as e:
        st.error(f"Error calculating points: {str(e)}")
        return [], 0, 0, 0

def create_maintenance_task(project_key, summary, description, issue_type='Task'):
    """Create a task in Jira"""
    if not st.session_state.jira_client:
        st.error("Not authenticated with Jira. Please log in first.")
        return None
        
    try:
        issue_dict = {
            'project': {'key': project_key},
            'summary': summary,
            'description': description,
            'issuetype': {'name': issue_type}
        }
        
        new_issue = st.session_state.jira_client.create_issue(fields=issue_dict)
        return new_issue
    except Exception as e:
        st.error(f"Error creating task: {str(e)}")
        return None

def render_jira_login():
    """Render the Jira login form and handle authentication"""
    # If already authenticated, just return True
    if 'jira_client' in st.session_state and st.session_state.jira_client:
        st.success("Connected to Jira")
        return True
    
    # Initialize session state for login form and attempts tracking
    if 'jira_username' not in st.session_state:
        st.session_state.jira_username = ""
    if 'jira_password' not in st.session_state:
        st.session_state.jira_password = ""
    if 'login_blocked_until' not in st.session_state:
        st.session_state.login_blocked_until = None
    
    # Check if login is temporarily blocked
    if st.session_state.login_blocked_until:
        if datetime.now() < st.session_state.login_blocked_until:
            wait_time = (st.session_state.login_blocked_until - datetime.now()).seconds
            st.error(f"Login temporarily blocked due to too many failed attempts. Please wait {wait_time} seconds before trying again.")
            st.info("If you need immediate access, please try logging in directly to Jira in your browser first, complete the CAPTCHA there, then return here.")
            return False
        else:
            st.session_state.login_blocked_until = None
    
    # Create login form
    with st.form(key="jira_login_form"):
        username = st.text_input("Jira Username", value=st.session_state.jira_username, key="username_input")
        password = st.text_input("Password", value=st.session_state.jira_password, type="password", key="password_input")
        submit_button = st.form_submit_button(label="Login")
        
        if submit_button:
            # Store credentials in session state
            st.session_state.jira_username = username
            st.session_state.jira_password = password
            
            # Try to authenticate
            try:
                jira_client = JIRA(server=JIRA_SERVER, basic_auth=(username, password))
                st.session_state.jira_client = jira_client
                
                # Get projects
                projects = get_projects()
                if projects:
                    st.session_state.projects = projects
                    st.success("Connected to Jira")
                    return True
                else:
                    st.error("Failed to fetch projects")
                    return False
            except Exception as e:
                error_message = str(e).lower()
                if "captcha_challenge" in error_message:
                    # Set a 5-minute block on login attempts
                    st.session_state.login_blocked_until = datetime.now() + timedelta(minutes=5)
                    st.error("Too many failed login attempts. Please try one of the following:")
                    st.info("""
                    1. Wait 5 minutes before trying again
                    2. Log in to Jira in your browser first, complete the CAPTCHA there, then return here
                    3. Clear your browser cookies and try again
                    """)
                else:
                    st.error(f"Authentication failed: {str(e)}")
                return False
    
    return False

def get_cached_metadata(project_key):
    """Get cached metadata or fetch new if cache is expired or doesn't exist"""
    # Check if metadata cache exists in session state
    if 'metadata_cache' not in st.session_state:
        st.session_state.metadata_cache = {}
    if 'metadata_cache_timestamp' not in st.session_state:
        st.session_state.metadata_cache_timestamp = {}
    
    current_time = datetime.now()
    cache_expiry = timedelta(minutes=30)  # Cache expires after 30 minutes
    
    # Check if we have valid cached metadata
    if (project_key in st.session_state.metadata_cache and
        project_key in st.session_state.metadata_cache_timestamp and
        current_time - st.session_state.metadata_cache_timestamp[project_key] < cache_expiry):
        logger.info(f"Using cached metadata for project {project_key}")
        return st.session_state.metadata_cache[project_key]
    
    # If no valid cache, fetch new metadata
    logger.info(f"Fetching fresh metadata for project {project_key}")
    metadata = get_project_metadata_fresh(project_key)
    
    if metadata:
        # Update cache
        st.session_state.metadata_cache[project_key] = metadata
        st.session_state.metadata_cache_timestamp[project_key] = current_time
        logger.info(f"Updated metadata cache for project {project_key}")
    
    return metadata

def get_project_metadata_fresh(project_key):
    """Fetch fresh metadata from Jira without using cache"""
    logger.info(f"=== Getting fresh metadata for project {project_key} ===")
    
    if not st.session_state.jira_client:
        logger.error("Not authenticated with Jira. Please log in first.")
        st.error("Not authenticated with Jira. Please log in first.")
        return None
        
    try:
        # Get project
        logger.info("Getting project...")
        project = st.session_state.jira_client.project(project_key)
        logger.info(f"Got project: {project.name}")
        
        logger.info("Getting createmeta with expanded fields...")
        # Get create metadata for the project with expanded field info
        metadata = st.session_state.jira_client.createmeta(
            projectKeys=project_key,
            expand='projects.issuetypes.fields',
            issuetypeNames='Story'  # Specifically get Story type fields
        )
        logger.info("Got createmeta response")
        
        if not metadata.get('projects'):
            logger.error(f"No metadata found for project {project_key}")
            st.error(f"No metadata found for project {project_key}")
            return None
            
        project_meta = metadata['projects'][0]
        issue_types = project_meta.get('issuetypes', [])
        
        # Log available issue types
        logger.info(f"Available issue types: {[t.get('name') for t in issue_types]}")
        
        # Try to get Story issue type first
        story_type = next((t for t in issue_types if t['name'] == 'Story'), None)
        if not story_type:
            logger.error("Story issue type not found in project")
            st.error("Story issue type not found in project")
            return None
            
        logger.info("Processing fields...")
        # Get required fields and all fields
        required_fields = {}
        all_fields = {}
        
        # Log all available fields before processing
        logger.info("Available fields in Story type:")
        for field_id, field in story_type['fields'].items():
            field_name = field.get('name', 'Unknown')
            field_type = field.get('schema', {}).get('type', 'Unknown')
            logger.info(f"Field: {field_name} (ID: {field_id}, Type: {field_type})")
            
            # Store complete field information including schema and allowed values
            all_fields[field_id] = {
                'name': field['name'],
                'required': field.get('required', False),
                'schema': field.get('schema', {}),
                'allowedValues': field.get('allowedValues', []),
                'hasDefaultValue': field.get('hasDefaultValue', False),
                'defaultValue': field.get('defaultValue'),
                'operations': field.get('operations', []),
                'configuration': field.get('configuration', {})
            }
            
            # If this is a cascading select field, log its structure
            if field.get('schema', {}).get('type') == 'option-with-child':
                logger.info(f"Found cascading select field: {field_name}")
                if 'allowedValues' in field:
                    for parent in field['allowedValues']:
                        parent_value = parent.get('value', 'Unknown')
                        logger.info(f"  Parent value: {parent_value}")
                        if 'cascadingOptions' in parent:
                            child_values = [child.get('value') for child in parent['cascadingOptions']]
                            logger.info(f"    Child values: {child_values}")
            
            # Store required fields separately
            if field.get('required', False):
                required_fields[field_id] = all_fields[field_id]
                logger.info(f"Required field: {field_name}")
        
        logger.info(f"Found {len(all_fields)} total fields, {len(required_fields)} required fields")
        
        metadata_result = {
            'project_name': project.name,
            'issue_type': 'Story',
            'required_fields': required_fields,
            'all_fields': all_fields
        }
        
        logger.info("Successfully processed project metadata")
        return metadata_result
        
    except Exception as e:
        logger.exception(f"Error getting project metadata: {str(e)}")
        st.error(f"Error getting project metadata: {str(e)}")
        st.error("Full error details:")
        st.error(str(e))
        st.code(traceback.format_exc(), language="python")
        return None

def get_project_metadata(project_key):
    """Get project metadata, using cache if available"""
    return get_cached_metadata(project_key)

def generate_task_content(filtered_scenarios_df):
    """Generate task summary and description using template-based approach"""
    try:
        # Extract key information
        environment = filtered_scenarios_df['Environment'].iloc[0]
        functional_area = filtered_scenarios_df['Functional area'].iloc[0]
        scenario_count = len(filtered_scenarios_df)
        
        # Generate summary
        summary = f"Maintenance: {environment} - {functional_area}"
        
        # Generate description
        description = "Performing maintenance on the following scenarios failing :\n\n"
        
        # Add each scenario and its error, using enumerate to start from 1
        for i, (_, row) in enumerate(filtered_scenarios_df.iterrows(), 1):
            description += f"{i}. {row['Scenario Name']}\n"
            description += f"   Error: {row['Error Message']}\n\n"
            
        return summary, description
    except Exception as e:
        st.error(f"Error generating task content: {str(e)}")
        return None, None

def get_regression_board(project_key):
    """Find the regression sprint board for the project"""
    boards = get_boards(project_key)
    if not boards:
        return None
        
    # Look specifically for the "Regression Sprints" board
    regression_board = next((b for b in boards if b['name'].lower() == 'regression sprints' and b['type'].lower() == 'scrum'), None)
    
    if not regression_board:
        st.error("Could not find the 'Regression Sprints' board. Available boards: " + 
                ", ".join([f"{b['name']} ({b['type']})" for b in boards]))
    
    return regression_board

def get_field_dependencies():
    """Cache and return field dependencies and their allowed values"""
    if 'field_dependencies' not in st.session_state:
        try:
            # Get project metadata for RS project
            metadata = get_project_metadata("RS")
            if not metadata:
                return None
            
            # Initialize dependencies dictionary with correct field IDs
            dependencies = {
                'Customer': {
                    'field_id': 'customfield_10427',
                    'values': [],
                    'dependencies': {}
                },
                'Environment': {
                    'field_id': 'customfield_14157',  # Updated field ID
                    'values': [],
                    'dependencies': {}
                },
                'Functional Areas': {
                    'field_id': 'customfield_15303',  # Updated field ID
                    'values': [],
                    'dependencies': {}
                }
            }
            
            # Get field values and their dependencies
            for field_name, field_info in dependencies.items():
                field_id = field_info['field_id']
                if field_id in metadata['all_fields']:
                    field_data = metadata['all_fields'][field_id]
                    if 'allowedValues' in field_data:
                        # Store allowed values
                        dependencies[field_name]['values'] = [
                            value.get('value', value.get('name', ''))
                            for value in field_data['allowedValues']
                            if isinstance(value, dict)
                        ]
                        
                        # Store dependencies (if any)
                        if 'dependency' in field_data:
                            dep_field = field_data['dependency']
                            dependencies[field_name]['dependencies'] = {
                                'field': dep_field['field']['name'],
                                'field_id': dep_field['field']['id'],
                                'values': dep_field.get('values', [])
                            }
            
            # Cache the dependencies
            st.session_state.field_dependencies = dependencies
            return dependencies
        except Exception as e:
            st.error(f"Error fetching field dependencies: {str(e)}")
            return None
    
    return st.session_state.field_dependencies

def get_dependent_field_value(field_name, parent_value=None):
    """Get the appropriate field value based on dependencies"""
    dependencies = get_field_dependencies()
    if not dependencies or field_name not in dependencies:
        return None
    
    field_info = dependencies[field_name]
    
    # If this field depends on another field
    if parent_value and field_info['dependencies']:
        dep_info = field_info['dependencies']
        # Find values that match the parent value
        for value_mapping in dep_info.get('values', []):
            if value_mapping.get('parent') == parent_value:
                return value_mapping.get('value')
    
    # If no dependency or no match, return first available value
    return field_info['values'][0] if field_info['values'] else None

def display_project_fields():
    """Display available fields for issue creation"""
    project_key = "RS"  # Using the fixed project key
    metadata = get_project_metadata(project_key)
    
    if metadata:
        st.subheader("Project Fields")
        
        # Display required fields
        st.write("### Required Fields")
        for field_id, field in metadata['required_fields'].items():
            st.write(f"- {field['name']} ({field_id})")
            if field.get('allowedValues'):
                st.write("  Allowed values:")
                for value in field['allowedValues']:
                    if isinstance(value, dict):
                        # Handle cascading select fields
                        if 'cascadingOptions' in value:
                            parent_value = value.get('value', 'Unknown')
                            st.write(f"  - {parent_value}")
                            st.write("    Child options:")
                            for child in value['cascadingOptions']:
                                st.write(f"      - {child.get('value', 'Unknown')}")
                        else:
                            st.write(f"  - {value.get('value', value.get('name', 'Unknown'))}")
                    else:
                        st.write(f"  - {value}")
        
        # Display custom fields with dependencies
        st.write("### Custom Fields and Dependencies")
        
        # Customer field (customfield_10427) - Cascading Select
        st.write("\n#### Customer (customfield_10427)")
        cust_field = metadata['all_fields'].get('customfield_10427', {})
        if cust_field.get('allowedValues'):
            st.write("Cascading options:")
            for value in cust_field['allowedValues']:
                if isinstance(value, dict):
                    parent_value = value.get('value', 'Unknown')
                    st.write(f"- {parent_value}")
                    if 'cascadingOptions' in value:
                        st.write("  Child options:")
                        for child in value['cascadingOptions']:
                            st.write(f"    - {child.get('value', 'Unknown')}")
        
        # Functional Areas field (customfield_13100) - Cascading Select
        st.write("\n#### Functional Areas (customfield_13100)")
        func_field = metadata['all_fields'].get('customfield_13100', {})
        if func_field.get('allowedValues'):
            st.write("Cascading options:")
            for value in func_field['allowedValues']:
                if isinstance(value, dict):
                    parent_value = value.get('value', 'Unknown')
                    st.write(f"- {parent_value}")
                    if 'cascadingOptions' in value:
                        st.write("  Child options:")
                        for child in value['cascadingOptions']:
                            st.write(f"    - {child.get('value', 'Unknown')}")
        
        # Environment field (customfield_14157)
        st.write("\n#### Environment (customfield_14157)")
        env_field = metadata['all_fields'].get('customfield_14157', {})
        if env_field.get('allowedValues'):
            st.write("Allowed values:")
            for value in env_field['allowedValues']:
                if isinstance(value, dict):
                    st.write(f"  - {value.get('value', value.get('name', 'Unknown'))}")
        
        # Display dependencies
        if any(field.get('dependency') for field in [env_field, func_field, cust_field]):
            st.write("\n### Field Dependencies")
            for field_name, field in [
                ('Environment', env_field),
                ('Functional Areas', func_field),
                ('Customer', cust_field)
            ]:
                if field.get('dependency'):
                    st.write(f"\n{field_name} depends on:")
                    dep = field['dependency']
                    st.write(f"  Field: {dep['field']['name']} ({dep['field']['id']})")
                    if dep.get('values'):
                        st.write("  Value mappings:")
                        for mapping in dep['values']:
                            parent_value = mapping.get('parent', 'Unknown')
                            child_value = mapping.get('value', 'Unknown')
                            st.write(f"    - When parent is '{parent_value}' β†’ '{child_value}'")
        
        # Display other custom fields
        st.write("\n### Other Custom Fields")
        excluded_fields = ['customfield_14157', 'customfield_13100', 'customfield_10427']
        custom_fields = {k: v for k, v in metadata['all_fields'].items() 
                        if k.startswith('customfield_') and k not in excluded_fields}
        
        for field_id, field in custom_fields.items():
            st.write(f"- {field['name']} ({field_id})")
            if field.get('allowedValues'):
                st.write("  Allowed values:")
                for value in field.get('allowedValues', []):
                    if isinstance(value, dict):
                        if 'cascadingOptions' in value:
                            parent_value = value.get('value', 'Unknown')
                            st.write(f"  - {parent_value}")
                            st.write("    Child options:")
                            for child in value['cascadingOptions']:
                                st.write(f"      - {child.get('value', 'Unknown')}")
                        else:
                            st.write(f"  - {value.get('value', value.get('name', 'Unknown'))}")
                    else:
                        st.write(f"  - {value}")

def get_closest_match(target, choices, threshold=60):
    """
    Find the closest matching string from choices using fuzzy matching.
    Returns the best match if similarity is above threshold, otherwise None.
    """
    if not choices:
        return None
        
    try:
        def similarity(a, b):
            # Normalize strings for comparison
            a = a.lower().replace('-', ' ').replace('  ', ' ').strip()
            b = b.lower().replace('-', ' ').replace('  ', ' ').strip()
            return SequenceMatcher(None, a, b).ratio() * 100
        
        # Calculate similarities
        similarities = [(choice, similarity(target, choice)) for choice in choices]
        # Sort by similarity score
        best_match = max(similarities, key=lambda x: x[1])
        
        if best_match[1] >= threshold:
            return best_match[0]
        return None
    except Exception as e:
        st.error(f"Error in fuzzy matching: {str(e)}")
        return None

def get_functional_area_values(metadata):
    """Extract all available functional area values from metadata"""
    logger.info("=== Starting get_functional_area_values ===")
    
    if not metadata:
        logger.error("No metadata provided")
        return []
    
    if 'all_fields' not in metadata:
        logger.error("No 'all_fields' in metadata")
        logger.debug(f"Available metadata keys: {list(metadata.keys())}")
        return []
    
    # Log all available field IDs for debugging
    logger.info("Available fields:")
    for field_id, field in metadata['all_fields'].items():
        field_name = field.get('name', 'Unknown')
        field_type = field.get('schema', {}).get('type', 'Unknown')
        logger.info(f"  {field_name} (ID: {field_id}, Type: {field_type})")
    
    # List of possible field IDs for functional areas
    functional_area_field_ids = [
        'customfield_15303',  # New field ID
        'customfield_13100',  # Old field ID
        'customfield_13101'   # Another possible variation
    ]
    
    # Try to find the functional area field by name or ID
    func_field = None
    for field_id, field in metadata['all_fields'].items():
        field_name = field.get('name', '').lower()
        if field_id in functional_area_field_ids or 'functional area' in field_name:
            func_field = field
            logger.info(f"Found functional area field: {field.get('name')} (ID: {field_id})")
            break
    
    if not func_field:
        logger.error("Could not find functional area field in metadata")
        logger.info("Available field names:")
        for field_id, field in metadata['all_fields'].items():
            logger.info(f"  {field.get('name', 'Unknown')} ({field_id})")
        return []
    
    # Check field type
    field_type = func_field.get('schema', {}).get('type')
    logger.info(f"Functional area field type: {field_type}")
    
    allowed_values = []
    
    if 'allowedValues' in func_field:
        logger.info("Processing allowed values...")
        for parent in func_field['allowedValues']:
            if isinstance(parent, dict):
                parent_value = parent.get('value', 'Unknown')
                logger.info(f"Processing parent value: {parent_value}")
                
                if 'cascadingOptions' in parent:
                    for child in parent['cascadingOptions']:
                        if isinstance(child, dict) and 'value' in child:
                            allowed_values.append(child['value'])
                            logger.debug(f"Added child value: {child['value']}")
                elif 'value' in parent:
                    allowed_values.append(parent['value'])
                    logger.debug(f"Added value: {parent['value']}")
    
    logger.info(f"Found {len(allowed_values)} allowed values")
    if allowed_values:
        logger.info(f"Sample of allowed values: {allowed_values[:5]}")
    else:
        logger.warning("No allowed values found in the field")
    
    return allowed_values

def calculate_story_points(scenario_count):
    """Calculate story points based on number of scenarios"""
    if scenario_count <= 3:
        return 1
    elif scenario_count <= 5:
        return 2
    elif scenario_count <= 9:
        return 3
    elif scenario_count <= 15:
        return 5
    else:
        return 8

def map_functional_area(functional_area, metadata):
    """Map a functional area to its closest Jira allowed parent and child values using structured mapping."""
    if not metadata or not functional_area:
        logger.error("No metadata or functional area provided")
        # Return default values instead of raising an exception
        return "R&I", "Data Exchange"
        
    # Get the functional area field from metadata
    func_field = metadata['all_fields'].get('customfield_13100', {})
    if not func_field or 'allowedValues' not in func_field:
        logger.error("Could not find functional area field in metadata")
        # Return default values instead of raising an exception
        return "R&I", "Data Exchange"
    
    # Define all allowed child values for R&I parent
    allowed_child_values = [
        "Data Exchange", "FIN-Cash Book", "FIN-Creditors", "FIN-Debtors", "FIN-Fixed Assets", 
        "FIN-General Ledger", "FIN-Parameters", "Services-Calendar", "Services-Call Center", 
        "Services-Communications", "Services-Decsion Services", "Services-Entity Relations", 
        "Services-Gamification", "Services-Job Manager", "Services-Measurements", 
        "Services-Multimedia", "Services-Platform", "Services-Questionnaire", "Services-Workflow", 
        "WARPSPEED-Access", "WARPSPEED-Applications", "WARPSPEED-Calendar", 
        "WARPSPEED-Entity Configuration", "WARPSPEED-Environments", "WARPSPEED-Event Types", 
        "WARPSPEED-External Systems", "WARPSPEED-Linked Programmes", "WARPSPEED-Messages", 
        "WARPSPEED-Parameters", "WARPSPEED-Preferences", "WARPSPEED-RefMaster", 
        "WARPSPEED-Relations", "WARPSPEED-Security Tokens", "WARPSPEED-Sequences", 
        "WARPSPEED-Setup - Systems", "WARPSPEED-Statuses", "WARPSPEED-System Index Search", 
        "WARPSPEED-Template Relations", "WARPSPEED-Users", "WARPSPEED-Utilites - My Profile", 
        "WARPSPEED-Utilities - Matrix Headers", "WARPSPEED-Web Objects"
    ]
    
    # Define ILR child values
    ilr_child_values = [
        "Products", "New Business", "Policy Maintenance", "Collections", "Payments", 
        "Claims", "Commission", "Month End", "Integration", "Campaigns", 
        "Claims (Non Legal)", "Claims (Legal) (Including WF)", "Claim Invoicing (Legal)", 
        "Bulk Processing", "Manual Transactions", "Entity Maintenance", "General Queries"
    ]
    
    # Log input functional area
    logger.info(f"\nInput functional area: {functional_area}")
    
    # Check if this is an ILR-related functional area
    ilr_keywords = ["ilr", "legal", "policy", "claim", "invoice", "commission", "collection", "payment"]
    is_ilr_related = any(keyword in functional_area.lower() for keyword in ilr_keywords)
    
    # Also check if the environment is Legalwise, Lifewise, Scorpion, or Talksure
    environment = metadata.get('environment', '')
    is_ilr_environment = any(env in environment.upper() if environment else False 
                            for env in ["LEGAL_WISE_NR", "LIFE_WISE_NR", "SCORPION_NR", "TALKSURE"])
    
    if is_ilr_related or is_ilr_environment or functional_area.startswith("ILR"):
        logger.info(f"Detected ILR-related functional area or environment: {functional_area}, {environment}")
        
        # Try to find the best matching ILR child value
        best_match = None
        best_score = 0
        
        for child_value in ilr_child_values:
            # Calculate similarity score
            score = 0
            child_lower = child_value.lower()
            func_lower = functional_area.lower()
            
            # Check for exact matches or contains
            if child_lower in func_lower or func_lower in child_lower:
                score = 100
            else:
                # Check for partial matches with specific keywords
                for keyword in child_lower.split():
                    if len(keyword) > 3 and keyword in func_lower:  # Only consider meaningful keywords
                        score += 20
            
            if score > best_score:
                best_score = score
                best_match = child_value
        
        # If we found a good match
        if best_match and best_score > 30:
            logger.info(f"Mapped ILR functional area to: {best_match} with score {best_score}")
            return "ILR", best_match
        
        # If no good match found, return None as the child value
        logger.info("No good match found for ILR functional area, using None")
        return "ILR", None
    
    # Enhanced direct mapping for common functional areas with more specific patterns
    direct_mappings = {
        "Financials - Creditors": "FIN-Creditors",
        "Financials - Debtors": "FIN-Debtors",
        "Financials - Cash Book": "FIN-Cash Book",
        "Financials - Fixed Assets": "FIN-Fixed Assets",
        "Financials - FA": "FIN-Fixed Assets",  # Added specific mapping for FA
        "Financials - General Ledger": "FIN-General Ledger",
        "Financials - Parameters": "FIN-Parameters",
        "Services - Calendar": "Services-Calendar",
        "Services - Call Center": "Services-Call Center",
        "Services - Communications": "Services-Communications",
        "Services - Entity Relations": "Services-Entity Relations",
        "Services - Gamification": "Services-Gamification",
        "Services - Job Manager": "Services-Job Manager",
        "Services - Platform": "Services-Platform",
        "Services - Workflow": "Services-Workflow",
        "Data Exchange": "Data Exchange"
    }
    
    # Check for direct matches first
    for pattern, value in direct_mappings.items():
        if pattern in functional_area:
            logger.info(f"Found direct mapping match: {pattern} -> {value}")
            return "R&I", value
    
    # Split the functional area into parts
    parts = [p.strip() for p in functional_area.split(' - ')]
    logger.info(f"Split into parts: {parts}")
    
    # Check first part for category matching
    first_part = parts[0].upper() if parts else ""
    
    # Map based on first part
    if "FINANCIALS" in first_part or "FIN" in first_part:
        # For financial areas, check if we can be more specific based on second part
        if len(parts) > 1:
            second_part = parts[1].upper()
            
            # Enhanced FA/Fixed Assets detection
            if "FA" in second_part or "FIXED" in second_part or "ASSETS" in second_part or "ASSET" in second_part:
                logger.info("Mapping to FIN-Fixed Assets based on FA detection")
                return "R&I", "FIN-Fixed Assets"
            elif "CREDITORS" in second_part or "AP" in second_part or "ACCOUNTS PAYABLE" in second_part:
                logger.info("Mapping to FIN-Creditors based on second part")
                return "R&I", "FIN-Creditors"
            elif "DEBTORS" in second_part or "AR" in second_part or "ACCOUNTS RECEIVABLE" in second_part:
                logger.info("Mapping to FIN-Debtors based on second part")
                return "R&I", "FIN-Debtors"
            elif "CASH" in second_part or "CASHBOOK" in second_part:
                logger.info("Mapping to FIN-Cash Book based on second part")
                return "R&I", "FIN-Cash Book"
            elif "GENERAL" in second_part or "GL" in second_part or "LEDGER" in second_part:
                logger.info("Mapping to FIN-General Ledger based on second part")
                return "R&I", "FIN-General Ledger"
        
        # Default financial mapping
        logger.info("Defaulting to FIN-Parameters")
        return "R&I", "FIN-Parameters"
    
    elif "SERVICE" in first_part or "SERVICES" in first_part:
        # For services areas, check if we can be more specific based on second part
        if len(parts) > 1:
            second_part = parts[1].upper()
            
            # Add specific check for "Work Flow" vs "Workflow"
            if "WORK" in second_part and ("FLOW" in second_part or "FLOWS" in second_part):
                logger.info("Mapping to Services-Workflow based on Work Flow detection")
                return "R&I", "Services-Workflow"
            
            # Continue with existing logic for other service types
            for child_value in allowed_child_values:
                if child_value.startswith("Services-"):
                    # Extract the part after "Services-"
                    service_type = child_value.split("Services-")[1].upper()
                    # Normalize by removing spaces for comparison
                    second_part_normalized = second_part.replace(" ", "")
                    service_type_normalized = service_type.replace(" ", "")
                    
                    if service_type_normalized in second_part_normalized or second_part_normalized in service_type_normalized:
                        logger.info(f"Mapping to {child_value} based on second part")
                        return "R&I", child_value
        
        # Default services mapping
        logger.info("Defaulting to Services-Platform")
        return "R&I", "Services-Platform"
    
    elif "WARPSPEED" in first_part:
        # For warpspeed areas, check if we can be more specific based on second part
        if len(parts) > 1:
            second_part = parts[1].upper()
            for child_value in allowed_child_values:
                if child_value.startswith("WARPSPEED-"):
                    # Extract the part after "WARPSPEED-"
                    warpspeed_type = child_value.split("WARPSPEED-")[1].upper()
                    if warpspeed_type in second_part or second_part in warpspeed_type:
                        logger.info(f"Mapping to {child_value} based on second part")
                        return "R&I", child_value
        
        # Default warpspeed mapping
        logger.info("Defaulting to WARPSPEED-Parameters")
        return "R&I", "WARPSPEED-Parameters"
    
    elif "DATA" in first_part or "EXCHANGE" in first_part:
        logger.info("Mapping to Data Exchange")
    return "R&I", "Data Exchange"
    
    # If no category match, try fuzzy matching with all allowed values
    best_match = None
    best_score = 0
    
    # Try to match the full functional area
    for child_value in allowed_child_values:
        # Calculate similarity score
        score = 0
        child_upper = child_value.upper()
        func_upper = functional_area.upper()
        
        # Check if child value appears in functional area
        if child_upper.replace("-", " ") in func_upper or child_upper in func_upper:
            score = 100  # Perfect match
        else:
            # Calculate partial match score
            for part in parts:
                part_upper = part.upper()
                if part_upper in child_upper or child_upper in part_upper:
                    score += 50 / len(parts)  # Partial match
        
        if score > best_score:
            best_score = score
            best_match = child_value
    
    # If we found a good match
    if best_match and best_score > 30:  # Threshold for accepting a match
        logger.info(f"Found fuzzy match: {best_match} with score {best_score}")
        return "R&I", best_match
    
    # If no good match found, return None as the child value
    logger.warning(f"No good match found for '{functional_area}', using None")
    return "R&I", None

def get_customer_field_values(metadata):
    """Extract all available customer field values and their child options from metadata"""
    if not metadata or 'all_fields' not in metadata:
        return {}
    
    customer_field = metadata['all_fields'].get('customfield_10427', {})
    customer_values = {}
    
    if 'allowedValues' in customer_field:
        for parent in customer_field['allowedValues']:
            if isinstance(parent, dict):
                parent_value = parent.get('value')
                if parent_value:
                    child_values = []
                    if 'cascadingOptions' in parent:
                        child_values = [child.get('value') for child in parent['cascadingOptions'] if child.get('value')]
                    customer_values[parent_value] = child_values
    
    return customer_values

def map_customer_value(environment_value, customer_values):
    """Map environment value to appropriate customer field values"""
    if not environment_value or not customer_values:
        return "MIP Research and Innovation", "R&I General"
    
    # Clean up environment value
    env_value = environment_value.strip()
    logger.info(f"Mapping customer value for environment: {env_value}")
    
    # Handle Legalwise environments
    if 'LEGAL_WISE_NR' in env_value:
        parent_value = "MIP ILR"
        child_value = "LEZA - LegalWise"
        logger.info(f"Mapped Legalwise environment {env_value} to {parent_value} parent with child {child_value}")
        return parent_value, child_value
    
    # Handle Lifewise environments
    if 'LIFE_WISE_NR' in env_value:
        parent_value = "MIP ILR"
        child_value = "LEZA - LifeWise"
        logger.info(f"Mapped Lifewise environment {env_value} to {parent_value} parent with child {child_value}")
        return parent_value, child_value
    
    # Handle Scorpion environments
    if 'SCORPION_NR' in env_value:
        parent_value = "MIP ILR"
        child_value = "LEZA - Scorpion"
        logger.info(f"Mapped Scorpion environment {env_value} to {parent_value} parent with child {child_value}")
        return parent_value, child_value
    
    # Handle Talksure environments
    if 'TALKSURE' in env_value:
        parent_value = "MIP ILR"
        child_value = "Talksure"
        logger.info(f"Mapped Talksure environment {env_value} to {parent_value} parent with child {child_value}")
        return parent_value, child_value
    
    # Special case for 2001 fin
    if '2001' in env_value and 'FIN' in env_value.lower():
        parent_value = "MIP Research and Innovation"
        child_value = "R&I 2001 Fin"
        logger.info(f"Mapped 2001 fin environment {env_value} to {parent_value} parent with child {child_value}")
        return parent_value, child_value
    
    # Handle R&I environments with FIN
    if 'R&I' in env_value and 'FIN' in env_value:
        parent_value = "MIP Research and Innovation"
        # Extract the number between R&I and FIN
        try:
            number = env_value.split('R&I')[1].split('FIN')[0].strip()
            child_value = f"R&I {number} Fin"  # Changed from FIN to Fin
            logger.info(f"Mapped R&I FIN environment {env_value} to {parent_value} parent with child {child_value}")
            return parent_value, child_value
        except:
            logger.warning(f"Could not parse R&I FIN format from {env_value}")
    
    # Handle R&I environments without FIN
    if 'R&I' in env_value and 'FIN' not in env_value:
        parent_value = "MIP Research and Innovation"
        # Extract the number after R&I
        try:
            number = env_value.split('R&I')[1].strip()
            child_value = f"R&I {number}"
            logger.info(f"Mapped R&I environment {env_value} to {parent_value} parent with child {child_value}")
            return parent_value, child_value
        except:
            logger.warning(f"Could not parse R&I format from {env_value}")
    
    # Handle Task Manager environments
    if 'Task Manager' in env_value:
        parent_value = "MIP Research and Innovation"
        child_value = env_value
        logger.info(f"Mapped Task Manager environment {env_value} to {parent_value} parent with child {child_value}")
        return parent_value, child_value
    
    # Handle App Manager environments
    if 'App Manager' in env_value:
        parent_value = "MIP Research and Innovation"
        child_value = env_value
        logger.info(f"Mapped App Manager environment {env_value} to {parent_value} parent with child {child_value}")
        return parent_value, child_value
    
    # Handle Auth Gateway environments
    if 'Auth Gateway' in env_value:
        parent_value = "MIP Research and Innovation"
        child_value = env_value
        logger.info(f"Mapped Auth Gateway environment {env_value} to {parent_value} parent with child {child_value}")
        return parent_value, child_value
    
    # Handle MIP Intranet
    if 'MIP Intranet' in env_value:
        parent_value = "MIP Research and Innovation"
        child_value = "MIP Intranet"
        logger.info(f"Mapped MIP Intranet environment {env_value} to {parent_value} parent with child {child_value}")
        return parent_value, child_value
    
    # Handle Pilotfish
    if 'Pilotfish' in env_value:
        parent_value = "MIP Research and Innovation"
        child_value = "Pilotfish"
        logger.info(f"Mapped Pilotfish environment {env_value} to {parent_value} parent with child {child_value}")
        return parent_value, child_value
    
    # Default case - try to find matching values
    for parent, children in customer_values.items():
        if parent == "MIP Research and Innovation":  # Default parent
            # Look for exact match in child values
            if env_value in children:
                return parent, env_value
            # Look for partial matches
            for child in children:
                if env_value in child or child in env_value:
                    return parent, child
    
    # If no match found, return defaults
    logger.warning(f"No specific mapping found for {env_value}, using defaults")
    return "MIP Research and Innovation", "R&I General"

def create_regression_task(project_key, summary, description, environment, filtered_scenarios_df):
    logger.info("=== Starting create_regression_task function ===")
    logger.info(f"Project: {project_key}, Summary: {summary}, Environment: {environment}")
    
    try:
        # Get metadata first to access field values
        metadata = get_project_metadata(project_key)
        if not metadata:
            error_msg = "Could not get project metadata"
            logger.error(error_msg)
            st.error(error_msg)
            return None
        
        # Add environment to metadata for use in mapping functions
        metadata['environment'] = environment
        
        # Check if environment is ILR-related
        is_ilr_environment = any(env in environment.upper() if environment else False 
                               for env in ["LEGAL_WISE_NR", "LIFE_WISE_NR", "SCORPION_NR", "TALKSURE"])
        logger.info(f"Environment check - Is ILR: {is_ilr_environment}")
        
        # Extract functional area from filtered scenarios
        functional_areas = []
        if "Functional area" in filtered_scenarios_df.columns:
            functional_areas = filtered_scenarios_df["Functional area"].unique().tolist()
            logger.info(f"Extracted functional areas: {functional_areas}")

        # Handle ILR environments
        if is_ilr_environment:
            functional_area_parent = "ILR"
            # Define ILR child values
            ilr_child_values = [
                "Products", "New Business", "Policy Maintenance", "Collections", "Payments", 
                "Claims", "Commission", "Month End", "Integration", "Campaigns", 
                "Claims (Non Legal)", "Claims (Legal) (Including WF)", "Claim Invoicing (Legal)", 
                "Bulk Processing", "Manual Transactions", "Entity Maintenance", "General Queries"
            ]
            
            # Default to General Queries
            functional_area_child = "General Queries"
            
            # Try to find a better match based on the functional area
            if functional_areas:
                func_area = functional_areas[0].lower()
                for child in ilr_child_values:
                    if child.lower() in func_area:
                        functional_area_child = child
                        break
            
            logger.info(f"Forced functional area to ILR parent with child: {functional_area_child}")
        else:
            # Map functional area using metadata (now with environment info)
            logger.info("Not an ILR environment, using standard mapping")
            functional_area_parent, functional_area_child = map_functional_area(
                functional_areas[0] if functional_areas else "Data Exchange",
                metadata
            )
        
        logger.info(f"Final functional area mapping - Parent: {functional_area_parent}, Child: {functional_area_child}")
        
        # Get customer field values and map environment
        customer_values = get_customer_field_values(metadata)
        
        # If functional area is ILR or environment is ILR-related, set customer to MIP ILR
        if functional_area_parent == "ILR" or is_ilr_environment:
            parent_value = "MIP ILR"
            # Set child value based on environment
            if "LEGAL_WISE_NR" in environment.upper():
                child_value = "LEZA - LegalWise"
            elif "LIFE_WISE_NR" in environment.upper():
                child_value = "LEZA - LifeWise"
            elif "SCORPION_NR" in environment.upper():
                child_value = "LEZA - Scorpion"
            elif "TALKSURE" in environment.upper():
                child_value = "Talksure"
            else:
                child_value = "General Queries"
        else:
            # Use the normal mapping for non-ILR functional areas
            parent_value, child_value = map_customer_value(environment, customer_values)
        
        logger.info(f"Mapped customer values - Parent: {parent_value}, Child: {child_value}")
        
        # Get Jira client
        if "jira_client" not in st.session_state:
            error_msg = "No Jira client available. Please connect to Jira first."
            logger.error(error_msg)
            return None
            
        jira_client = st.session_state.jira_client
        logger.info("Got Jira client from session state")
        
        # Get active sprint
        active_sprint = get_current_sprint(get_regression_board(project_key)['id'])
        if not active_sprint:
            error_msg = "No active sprint found"
            logger.error(error_msg)
            return None
            
        logger.info(f"Found active sprint: {active_sprint.name} (ID: {active_sprint.id})")
        
        # Calculate story points based on number of scenarios
        story_points = calculate_story_points(len(filtered_scenarios_df))
        logger.info(f"Calculated story points: {story_points}")
        
        # Prepare issue dictionary with all required fields
        issue_dict = {
            "project": {"key": project_key},
            "summary": summary,
            "description": description,
            "issuetype": {"name": "Story"},
            "components": [{"name": "Maintenance (Regression)"}],
            "customfield_10427": {
                "value": parent_value,
                "child": {
                    "value": child_value
                }
            },
            "customfield_12730": {"value": "Non-Business Critical"},  # Regression Type field
            "customfield_13430": {"value": str(len(filtered_scenarios_df))},  # Number of Scenarios
            "customfield_13100": {
                "value": functional_area_parent,
                "child": {
                    "value": functional_area_child
                }
            },
            "assignee": {"name": st.session_state.jira_username},
            "customfield_10002": story_points  # Story Points field
        }
        
        # Log the complete issue dictionary
        logger.info(f"Issue dictionary prepared: {issue_dict}")
        
        # Create the issue
        logger.info("Attempting to create issue in Jira...")
        
        try:
            # Create the issue with all fields
            new_issue = jira_client.create_issue(fields=issue_dict)
            logger.info(f"Issue created successfully: {new_issue.key}")
            
            # Add issue to sprint
            try:
                logger.info(f"Attempting to add issue {new_issue.key} to sprint {active_sprint.id}...")
                jira_client.add_issues_to_sprint(active_sprint.id, [new_issue.key])
                logger.info(f"Added issue {new_issue.key} to sprint {active_sprint.name}")
            except Exception as sprint_error:
                logger.exception(f"Failed to add issue to sprint: {str(sprint_error)}")
                st.warning(f"⚠️ Could not add task to sprint. Error: {str(sprint_error)}")
            
            # Display success message
           
            return new_issue
            
        except Exception as create_error:
            error_message = str(create_error)
            logger.exception(f"Failed to create issue: {error_message}")
            
            # Try to extract the response content if it's a JIRA Error
            try:
                if hasattr(create_error, 'response'):
                    status_code = getattr(create_error.response, 'status_code', 'N/A')
                    logger.error(f"Response status code: {status_code}")
                    
                    if hasattr(create_error.response, 'text'):
                        response_text = create_error.response.text
                        logger.error(f"Response text: {response_text}")
                        
                        # Display the error to the user
                        st.error(f"❌ Error creating task in Jira (Status: {status_code}):")
                        st.error(response_text)
            except Exception as extract_error:
                logger.exception(f"Error extracting response details: {str(extract_error)}")
            return None
    except Exception as e:
        error_message = f"❌ Unexpected error in create_regression_task: {str(e)}"
        logger.exception(error_message)
        st.error(error_message)
        logger.error(f"Traceback: {''.join(traceback.format_exception(type(e), e, e.__traceback__))}")
        return None

def create_test_data():
    """Create test data for development/testing that matches the filtered scenarios from multiple.py"""
    test_data = {
        'Environment': ['RI2008'] * 7,  # Same environment for all scenarios
        'Functional area': ['Data Exchange - Enquiries - Reports'] * 7,
        'Scenario Name': [
            'Add Missions Error Handling - Existing Code',
            'Add Missions Error Handling - Incorrect Max Iterations',
            'Add Missions Error Handling - No Badges',
            'Add Missions Success - Individual',
            'Add Missions Success - Team',
            'Add Missions Success - Individual Iteration',
            'Add Missions Success - Team Max Iterations'
        ],
        'Error Message': [
            'AssertionError [ERR_ASSERTION]: Error handling for existing code failed',
            'AssertionError [ERR_ASSERTION]: Error handling for max iterations failed',
            'AssertionError [ERR_ASSERTION]: Error handling for missing badges failed',
            'AssertionError [ERR_ASSERTION]: Link validation failed',
            'AssertionError [ERR_ASSERTION]: Link validation failed',
            'AssertionError [ERR_ASSERTION]: Link validation failed',
            'AssertionError [ERR_ASSERTION]: Link validation failed'
        ],
        'Status': ['FAILED'] * 7,
        'Time spent(m:s)': ['02:30'] * 7,  # Example time spent
        'Start datetime': [datetime.now()] * 7  # Current time as example
    }
    
    # Create DataFrame
    df = pd.DataFrame(test_data)
    
    # Add metadata that will be used for Jira task creation
    df.attrs['metadata'] = {
        'Customer': 'MIP Research and Innovation - R&I 2008',
        'Sprint': 'RS Sprint 195',
        'Story Points': 5,
        'Regression Type': 'Non-Business Critical',
        'Component': 'Maintenance (Regression)',
        'Priority': 'Lowest',
        'Type': 'Story',
        'Labels': 'None',
        'Assignee': 'Daniel Akinsola',
        'Reporter': 'Daniel Akinsola'
    }
    
    return df

def process_failures_button(filtered_scenarios_df, environment=None):
    """Process failures and create Jira task"""
    # Use RS project key since we can see it's a Regression Sprint board
    project_key = "RS"
    project_name = "RS - Regression"
    
    # Get environment from DataFrame if not provided
    if environment is None and 'Environment' in filtered_scenarios_df.columns:
        environment = filtered_scenarios_df['Environment'].iloc[0]
    
    # Get unique functional areas from the DataFrame
    functional_areas = filtered_scenarios_df['Functional area'].unique()
    functional_area = functional_areas[0] if len(functional_areas) > 0 else 'R&I'
    
    # Extract the main service category
    service_category = functional_area.split(' - ')[0] + '-' + functional_area.split(' - ')[1]
    service_category = service_category.replace(' ', '')
    
    # Format environment value
    env_number = environment[2:] if environment.startswith('RI') else environment
    env_value = env_number if env_number.startswith('R&I') else f"R&I {env_number}"
    
    # Get the current sprint from the regression board
    board = get_regression_board(project_key)
    sprint = None
    sprint_name = "No Active Sprint"
    if board:
        sprint = get_current_sprint(board['id'])
        if sprint:
            sprint_name = sprint.name
            st.write(f"Found active sprint: {sprint_name}")
        else:
            st.warning("No active sprint found")
    
    # Create metadata dictionary with all required fields
    metadata = {
        'Project Key': project_key,
        'Project': project_name,
        'Issue Type': 'Story',
        'Customer': 'MIP Research and Innovation',
        'Environment': env_value,
        'Functional Areas': service_category,
        'Sprint': sprint_name,
        'Story Points': calculate_story_points(len(filtered_scenarios_df)),
        'Regression Type': 'Non-Business Critical',
        'Number of Scenarios': len(filtered_scenarios_df) if len(filtered_scenarios_df) <= 50 else 50
    }
    
    # Initialize session states if not exists
    if 'task_content' not in st.session_state:
        st.session_state.task_content = None
    if 'task_created' not in st.session_state:
        st.session_state.task_created = False
    if 'created_task' not in st.session_state:
        st.session_state.created_task = None
    if 'show_success' not in st.session_state:
        st.session_state.show_success = False
    if 'last_task_key' not in st.session_state:
        st.session_state.last_task_key = None
    if 'last_task_url' not in st.session_state:
        st.session_state.last_task_url = None
    
    # Store sprint information in session state for task creation
    if sprint:
        st.session_state.current_sprint = sprint
    
    # If we have a recently created task, show the success message first
    if st.session_state.show_success and st.session_state.last_task_key:
        st.success(f"βœ… Task created successfully!")
        
        # Display task link in a more prominent way
        st.markdown(
            f"""
            <div style='padding: 10px; border-radius: 5px; border: 1px solid #90EE90; margin: 10px 0;'>
                <h3 style='margin: 0; color: #90EE90;'>Task Details</h3>
                <p style='margin: 10px 0;'>Task Key: {st.session_state.last_task_key}</p>
                <a href='{st.session_state.last_task_url}' target='_blank' 
                   style='background-color: #90EE90; color: black; padding: 5px 10px; 
                          border-radius: 3px; text-decoration: none; display: inline-block;'>
                    View Task in Jira
                </a>
            </div>
            """,
            unsafe_allow_html=True
        )
        
        # Add a button to create another task
        if st.button("Create Another Task", key="create_another"):
            # Clear all task-related state
            st.session_state.task_content = None
            st.session_state.task_created = False
            st.session_state.created_task = None
            st.session_state.show_success = False
            st.session_state.last_task_key = None
            st.session_state.last_task_url = None
            st.rerun()
        return
    
    # Button to generate content
    if st.button("Generate Task Content"):
        with st.spinner("Generating task content..."):
            summary, description = generate_task_content(filtered_scenarios_df)
            if summary and description:
                st.session_state.task_content = {
                    'summary': summary,
                    'description': description,
                    'environment': environment,
                    'metadata': metadata
                }
            else:
                st.error("Failed to generate task content. Please try again.")
                return
    
    # Display content and create task button if content exists
    if st.session_state.task_content:
        with st.expander("Generated Task Content", expanded=True):
            # Summary section with styling
            st.markdown("### Summary")
            st.markdown(f"""
            <div style='background-color: #f0f2f6; padding: 10px; border-radius: 5px; border: 1px solid #e0e0e0; color: #0f1629;'>
            {st.session_state.task_content['summary']}
            </div>
            """, unsafe_allow_html=True)
            
            # Description section with styling
            st.markdown("### Description")
            st.markdown(f"""
            <div style='background-color: #f0f2f6; padding: 10px; border-radius: 5px; border: 1px solid #e0e0e0; color: #0f1629; white-space: pre-wrap;'>
            {st.session_state.task_content['description']}
            </div>
            """, unsafe_allow_html=True)
            
            # Get and display available functional area values
            display_functional_areas(st.session_state.task_content['metadata'])
            
            # Display metadata with actual field values
            st.markdown("### Fields to be Set")
            metadata = st.session_state.task_content['metadata']
            metadata_html = f"""
            <div style='background-color: #f0f2f6; padding: 10px; border-radius: 5px; border: 1px solid #e0e0e0; color: #0f1629;'>
                <p><strong>Project:</strong> {metadata['Project']}</p>
                <p><strong>Issue Type:</strong> {metadata['Issue Type']}</p>
                <p><strong>Customer:</strong> {metadata['Customer']}</p>
                <p><strong>Environment:</strong> {metadata['Environment']}</p>
                <p><strong>Functional Areas:</strong> {metadata['Functional Areas']}</p>
                <p><strong>Sprint:</strong> {metadata['Sprint']}</p>
                <p><strong>Story Points:</strong> {metadata['Story Points']}</p>
                <p><strong>Regression Type:</strong> {metadata['Regression Type']}</p>
                <p><strong>Number of Scenarios:</strong> {metadata['Number of Scenarios']}</p>
            </div>
            """
            st.markdown(metadata_html, unsafe_allow_html=True)
            
            # Add buttons in columns for better layout
            col1, col2 = st.columns(2)
            with col1:
                if st.button("πŸ”„ Regenerate Content", key="regenerate"):
                    st.session_state.task_content = None
                    st.rerun()
            
            with col2:
                if st.button("πŸ“ Create Jira Task", key="create"):
                    task = create_regression_task(
                        metadata['Project Key'],
                        st.session_state.task_content['summary'],
                        st.session_state.task_content['description'],
                        st.session_state.task_content['environment'],
                        filtered_scenarios_df
                    )
                    
                    if task:
                        # Store task information in session state
                        st.session_state.last_task_key = task.key
                        st.session_state.last_task_url = f"{JIRA_SERVER}/browse/{task.key}"
                        st.session_state.show_success = True
                        # Clear the content
                        st.session_state.task_content = None
                        # Force refresh of sprint stats on next load
                        st.session_state.force_sprint_refresh = True
                        st.rerun()
                    else:
                        st.error("Failed to create task. Please try again.")

def display_functional_areas(metadata):
    """Display functional areas and customer fields in a tree-like structure with styling"""
    if not metadata:
        st.error("No metadata available")
        return
        
    # If this is task metadata (not project metadata), get project metadata first
    if 'all_fields' not in metadata:
        project_metadata = get_project_metadata("RS")  # RS is the fixed project key
        if not project_metadata:
            st.error("Could not fetch project metadata")
            return
        metadata = project_metadata
        
    # Display Functional Areas
    func_field = metadata['all_fields'].get('customfield_13100', {})
    if func_field and 'allowedValues' in func_field:
        st.markdown("### Available Functional Areas")
        
        # Log the raw allowedValues for debugging
        logger.info("=== Raw Functional Area Values ===")
        for value in func_field['allowedValues']:
            logger.info(f"Raw value: {value}")
        
        # Create a dictionary to store parent-child relationships
        parent_child_map = {}
        
        # First pass: collect all parent-child relationships
        for value in func_field['allowedValues']:
            if isinstance(value, dict):
                parent_value = value.get('value', 'Unknown')
                if parent_value:
                    child_values = []
                    logger.info(f"\nProcessing parent: {parent_value}")
                    
                    if 'cascadingOptions' in value:
                        logger.info(f"Found cascading options for {parent_value}:")
                        for child in value['cascadingOptions']:
                            logger.info(f"Raw child value: {child}")
                            if isinstance(child, dict) and child.get('value'):
                                child_value = child.get('value')
                                child_values.append(child_value)
                                logger.info(f"  - Added child: {child_value}")
                    
                    parent_child_map[parent_value] = sorted(child_values) if child_values else []
                    logger.info(f"Final children for {parent_value}: {parent_child_map[parent_value]}")
        
        # Second pass: display the relationships
        for parent_value in sorted(parent_child_map.keys()):
            child_values = parent_child_map[parent_value]
            
            # Create a styled box for each parent and its children
            st.markdown(f"""
            <div style='background-color: #f0f2f6; padding: 10px; border-radius: 5px; border: 1px solid #e0e0e0; color: #0f1629; margin-bottom: 10px;'>
                <strong>{parent_value}</strong>
                {"<ul style='margin-bottom: 0; margin-top: 5px;'>" if child_values else ""}
            """, unsafe_allow_html=True)
            
            # Display child values if they exist
            for child in child_values:
                st.markdown(f"<li>{child}</li>", unsafe_allow_html=True)
            
            if child_values:
                st.markdown("</ul>", unsafe_allow_html=True)
            st.markdown("</div>", unsafe_allow_html=True)
            
            # Log the parent-child relationship for debugging
            logger.info(f"Displaying Parent: {parent_value}")
            if child_values:
                logger.info(f"  With Children: {', '.join(child_values)}")
            else:
                logger.info("  No children found")
    else:
        st.warning("No functional area values found in metadata")
        logger.warning("No functional area values found in metadata")
        if func_field:
            logger.info("Available func_field keys: " + str(list(func_field.keys())))
        
    # Display Customer Field
    cust_field = metadata['all_fields'].get('customfield_10427', {})
    if cust_field and 'allowedValues' in cust_field:
        st.markdown("### Available Customer Values")
        
        # Log the raw allowedValues for debugging
        logger.info("=== Raw Customer Field Values ===")
        for value in cust_field['allowedValues']:
            logger.info(f"Raw value: {value}")
        
        # Create a dictionary to store parent-child relationships for customer field
        customer_parent_child_map = {}
        
        # First pass: collect all parent-child relationships
        for value in cust_field['allowedValues']:
            if isinstance(value, dict):
                parent_value = value.get('value', 'Unknown')
                if parent_value:
                    child_values = []
                    logger.info(f"\nProcessing customer parent: {parent_value}")
                    
                    if 'cascadingOptions' in value:
                        logger.info(f"Found customer cascading options for {parent_value}:")
                        for child in value['cascadingOptions']:
                            logger.info(f"Raw child value: {child}")
                            if isinstance(child, dict) and child.get('value'):
                                child_value = child.get('value')
                                child_values.append(child_value)
                                logger.info(f"  - Added child: {child_value}")
                    
                    customer_parent_child_map[parent_value] = sorted(child_values) if child_values else []
                    logger.info(f"Final customer children for {parent_value}: {customer_parent_child_map[parent_value]}")
        
        # Second pass: display the relationships
        for parent_value in sorted(customer_parent_child_map.keys()):
            child_values = customer_parent_child_map[parent_value]
            
            # Create a styled box for each parent and its children
            st.markdown(f"""
            <div style='background-color: #f0f2f6; padding: 10px; border-radius: 5px; border: 1px solid #e0e0e0; color: #0f1629; margin-bottom: 10px;'>
                <strong>{parent_value}</strong>
                {"<ul style='margin-bottom: 0; margin-top: 5px;'>" if child_values else ""}
            """, unsafe_allow_html=True)
            
            # Display child values if they exist
            for child in child_values:
                st.markdown(f"<li>{child}</li>", unsafe_allow_html=True)
            
            if child_values:
                st.markdown("</ul>", unsafe_allow_html=True)
            st.markdown("</div>", unsafe_allow_html=True)
            
            # Log the parent-child relationship for debugging
            logger.info(f"Displaying Customer Parent: {parent_value}")
            if child_values:
                logger.info(f"  With Children: {', '.join(child_values)}")
            else:
                logger.info("  No children found")
    else:
        st.warning("No customer field values found in metadata")
        logger.warning("No customer field values found in metadata")
        if cust_field:
            logger.info("Available cust_field keys: " + str(list(cust_field.keys())))

def display_story_points_stats(force_refresh=False):
    """Display story points statistics from current sprint"""
    if not st.session_state.jira_client:
        return
        
    # Initialize session state for sprint data if not exists
    if 'sprint_data' not in st.session_state:
        st.session_state.sprint_data = None
    
    # Initialize refresh timestamp if not exists
    if 'last_sprint_refresh' not in st.session_state:
        st.session_state.last_sprint_refresh = None
        
    try:
        # Only fetch data if forced refresh, no data exists, or refresh timestamp is old
        current_time = datetime.now()
        refresh_needed = (
            force_refresh or 
            st.session_state.sprint_data is None or 
            (st.session_state.last_sprint_refresh and 
             (current_time - st.session_state.last_sprint_refresh).total_seconds() > 300)  # 5 minutes cache
        )
        
        if refresh_needed:
            with st.spinner("Fetching sprint data..."):
                # Get regression board
                board = get_regression_board("RS")
                if not board:
                    return
                
                # Get current sprint
                sprint = get_current_sprint(board['id'])
                if not sprint:
                    return
                
                # Get sprint issues
                issues = get_sprint_issues(board['id'], sprint.id, board['estimation_field'])
                if not issues:
                    return
                
                # Calculate points
                issues_data, total_points, completed_points, in_progress_points = calculate_points(issues, board['estimation_field'])
                
                # Store in session state
                st.session_state.sprint_data = {
                    'sprint_name': sprint.name,
                    'total_points': total_points,
                    'completed_points': completed_points,
                    'in_progress_points': in_progress_points,
                    'timestamp': current_time
                }
                st.session_state.last_sprint_refresh = current_time
        
        # Display data from session state
        if st.session_state.sprint_data:
            sprint_data = st.session_state.sprint_data
            
            # Create compact metrics display using custom HTML/CSS
            st.markdown(f"""
                <div style='background-color: #1E1E1E; padding: 10px; border-radius: 5px; margin-bottom: 10px;'>
                    <div style='font-size: 0.8em; color: #E0E0E0; margin-bottom: 8px;'>Current Sprint: {sprint_data['sprint_name']}</div>
                    <div style='display: grid; grid-template-columns: repeat(4, 1fr); gap: 5px; font-size: 0.9em;'>
                        <div style='text-align: center;'>
                            <div style='color: #E0E0E0;'>Total</div>
                            <div style='font-size: 1.2em; font-weight: bold;'>{sprint_data['total_points']:.1f}</div>
                        </div>
                        <div style='text-align: center;'>
                            <div style='color: #E0E0E0;'>Done</div>
                            <div style='font-size: 1.2em; font-weight: bold;'>{sprint_data['completed_points']:.1f}</div>
                        </div>
                        <div style='text-align: center;'>
                            <div style='color: #E0E0E0;'>In Progress</div>
                            <div style='font-size: 1.2em; font-weight: bold;'>{sprint_data['in_progress_points']:.1f}</div>
                        </div>
                        <div style='text-align: center;'>
                            <div style='color: #E0E0E0;'>Complete</div>
                            <div style='font-size: 1.2em; font-weight: bold;'>{(sprint_data['completed_points'] / sprint_data['total_points'] * 100) if sprint_data['total_points'] > 0 else 0:.1f}%</div>
                        </div>
                    </div>
                </div>
            """, unsafe_allow_html=True)
            
            # Show progress bar
            progress = sprint_data['completed_points'] / sprint_data['total_points'] if sprint_data['total_points'] > 0 else 0
            st.progress(progress)
            
            # Add refresh button with key based on timestamp to prevent rerendering
            refresh_key = f"refresh_stats_{datetime.now().strftime('%Y%m%d%H%M%S')}"
            if st.button("πŸ”„ Refresh", key=refresh_key, use_container_width=True):
                # Use a session state flag to trigger refresh on next rerun
                st.session_state.force_sprint_refresh = True
                st.rerun()
                
    except Exception as e:
        st.error(f"Error updating story points: {str(e)}")
        
    # Check if we need to force refresh (from button click)
    if 'force_sprint_refresh' in st.session_state and st.session_state.force_sprint_refresh:
        st.session_state.force_sprint_refresh = False
        return display_story_points_stats(force_refresh=True)

def main():
    st.title("Jira Integration Test")
    
    # Add test data button
    if st.button("Load Test Data"):
        st.session_state.filtered_scenarios_df = create_test_data()
        st.success("Test data loaded!")
    
    is_authenticated = render_jira_login()
    
    if is_authenticated and st.session_state.projects:
        # Fixed project and board selection
        project_key = "RS"
        board_type = "scrum"
        board_name = "Regression Sprints"
        
        # Display fixed selections in a more compact way
        st.markdown("""
        <div style='display: flex; gap: 10px; margin-bottom: 15px; font-size: 0.9em;'>
            <div style='flex: 1;'>
                <div style='color: #E0E0E0; margin-bottom: 4px;'>Project</div>
                <div style='background-color: #262730; padding: 5px 8px; border-radius: 4px; font-size: 0.9em;'>RS - Regression</div>
            </div>
            <div style='flex: 1;'>
                <div style='color: #E0E0E0; margin-bottom: 4px;'>Board</div>
                <div style='background-color: #262730; padding: 5px 8px; border-radius: 4px; font-size: 0.9em;'>Regression Sprints (scrum)</div>
            </div>
        </div>
        """, unsafe_allow_html=True)
        
        # Display sprint stats (only fetch if no data exists)
        display_story_points_stats(force_refresh=False)
        
        # Show test data if loaded
        if 'filtered_scenarios_df' in st.session_state:
            st.subheader("Failed Scenarios")
            st.dataframe(st.session_state.filtered_scenarios_df)
            
            # Get environment directly from the DataFrame
            if 'Environment' in st.session_state.filtered_scenarios_df.columns:
                environment = st.session_state.filtered_scenarios_df['Environment'].iloc[0]
                st.info(f"Using environment from data: {environment}")
                process_failures_button(st.session_state.filtered_scenarios_df, environment)
            else:
                st.error("No environment information found in the data")

        # Add project fields button at the bottom
        if st.button("Show Project Fields"):
            display_project_fields()

if __name__ == "__main__":
    main()