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9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
"""Bottleneck prediction and recommendation engine for sprint risk analysis (M11).

Real mode: XGBoost prediction + SHAP explanation + rule-based bottleneck detection.
Mock mode: Rule-based heuristics that still produce realistic reports.

Three layers:
  1. ML prediction: risk_level + risk_score from XGBoost
  2. Rule-based detector: specific bottleneck types (always runs)
  3. Recommendation engine: actionable advice per bottleneck
"""

import logging
import random
from datetime import datetime, timezone
from collections import defaultdict

import numpy as np

from .model import get_model, compute_sprint_features
from utils.privacy import assert_no_pii

log = logging.getLogger(__name__)

# Thresholds
_OVERLOAD_TASK_THRESHOLD = 5
_BLOCKED_RATIO_HIGH = 0.20
_SCOPE_RATIO_THRESHOLD = 1.2
_MEMBER_CAPACITY_DEFAULT = 20  # SP per member per sprint


def analyze(
    sprint_id: str,
    sprint_name: str,
    start_date: str,
    end_date: str,
    capacity: float | None,
    tasks: list[dict],
    member_workloads: dict[str, int],
    past_velocities: list[float] | None = None,
) -> dict:
    """Analyze a sprint for bottleneck risks.

    Returns a dict matching SprintAnalysisResponse schema.
    """
    assert_no_pii(
        {
            "sprint_id": sprint_id,
            "sprint_name": sprint_name,
            "tasks": tasks,
            "member_workloads": member_workloads,
        },
        context="optimizer",
    )

    om = get_model()

    # Rule-based bottleneck detection (always runs, regardless of ML)
    bottlenecks = _detect_bottlenecks(tasks, member_workloads, capacity, start_date, end_date)
    recommendations = _generate_recommendations(bottlenecks)

    if om.is_loaded and not om.is_mock:
        return _predict_with_model(
            om, sprint_id, tasks, member_workloads, capacity,
            start_date, end_date, past_velocities,
            bottlenecks, recommendations,
        )

    return _rule_based_analysis(
        om, sprint_id, sprint_name, start_date, end_date,
        capacity, tasks, member_workloads,
        bottlenecks, recommendations,
    )


def analyze_project(sprint_analyses: list[dict]) -> dict:
    """Aggregate multiple sprint analyses into a project-level summary."""
    if not sprint_analyses:
        return {
            "risk_level": "low",
            "risk_score": 0.05,
            "sprint_count": 0,
            "bottleneck_count": 0,
            "top_recommendations": [],
            "model_version": "mock-v0",
        }

    avg_score = sum(s["risk_score"] for s in sprint_analyses) / len(sprint_analyses)
    all_bottlenecks = []
    all_recs = []
    for s in sprint_analyses:
        all_bottlenecks.extend(s.get("bottlenecks", []))
        all_recs.extend(s.get("recommendations", []))

    # Deduplicate recommendations
    seen = set()
    unique_recs = []
    for r in all_recs:
        if r["action"] not in seen:
            seen.add(r["action"])
            unique_recs.append(r)

    return {
        "risk_level": _score_to_level(avg_score),
        "risk_score": round(avg_score, 2),
        "sprint_count": len(sprint_analyses),
        "bottleneck_count": len(all_bottlenecks),
        "top_recommendations": unique_recs[:5],
        "model_version": sprint_analyses[0].get("model_version", "mock-v0"),
    }


# ---------------------------------------------------------------------------
# Real model prediction
# ---------------------------------------------------------------------------

