"""The design brief: what this piece of content should do to the eye, written by the model before any layout is
chosen. Everything after follows from it: the composition, the looks per block, the words' weight. The owner's
taste, read from what they kept before, is part of what it is written from. The model call is a function the
product passes in: (system, user) -> dict."""
from __future__ import annotations

from typing import Any, Callable, Dict

Ask = Callable[[str, str], Dict[str, Any]]

SYSTEM = """You are the art director for one piece of content. Before any layout is chosen, write the design brief:
what the reader's eye should land on first, the one job of the piece, its mood, how dense it should be, what role a
picture plays if any, where the brand's accent colour belongs, and the hierarchy of the parts in order. Decide
from the brief, the owner's words, what is known about the brand (its colours, fonts, the look of its site), the
audience, the facts in hand, and what this owner kept before. Design in the brand's own look: a quiet serif brand
and a bold product brand should not get the same email. Say nothing about words; say how it should feel and read.
Answer as JSON: {"first_seen": "...", "job": "...", "mood": "...", "density": "airy|regular|compact",
"picture": "none|supporting|leading", "accent": "where it lands, in a few words", "hierarchy": ["...", "..."],
"notes": "two sentences at most for whoever lays it out"}."""

FIELDS = ("first_seen", "job", "mood", "density", "picture", "accent", "hierarchy", "notes")


def design_brief(brief: str, ask: Ask, owner_words: str = "", known: str = "", brand_look: str = "", taste: str = "") -> Dict[str, Any]:
    user = (f"THE BRIEF\n{brief.strip()}\n\nTHE OWNER'S WORDS ABOUT THE DESIGN\n{owner_words.strip() or 'nothing'}\n\n"
            f"THE BRAND'S LOOK\n{brand_look.strip() or 'nothing measured'}\n\nWHAT IS KNOWN\n{known.strip() or 'nothing beyond the brief'}\n\n"
            f"WHAT THIS OWNER KEPT BEFORE\n{taste.strip() or 'nothing yet'}")
    answer = ask(SYSTEM, user) or {}
    return {k: answer.get(k) for k in FIELDS if answer.get(k) not in (None, "", [])}


def as_lines(design: Dict[str, Any]) -> str:
    """The brief as lines for the next step's prompt."""
    if not design:
        return "none"
    out = []
    for k in FIELDS:
        v = design.get(k)
        if v in (None, "", []):
            continue
        out.append(f"- {k.replace('_', ' ')}: {', '.join(map(str, v)) if isinstance(v, list) else v}")
    return "\n".join(out)
