"""Snapshot the current product-first pipeline into a ComplianceBaseline.

This is the ONLY place RCI runs the pipeline — to freeze a point-in-time map +
registry-linked obligations + their required evidence. Everything downstream
(delta computation) works purely against this snapshot, never re-evaluating.
"""

from __future__ import annotations

from typing import Dict, List, Optional

from compliance.profile.canonical import CanonicalProductRegulatoryProfile
from compliance.profile.to_reasoning import to_reasoning_profile
from compliance.reasoning.obligation_engine import derive_obligations
from compliance.regulatory_map.renderer import render_regulatory_map

from .schemas import ComplianceBaseline


def create_baseline(
    profile: CanonicalProductRegulatoryProfile,
    evidence_refs: Optional[Dict[str, List[str]]] = None,
    baseline_id: str = "baseline",
    created_at: Optional[str] = None,
) -> ComplianceBaseline:
    reg_map = render_regulatory_map(profile)
    obligations = derive_obligations(to_reasoning_profile(profile)).applicable_obligations

    applicable: List[str] = []
    required: Dict[str, List[str]] = {}
    for ob in obligations:
        if ob.registry_anchor:  # only registry-linked obligations enter the baseline
            applicable.append(ob.obligation_id)
            required[ob.obligation_id] = list(ob.required_evidence)

    return ComplianceBaseline(
        baseline_id=baseline_id,
        product_profile_snapshot=profile,
        regulatory_map_snapshot=reg_map,
        applicable_obligations=applicable,
        obligation_evidence_required=required,
        evidence_refs=dict(evidence_refs or {}),
        created_at=created_at,
    )
