"""Catalog pricing for the public web design cost estimator.

Draft catalog pricing — not a formal quote. Third-party usage
(Stripe fees, SMS, postage) is billed separately.
"""

# Feature ids used by the interactive UI and WebMCP estimate_web_design_cost tool.
WEB_DESIGN_FEATURES = (
    {
        "id": "public_site",
        "name": "Public site",
        "description": "Landing, about, contact (+ your service pages).",
        "build": 300,
        "monthly": 10,
        "required": True,
        "requires": (),
    },
    {
        "id": "client_portal",
        "name": "Client portal + UTM",
        "description": "Login, dashboard, leads, UTM analytics — every client.",
        "build": 300,
        "monthly": 30,
        "required": True,
        "requires": (),
    },
    {
        "id": "email_sms",
        "name": "Email & SMS",
        "description": "Campaigns, mailing list, engagement reports.",
        "build": 500,
        "monthly": 10,
        "required": False,
        "requires": (),
    },
    {
        "id": "direct_mail",
        "name": "Direct mail",
        "description": "Postcard designer + print/send. Postage separate.",
        "build": 500,
        "monthly": 5,
        "required": False,
        "requires": (),
    },
    {
        "id": "blog",
        "name": "Blog",
        "description": "Public blog + portal post management.",
        "build": 500,
        "monthly": 10,
        "required": False,
        "requires": (),
    },
    {
        "id": "payments",
        "name": "Payments (Stripe)",
        "description": "Invoices + pay links. Requires Email & SMS.",
        "build": 500,
        "monthly": 20,
        "required": False,
        "requires": ("email_sms",),
        "requires_note": "Requires Email & SMS — auto-selected.",
    },
    {
        "id": "social",
        "name": "Social consolidation",
        "description": "Accounts, composer, scheduling.",
        "build": 500,
        "monthly": 20,
        "required": False,
        "requires": (),
    },
    {
        "id": "ai_social",
        "name": "AI social generator",
        "description": "Ollama drafts. Requires Social consolidation.",
        "build": 300,
        "monthly": 20,
        "required": False,
        "requires": ("social",),
        "requires_note": "Requires Social consolidation — auto-selected.",
    },
)

WEB_DESIGN_INCLUDED = (
    {
        "title": "Tailored to your brand",
        "description": "Custom design suited to your business — layout, visuals, and flows built around how your customers actually use the site.",
    },
    {
        "title": "You own the site & data",
        "description": "The site and your data belong to you. Leave anytime and take everything with you — no lock-in.",
    },
    {
        "title": "Three-instance hosting",
        "description": "Every site runs on at least three instances for higher reliability and scale.",
    },
    {
        "title": "UTM tracking & lead capture",
        "description": "UTM tracking and lead capture/analysis ship with every client portal.",
    },
    {
        "title": "SEO, accessibility & LLM-ready",
        "description": "Optimized SEO, accessibility, performance, and LLM integration on every build.",
    },
    {
        "title": "Grafana metrics & alerts",
        "description": "Live dashboards with Grafana metrics and proactive alerts on uptime and performance.",
    },
)

WEB_DESIGN_PRICING_DISCLAIMER = (
    "Draft catalog pricing. Not a formal quote. "
    "Third-party usage (Stripe fees, SMS, postage) not included."
)


def get_feature_by_id(feature_id):
    for feature in WEB_DESIGN_FEATURES:
        if feature["id"] == feature_id:
            return feature
    return None


def resolve_selected_features(selected_ids):
    """Expand required bases + dependency chains; return ordered unique ids."""
    catalog = {f["id"]: f for f in WEB_DESIGN_FEATURES}
    selected = set(selected_ids or [])

    for feature in WEB_DESIGN_FEATURES:
        if feature["required"]:
            selected.add(feature["id"])

    changed = True
    while changed:
        changed = False
        for feature_id in list(selected):
            feature = catalog.get(feature_id)
            if not feature:
                continue
            for dep in feature.get("requires", ()):
                if dep not in selected:
                    selected.add(dep)
                    changed = True

    return [f["id"] for f in WEB_DESIGN_FEATURES if f["id"] in selected]


def estimate_web_design_cost(selected_ids=None):
    """Return build/monthly totals for a feature selection."""
    resolved = resolve_selected_features(selected_ids)
    catalog = {f["id"]: f for f in WEB_DESIGN_FEATURES}
    features = [catalog[fid] for fid in resolved]

    return {
        "selected": [
            {
                "id": f["id"],
                "name": f["name"],
                "build": f["build"],
                "monthly": f["monthly"],
            }
            for f in features
        ],
        "selected_count": len(features),
        "one_time_build": sum(f["build"] for f in features),
        "monthly": sum(f["monthly"] for f in features),
        "disclaimer": WEB_DESIGN_PRICING_DISCLAIMER,
        "included_with_every_site": [
            {"title": item["title"], "description": item["description"]}
            for item in WEB_DESIGN_INCLUDED
        ],
    }


def features_for_json():
    """JSON-serializable feature list for templates and WebMCP config."""
    return [
        {
            "id": f["id"],
            "name": f["name"],
            "description": f["description"],
            "build": f["build"],
            "monthly": f["monthly"],
            "required": f["required"],
            "requires": list(f.get("requires", ())),
            "requires_note": f.get("requires_note", ""),
        }
        for f in WEB_DESIGN_FEATURES
    ]
