"""Claude Code rule file template (.claude/rules/chowkidar-alerts.md)."""

from __future__ import annotations

from datetime import datetime, timezone


def generate_claude_rules(deprecations: list[dict]) -> str:
    """Generate a rules file for Claude Code."""
    now = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")

    header = """---
description: "Chowkidar — LLM model deprecation alerts (auto-generated)"
globs: ["**/.env*", "**/config.*", "**/settings.*", "**/*.py", "**/*.js", "**/*.ts"]
---

## Model Deprecation Alerts (auto-generated by Chowkidar)

The following models used in this project are deprecated or sunsetting soon.
**When editing any file containing these model strings, update them to the recommended replacements.**

"""

    table = _build_table(deprecations)

    footer = f"""

---
*Last updated: {now} by Chowkidar.*
*Do not edit — regenerated automatically.*
"""

    return header + table + footer


def _build_table(deprecations: list[dict]) -> str:
    lines: list[str] = []
    for dep in deprecations:
        file_short = dep.get("file", "").split("/")[-1]
        replacement = dep.get("replacement") or "check provider docs"
        days = dep.get("days_until", "?")
        sunset = dep.get("sunset_date", "unknown")

        status = ""
        if isinstance(days, int) and days <= 0:
            status = "**SUNSET PASSED**"
        elif isinstance(days, int) and days <= 7:
            status = f"**{days} days left**"
        elif isinstance(days, int):
            status = f"{days} days left"

        lines.append(
            f"- `{dep.get('model', '?')}` in `{file_short}` (`{dep.get('variable', '?')}`): "
            f"Sunset {sunset} ({status}). Replace with `{replacement}`."
        )

    return "\n".join(lines)
