"""Generate developer copy-paste snippets to promote DB patterns into parser source."""

from __future__ import annotations

from app.models.parser_pattern import ParserPattern

DEFAULT_PROMOTE_CONFIDENCE = 0.96


def build_promote_preview(pattern: ParserPattern) -> dict:
    target_file = f"api/app/parsers/{pattern.company_code}.py"
    snippet = f'("{pattern.field_name}", {pattern.regex!r}, {DEFAULT_PROMOTE_CONFIDENCE}),'
    page_note = (
        f"This pattern was trained with page_hint={pattern.page_hint}. "
        "Add to the page-specific pattern list if the insurer uses multiple layouts."
        if pattern.page_hint
        else "Add to the insurer's main PATTERNS list."
    )

    return {
        "pattern_id": pattern.id,
        "company_code": pattern.company_code,
        "field_name": pattern.field_name,
        "regex": pattern.regex,
        "page_hint": pattern.page_hint,
        "target_file": target_file,
        "snippet": snippet,
        "instructions": [
            f"Open `{target_file}` in your editor.",
            page_note,
            "Paste the snippet tuple into the appropriate patterns list.",
            "Run parser tests: `pytest tests/ -q -k {0}`".format(pattern.company_code),
            "After the hardcoded parser passes regression, deactivate this DB pattern in Parser Lab.",
        ],
        "warning": "This does not modify files automatically. Review and paste manually.",
    }