def _predict_with_model(
    om, sprint_id, tasks, member_workloads, capacity,
    start_date, end_date, past_velocities,
    bottlenecks, recommendations,
) -> dict:
    """Run XGBoost + optional SHAP for risk prediction."""
    # 1. Compute feature vector
    features = compute_sprint_features(
        tasks, member_workloads, capacity, start_date, end_date, past_velocities
    )

    # 2. Scale
    features_scaled = om.scaler.transform(features.reshape(1, -1))

    # 3. ML prediction
    probas = om.model.predict_proba(features_scaled)[0]
    predicted_class = int(np.argmax(probas))
    risk_score = float(np.max(probas))

    # Decode risk level
    if om.label_encoder is not None:
        risk_level = om.label_encoder.inverse_transform([predicted_class])[0]
    else:
        risk_levels = ["critical", "high", "low", "medium"]  # alphabetical default
        risk_level = risk_levels[predicted_class] if predicted_class < len(risk_levels) else "medium"

    # 4. SHAP explanation (top 3 contributing features)
    shap_explanation = None
    if om.explainer is not None:
        try:
            shap_values = om.explainer.shap_values(features_scaled)
            # For multiclass, get SHAP for the predicted class
            if isinstance(shap_values, list):
                class_shap = shap_values[predicted_class][0]
            else:
                class_shap = shap_values[0]

            # Top 3 features by absolute SHAP value
            feature_names = om.feature_names
            abs_shap = np.abs(class_shap)
            top_indices = np.argsort(abs_shap)[::-1][:3]
            shap_explanation = [
                {
                    "feature": feature_names[idx],
                    "value": float(features[idx]),
                    "impact": round(float(class_shap[idx]), 4),
                }
                for idx in top_indices
            ]
        except Exception as exc:
            log.warning("SHAP explanation failed: %s", exc)

    # Build response
    result = {
        "sprint_id": sprint_id,
        "risk_level": risk_level,
        "risk_score": round(risk_score, 2),
        "bottlenecks": bottlenecks,
        "recommendations": recommendations,
        "model_version": om.version,
    }

    if shap_explanation:
        # Attach SHAP info to bottlenecks as context
        for bn in result["bottlenecks"]:
            bn["_shap_context"] = shap_explanation

    return result


# ---------------------------------------------------------------------------
# Rule-based / mock analysis
# ---------------------------------------------------------------------------

def _rule_based_analysis(
    om, sprint_id, sprint_name, start_date, end_date,
    capacity, tasks, member_workloads,
    bottlenecks, recommendations,
) -> dict:
    """Fall back to rule-based risk scoring when ML model is unavailable."""
    log.info("Rule-based analysis for sprint %s (%s)", sprint_id, sprint_name)

    # Compute risk score from bottleneck severities
    severity_weights = {"low": 0.1, "medium": 0.25, "high": 0.5, "critical": 0.8}
    if bottlenecks:
        total_severity = sum(severity_weights.get(b["severity"], 0.1) for b in bottlenecks)
        risk_score = min(1.0, total_severity / max(len(bottlenecks), 1))
    else:
        risk_score = round(random.uniform(0.05, 0.15), 2)

    # Timeline pressure boost
    try:
        now = datetime.now(timezone.utc)
        end = datetime.fromisoformat(end_date.replace("Z", "+00:00"))
        start = datetime.fromisoformat(start_date.replace("Z", "+00:00"))
        total_days = (end - start).days or 1
        remaining = max(0, (end - now).days)
        elapsed_ratio = 1.0 - (remaining / total_days)

        incomplete = sum(1 for t in tasks if t.get("status") not in ("done",))
        total = len(tasks) or 1
        if elapsed_ratio > 0.7 and (incomplete / total) > 0.5:
            risk_score = min(1.0, risk_score + 0.15)
    except (ValueError, TypeError):
        pass

    risk_score = round(risk_score, 2)

    return {
        "sprint_id": sprint_id,
        "risk_level": _score_to_level(risk_score),
        "risk_score": risk_score,
        "bottlenecks": bottlenecks,
        "recommendations": recommendations,
        "model_version": om.version,
    }


# ---------------------------------------------------------------------------
# Bottleneck detection (rule-based — always runs)
# ---------------------------------------------------------------------------

