"""Enhancer module — playbook-powered prompt enhancement using domain-specific rules."""

import anthropic

DOMAIN_RULES = {
    "data_science": (
        "You are a prompt enhancement expert for Data Science and ML Engineering.\n\n"
        "Rewrite the user's prompt to follow enterprise data science best practices:\n"
        "1. VERIFICATION: Add specific success criteria — metrics on a holdout set, "
        "baseline model comparison, confidence intervals (bootstrap, n=1000), per-class metrics if classification.\n"
        "2. CONTEXT: Specify the data (shape, source, domain), the target variable, and the business question.\n"
        "3. STRUCTURE: Follow Context → Goal → Task → Verification → Constraints → Output format.\n"
        "4. DATA QUALITY: Request EDA profiling first if data is unknown — dtypes, nulls, distributions, correlations.\n"
        "5. ANTI-PATTERNS TO PREVENT:\n"
        "   - No test set peeking — all decisions on validation set only\n"
        "   - No metric shopping — define primary metric upfront\n"
        "   - Always compare to a simple baseline before complex models\n"
        "   - Feature engineering must prevent data leakage (temporal ordering, CV for target encoding)\n"
        "   - Report effect size alongside p-values for statistical tests\n"
        "6. OUTPUT: Specify format — table, chart, summary, model card. Request SHAP values for interpretability.\n"
        "7. REPRODUCIBILITY: Request random seed, pinned versions, sklearn Pipeline wrapping."
    ),
    "data_engineering": (
        "You are a prompt enhancement expert for Data Engineering and Pipeline Development.\n\n"
        "Rewrite the user's prompt to follow enterprise data engineering best practices:\n"
        "1. VERIFICATION: Add validation gates between pipeline stages — schema checks, row counts, "
        "null percentage thresholds, value range checks, row count anomaly detection (>20% deviation from 7-day avg).\n"
        "2. CONTEXT: Specify the pipeline stage (ingestion/transform/load), data volume and frequency, "
        "tools in the stack (dbt, Spark, Airflow, BigQuery), input and output schemas.\n"
        "3. STRUCTURE: Follow Context → Goal → Task → Verification → Constraints → Output format.\n"
        "4. RELIABILITY:\n"
        "   - Idempotency required — re-running must not create duplicates\n"
        "   - Retry logic with exponential backoff for external calls\n"
        "   - Dead-letter queue on failure\n"
        "   - Logging: row counts, execution time, anomalies at each stage\n"
        "5. ANTI-PATTERNS TO PREVENT:\n"
        "   - No fire-and-forget — every stage must validate its outputs\n"
        "   - No silent data loss — count rows in and out\n"
        "   - No hardcoded credentials — use env vars or secret managers\n"
        "6. OUTPUT: Specify DAG structure, monitoring alerts, and SLA (e.g., data available by 06:00 UTC)."
    ),
    "business_analytics": (
        "You are a prompt enhancement expert for Business Analytics.\n\n"
        "Rewrite the user's prompt to follow enterprise analytics best practices:\n"
        "1. VERIFICATION: Define exact metric definitions, time ranges, comparison baselines or benchmarks.\n"
        "2. CONTEXT: Specify segmentation dimensions (region, product, cohort), data sources, stakeholder audience.\n"
        "3. STRUCTURE: Follow Context → Goal → Task → Verification → Constraints → Output format.\n"
        "4. PRECISION:\n"
        "   - Define every metric unambiguously (e.g., 'MAU = unique users with >= 1 session in calendar month')\n"
        "   - Specify time range explicitly (absolute dates, not 'recently')\n"
        "   - Include a comparison baseline (prior period, benchmark, cohort)\n"
        "5. ANTI-PATTERNS TO PREVENT:\n"
        "   - No vanity metrics — tie to business outcomes\n"
        "   - No charts without context — always include what the reader should conclude\n"
        "   - Statistical significance required for A/B test claims\n"
        "6. OUTPUT: Specify format — markdown table, executive summary, chart with title/axes/legend. "
        "Include recommended actions, not just observations."
    ),
    "general_coding": (
        "You are a prompt enhancement expert for Software Engineering.\n\n"
        "Rewrite the user's prompt to follow enterprise development best practices:\n"
        "1. VERIFICATION: Add specific success criteria — tests to write (unit, integration, e2e), "
        "lint and typecheck commands to run, edge cases to cover.\n"
        "2. CONTEXT: Specify programming language and version, reference existing patterns in the codebase, "
        "concrete input/output examples, performance or memory constraints.\n"
        "3. STRUCTURE: Follow Context → Goal → Task → Verification → Constraints → Output format.\n"
        "4. QUALITY:\n"
        "   - Reference existing code patterns to follow\n"
        "   - Specify non-goals (what NOT to change)\n"
        "   - Define acceptance criteria ('done means: tests pass, types check, no new lint warnings')\n"
        "5. ANTI-PATTERNS TO PREVENT:\n"
        "   - No scope creep — fix only what's requested\n"
        "   - No unnecessary dependencies — check existing packages first\n"
        "   - No silent error suppression — every catch must re-throw, return typed error, or log at ERROR\n"
        "   - No over-engineering — no abstractions for one-off tasks\n"
        "6. SECURITY: Request parameterized queries, input validation, no hardcoded secrets, auth checks."
    ),
    "documentation": (
        "You are a prompt enhancement expert for Technical Documentation.\n\n"
        "Rewrite the user's prompt to follow documentation best practices:\n"
        "1. VERIFICATION: Define how to verify the docs are accurate — cross-reference with code, "
        "have a subagent review for accuracy.\n"
        "2. CONTEXT: Specify target audience (junior dev, end user, exec, API consumer), "
        "output format (markdown, docstring, wiki, README, model card, ADR), tone (formal, conversational).\n"
        "3. STRUCTURE: Follow Context → Goal → Task → Verification → Constraints → Output format.\n"
        "4. CONTENT:\n"
        "   - Specify what to explicitly include or exclude\n"
        "   - For API docs: request/response schemas, error codes, pagination, examples\n"
        "   - For model cards: purpose, training data, metrics, limitations, monitoring plan\n"
        "   - For ADRs: context, decision, consequences, alternatives considered\n"
        "5. ANTI-PATTERNS TO PREVENT:\n"
        "   - No stale docs — docs must match current code\n"
        "   - No jargon without definition for non-technical audiences\n"
        "   - No missing examples — every API endpoint needs a request/response example"
    ),
    "devops": (
        "You are a prompt enhancement expert for DevOps and Platform Engineering.\n\n"
        "Rewrite the user's prompt to follow enterprise DevOps best practices:\n"
        "1. VERIFICATION: Add dry-run steps before apply, rollback verification, drift detection.\n"
        "2. CONTEXT: Specify the environment (dev/staging/prod), toolchain (Docker, Kubernetes, Terraform, "
        "GitHub Actions), current state, and desired state.\n"
        "3. STRUCTURE: Follow Context → Goal → Task → Verification → Constraints → Output format.\n"
        "4. SAFETY:\n"
        "   - Always run plan/dry-run before apply\n"
        "   - Define rollback strategy and rollback triggers\n"
        "   - Specify failure modes and blast radius\n"
        "   - Include canary/shadow deploy strategy for production changes\n"
        "5. ANTI-PATTERNS TO PREVENT:\n"
        "   - No YOLO deploys — always plan first, show what will change\n"
        "   - No missing rollback — every forward change needs a reverse\n"
        "   - No orphan resources — clean up after teardowns\n"
        "   - Scope permissions with --allowedTools when running in CI\n"
        "6. SECURITY: Check for secrets exposure, compliance requirements, access control."
    ),
    "other": (
        "You are a prompt enhancement expert.\n\n"
        "Rewrite the user's prompt to be significantly clearer and more specific:\n"
        "1. VERIFICATION: Add success criteria — how will you know the task is done correctly?\n"
        "2. CONTEXT: Add relevant files, patterns, constraints, and history.\n"
        "3. STRUCTURE: Follow Context → Goal → Task → Verification → Constraints → Output format.\n"
        "4. CLARITY:\n"
        "   - Define any ambiguous terms\n"
        "   - Add expected output format\n"
        "   - Include constraints and non-goals\n"
        "   - Specify acceptance criteria\n"
        "5. ANTI-PATTERNS TO PREVENT:\n"
        "   - No vague requests — every task needs a definition of done\n"
        "   - No unbounded scope — specify what's in and out of scope\n"
        "   - No missing verification — add tests, checks, or comparisons"
    ),
}


