"""The shipped roadmap templates.

Loaded append-only at startup: a template is created only if its key is absent,
so this never fights a library that has since been edited. Editing a template
can never reach a live project — instantiation deep-copies (§5.5).

Tool slots carry `default_tool_slug` as a SUGGESTION only. Until caddie M2
brings the tool router, those stages render as ordinary prose stages — which is
exactly the M1 gate's intent.
"""

import logging

from app.db import get_session_factory
from app.models import ProjectTemplate, ProjectTemplateStage, StageTemplate

log = logging.getLogger(__name__)

STAGE_TEMPLATES = [
    dict(key="onboarding", title="Onboarding",
         body_md=("Everything we need from you to get started — access, "
                  "content, and a few decisions. We'll work through it "
                  "together; nothing here is urgent on its own."),
         default_tool_slug="punchlist", client_visible=True),
    dict(key="design_direction", title="Design direction",
         body_md=("We gather a look and feel we're both happy with before "
                  "anything gets built."),
         default_tool_slug="scout", client_visible=True),
    dict(key="mockups", title="Mockups",
         body_md=("Designs for the key pages. Have a look and let us know — "
                  "your approval here is what starts the build."),
         requires_approval=True, client_visible=True),
    dict(key="build", title="Build",
         body_md="We build the site out on our staging server.",
         client_visible=True),
    dict(key="staging", title="Staging",
         body_md=("The site on a private URL, for you to click around before "
                  "it goes live."),
         client_visible=True),
    dict(key="reviewing", title="Reviewing",
         body_md=("Feedback and revisions. This one usually stays open a while "
                  "— often past launch, which is normal."),
         client_visible=True),
    dict(key="launch", title="Launch",
         body_md="We point the domain at the new site and watch it settle.",
         requires_approval=True, client_visible=True),
]

PROJECT_TEMPLATES = [
    dict(key="website_build_v1", title="Website build v1",
         description=("The usual shape of a website project. It is a PLAN, not "
                      "a track — stages run concurrently and close "
                      "independently."),
         stages=["onboarding", "design_direction", "mockups", "build",
                 "staging", "reviewing", "launch"]),
]


def load_templates() -> None:
    with get_session_factory()() as session:
        for spec in STAGE_TEMPLATES:
            if session.get(StageTemplate, spec["key"]):
                continue
            session.add(StageTemplate(**spec))
        session.flush()

        for spec in PROJECT_TEMPLATES:
            if session.get(ProjectTemplate, spec["key"]):
                continue
            session.add(ProjectTemplate(key=spec["key"], title=spec["title"],
                                        description=spec["description"]))
            for i, sk in enumerate(spec["stages"]):
                session.add(ProjectTemplateStage(
                    template_key=spec["key"], stage_template_key=sk, position=i))
            log.info("seeded project template %s", spec["key"])
        session.commit()