def _detect_bottlenecks(
    tasks: list[dict],
    member_workloads: dict[str, int],
    capacity: float | None,
    start_date: str | None = None,
    end_date: str | None = None,
) -> list[dict]:
    """Identify specific bottleneck types in a sprint.

    These deterministic rules always fire regardless of ML prediction.
    """
    bottlenecks: list[dict] = []
    if not tasks:
        return bottlenecks

    total_tasks = len(tasks)

    # 1. Member overload
    member_tasks: dict[str, list[str]] = defaultdict(list)
    for t in tasks:
        aid = t.get("assignee_id")
        if aid and t.get("status") not in ("done",):
            member_tasks[aid].append(t.get("task_id", t.get("id", "")))

    overloaded_count = 0
    for uid, task_ids in member_tasks.items():
        if len(task_ids) > _OVERLOAD_TASK_THRESHOLD:
            overloaded_count += 1
            severity = "critical" if len(task_ids) > _OVERLOAD_TASK_THRESHOLD + 3 else "high" if len(task_ids) > _OVERLOAD_TASK_THRESHOLD + 1 else "medium"
            bottlenecks.append({
                "type": "member_overload",
                "description": f"Member {uid[:8]} has {len(task_ids)} active tasks (threshold: {_OVERLOAD_TASK_THRESHOLD})",
                "affected_task_ids": task_ids,
                "severity": severity,
            })

    # 2. Blocked tasks
    blocked = [t for t in tasks if t.get("status") == "blocked"]
    if len(blocked) > 2:
        blocked_ratio = len(blocked) / total_tasks
        severity = "critical" if len(blocked) > 5 else "high" if len(blocked) > 3 else "medium"
        bottlenecks.append({
            "type": "blocked_tasks",
            "description": f"{len(blocked)} of {total_tasks} tasks are blocked ({blocked_ratio:.0%})",
            "affected_task_ids": [t.get("task_id", t.get("id", "")) for t in blocked],
            "severity": severity,
        })

    # 3. Unassigned critical tasks
    unassigned_critical = [
        t for t in tasks
        if not t.get("assignee_id")
        and t.get("status") != "done"
        and t.get("priority") in ("critical", "urgent", "high")
    ]
    if unassigned_critical:
        bottlenecks.append({
            "type": "unassigned_critical",
            "description": f"{len(unassigned_critical)} high-priority task{'s' if len(unassigned_critical) > 1 else ''} unassigned",
            "affected_task_ids": [t.get("task_id", t.get("id", "")) for t in unassigned_critical],
            "severity": "high",
        })

    # 4. Velocity lag (deadline risk check)
    try:
        if end_date and start_date:
            now = datetime.now(timezone.utc)
            end_dt = datetime.fromisoformat(end_date.replace("Z", "+00:00"))
            start_dt = datetime.fromisoformat(start_date.replace("Z", "+00:00"))
            sprint_length = max(1, (end_dt - start_dt).days)
            days_remaining = max(0, (end_dt - now).days)
            elapsed_frac = (sprint_length - days_remaining) / sprint_length

            done_count = sum(1 for t in tasks if t.get("status") == "done")
            completion_rate = done_count / total_tasks
            expected = max(elapsed_frac, 0.01)
            velocity_vs_plan = completion_rate / expected

            if velocity_vs_plan < 0.6:
                pct_behind = int((1 - velocity_vs_plan) * 100)
                bottlenecks.append({
                    "type": "velocity_lag",
                    "description": f"Sprint velocity is {pct_behind}% behind planned pace",
                    "affected_task_ids": [],
                    "severity": "high" if velocity_vs_plan < 0.4 else "medium",
                })

            # 5. Deadline risk
            if days_remaining < 2 and completion_rate < 0.7:
                remaining_pct = int((1 - completion_rate) * 100)
                bottlenecks.append({
                    "type": "deadline_risk",
                    "description": f"Sprint ends in {days_remaining} day{'s' if days_remaining != 1 else ''} with {remaining_pct}% work remaining",
                    "affected_task_ids": [],
                    "severity": "critical",
                })
    except (ValueError, TypeError):
        pass

    # 6. Scope creep
    if capacity and capacity > 0:
        total_sp = sum(t.get("story_points") or 0 for t in tasks)
        if total_sp > capacity * _SCOPE_RATIO_THRESHOLD:
            overcommit = int(((total_sp / capacity) - 1) * 100)
            bottlenecks.append({
                "type": "scope_creep",
                "description": f"Sprint is {overcommit}% over capacity ({total_sp} SP vs {capacity} capacity)",
                "affected_task_ids": [],
                "severity": "high" if total_sp > capacity * 1.4 else "medium",
            })

    # 7. Workload imbalance
    workloads = list(member_workloads.values()) if member_workloads else []
    if workloads and len(workloads) > 1:
        mean_wl = np.mean(workloads)
        if mean_wl > 0:
            std_ratio = float(np.std(workloads) / mean_wl)
            if std_ratio > 0.5:
                bottlenecks.append({
                    "type": "workload_imbalance",
                    "description": "Significant workload imbalance across team members",
                    "affected_task_ids": [],
                    "severity": "medium",
                })

    # 8. Overdue tasks
    now = datetime.now(timezone.utc)
    overdue = []
    for t in tasks:
        if t.get("status") == "done":
            continue
        due = t.get("due_date")
        if due:
            try:
                due_dt = datetime.fromisoformat(due.replace("Z", "+00:00"))
                if due_dt < now:
                    overdue.append(t)
            except (ValueError, TypeError):
                pass
    if len(overdue) > 3:
        bottlenecks.append({
            "type": "overdue_tasks",
            "description": f"{len(overdue)} tasks are past their due date",
            "affected_task_ids": [t.get("task_id", t.get("id", "")) for t in overdue],
            "severity": "high" if len(overdue) > 5 else "medium",
        })

    # 9. Unassigned tasks (general)
    unassigned_all = [t for t in tasks if not t.get("assignee_id") and t.get("status") != "done"]
    if len(unassigned_all) > 3:
        bottlenecks.append({
            "type": "unassigned_tasks",
            "description": f"{len(unassigned_all)} tasks have no assignee",
            "affected_task_ids": [t.get("task_id", t.get("id", "")) for t in unassigned_all],
            "severity": "medium" if len(unassigned_all) <= 5 else "high",
        })

    return bottlenecks