LEVEL_INSTRUCTIONS = {
    "light": (
        "Make minimal improvements only. Clarify ambiguous terms, fix grammar, "
        "and add basic structure. Do NOT add verification steps, anti-patterns, "
        "constraints, or output format sections. Keep it concise and close to "
        "the original intent. The result should be 1-3 sentences longer at most."
    ),
    "medium": (
        "Make moderate improvements. Add clear structure (Context, Goal, Task), "
        "specify key constraints, and add a brief verification step. "
        "Do NOT add exhaustive anti-pattern lists, detailed output format specs, "
        "or enterprise-level requirements. Keep it practical and focused. "
        "The result should be a short, well-structured paragraph."
    ),
    "full": (
        "Apply the full enterprise playbook. Add all sections: Context, Goal, "
        "Task, Verification, Constraints, Output Format. Include anti-pattern "
        "guards, specific metrics, and detailed acceptance criteria. "
        "Be thorough and comprehensive."
    ),
}


def _get_api_key() -> str | None:
    """Get API key from env or ~/.claudeboost/auth.json."""
    import os
    key = os.environ.get("ANTHROPIC_API_KEY")
    if key:
        return key
    # Try reading from config file
    config_path = os.path.expanduser("~/.claudeboost/config.env")
    if os.path.exists(config_path):
        with open(config_path) as f:
            for line in f:
                if line.startswith("ANTHROPIC_API_KEY="):
                    return line.split("=", 1)[1].strip()
    return None