# ---------------------------------------------------------------------------
# Recommendation engine
# ---------------------------------------------------------------------------

def _generate_recommendations(bottlenecks: list[dict]) -> list[dict]:
    """Map bottleneck types to actionable recommendations."""
    recommendations: list[dict] = []

    for bn in bottlenecks:
        bn_type = bn["type"]
        severity = bn["severity"]

        if bn_type == "member_overload":
            recommendations.append({
                "action": "Redistribute tasks from overloaded members to team members with available capacity",
                "reason": bn["description"],
                "priority": "high",
            })

        elif bn_type == "blocked_tasks":
            recommendations.append({
                "action": "Schedule a blocker resolution meeting to unblock stuck tasks",
                "reason": bn["description"],
                "priority": "high" if severity in ("high", "critical") else "medium",
            })

        elif bn_type == "unassigned_critical":
            recommendations.append({
                "action": "Immediately assign all unassigned high-priority tasks",
                "reason": bn["description"],
                "priority": "high",
            })

        elif bn_type == "velocity_lag":
            recommendations.append({
                "action": "Consider reducing sprint scope — move low-priority tasks to backlog",
                "reason": bn["description"],
                "priority": "high" if severity == "high" else "medium",
            })

        elif bn_type == "deadline_risk":
            recommendations.append({
                "action": "Sprint at risk — escalate to project lead and prioritize must-have items",
                "reason": bn["description"],
                "priority": "high",
            })

        elif bn_type == "scope_creep":
            recommendations.append({
                "action": "Move lowest-priority tasks to backlog to bring sprint within capacity",
                "reason": bn["description"],
                "priority": "medium",
            })

        elif bn_type == "workload_imbalance":
            recommendations.append({
                "action": "Rebalance task assignments to distribute workload more evenly",
                "reason": bn["description"],
                "priority": "medium",
            })

        elif bn_type == "overdue_tasks":
            recommendations.append({
                "action": "Review overdue tasks — update due dates or reassign to unblock progress",
                "reason": bn["description"],
                "priority": "high" if severity == "high" else "medium",
            })

        elif bn_type == "unassigned_tasks":
            recommendations.append({
                "action": "Use AI smart assignment to assign unowned tasks to available team members",
                "reason": bn["description"],
                "priority": "medium",
            })

    # Deduplicate
    seen = set()
    unique = []
    for r in recommendations:
        if r["action"] not in seen:
            seen.add(r["action"])
            unique.append(r)

    # Sort by priority
    priority_order = {"high": 0, "medium": 1, "low": 2}
    unique.sort(key=lambda r: priority_order.get(r["priority"], 2))

    return unique


def _score_to_level(score: float) -> str:
    """Convert numeric risk score to risk level string."""
    if score >= 0.75:
        return "critical"
    if score >= 0.50:
        return "high"
    if score >= 0.25:
        return "medium"
    return "low"