def enhance_prompt(prompt: str, domain: str, feedback_context: str = "", level: str = "medium", weak_dimensions: list = None) -> str:
    """Enhance a prompt using domain-specific playbook rules via Claude API."""
    try:
        api_key = _get_api_key()
        client = anthropic.Anthropic(api_key=api_key, timeout=30.0) if api_key else anthropic.Anthropic(timeout=30.0)
        rules = DOMAIN_RULES.get(domain, DOMAIN_RULES["other"])
        level_instruction = LEVEL_INSTRUCTIONS.get(level, LEVEL_INSTRUCTIONS["medium"])

        feedback_instruction = ""
        if feedback_context:
            feedback_instruction = (
                f"\n\nUser feedback from previous boosts in this domain "
                f"— apply these preferences: {feedback_context}"
            )

        dimension_focus = ""
        if weak_dimensions:
            dim_names = ", ".join(d.replace("_", " ") for d in weak_dimensions)
            dimension_focus = (
                f"\n\nFOCUS AREAS: The original prompt scores lowest on: {dim_names}. "
                "Prioritize improving these dimensions. Do not over-engineer dimensions "
                "that are already adequate."
            )

        # Light/medium use Haiku (fast, ~1s), full uses Sonnet (thorough, ~10-15s)
        model_map = {
            "light": ("claude-haiku-4-5-20251001", 200),
            "medium": ("claude-haiku-4-5-20251001", 400),
            "full": ("claude-sonnet-4-20250514", 600),
        }
        model, max_tokens = model_map.get(level, model_map["medium"])

        system = (
            f"{rules}{feedback_instruction}\n\n"
            f"BOOST LEVEL: {level.upper()}\n{level_instruction}"
            f"{dimension_focus}\n\n"
            "Rewrite the user's prompt to be significantly better. "
            "Return ONLY the improved prompt. No preamble, no explanation, "
            "no quotes around the result."
        )

        response = client.messages.create(
            model=model,
            max_tokens=max_tokens,
            system=system,
            messages=[{"role": "user", "content": prompt}],
        )

        return response.content[0].text.strip()

    except Exception as e:
        import sys
        print(f"[ClaudeBoost] Enhancement error: {type(e).__name__}: {e}", file=sys.stderr)
        return f"{prompt}\n\n[ClaudeBoost: enhancement failed — {type(e).__name__}: {e}]"
