"""Locked SVG builders for ATO pages, ported 1:1 from the v6.2 design (site/ato-v6/v6-parts.js).
Literal hex fills and per-instance ids (locked badge rules). DFE #1D9BF0, ATO gold #F5B83D, Founding compass rose."""
from markupsafe import Markup

BODY = 'M11.50 1.63Q12.00 1.40 12.50 1.63Q13.00 1.86 13.40 2.42Q13.79 2.98 14.09 3.56Q14.38 4.14 14.68 4.47Q14.98 4.79 15.43 4.78Q15.87 4.76 16.49 4.55Q17.11 4.35 17.79 4.24Q18.46 4.12 18.98 4.31Q19.50 4.50 19.69 5.02Q19.88 5.54 19.76 6.21Q19.65 6.89 19.45 7.51Q19.24 8.13 19.22 8.57Q19.21 9.02 19.53 9.32Q19.86 9.62 20.44 9.91Q21.02 10.21 21.58 10.60Q22.14 11.00 22.37 11.50Q22.60 12.00 22.37 12.50Q22.14 13.00 21.58 13.40Q21.02 13.79 20.44 14.09Q19.86 14.38 19.53 14.68Q19.21 14.98 19.22 15.43Q19.24 15.87 19.45 16.49Q19.65 17.11 19.76 17.79Q19.88 18.46 19.69 18.98Q19.50 19.50 18.98 19.69Q18.46 19.88 17.79 19.76Q17.11 19.65 16.49 19.45Q15.87 19.24 15.43 19.22Q14.98 19.21 14.68 19.53Q14.38 19.86 14.09 20.44Q13.79 21.02 13.40 21.58Q13.00 22.14 12.50 22.37Q12.00 22.60 11.50 22.37Q11.00 22.14 10.60 21.58Q10.21 21.02 9.91 20.44Q9.62 19.86 9.32 19.53Q9.02 19.21 8.57 19.22Q8.13 19.24 7.51 19.45Q6.89 19.65 6.21 19.76Q5.54 19.88 5.02 19.69Q4.50 19.50 4.31 18.98Q4.12 18.46 4.24 17.79Q4.35 17.11 4.55 16.49Q4.76 15.87 4.78 15.43Q4.79 14.98 4.47 14.68Q4.14 14.38 3.56 14.09Q2.98 13.79 2.42 13.40Q1.86 13.00 1.63 12.50Q1.40 12.00 1.63 11.50Q1.86 11.00 2.42 10.60Q2.98 10.21 3.56 9.91Q4.14 9.62 4.47 9.32Q4.79 9.02 4.78 8.57Q4.76 8.13 4.55 7.51Q4.35 6.89 4.24 6.21Q4.12 5.54 4.31 5.02Q4.50 4.50 5.02 4.31Q5.54 4.12 6.21 4.24Q6.89 4.35 7.51 4.55Q8.13 4.76 8.57 4.78Q9.02 4.79 9.32 4.47Q9.62 4.14 9.91 3.56Q10.21 2.98 10.60 2.42Q11.00 1.86 11.50 1.63Z'
CHECK = 'M8.273,14.295 L6.855,12.878 L8.325,11.460 L10.215,13.350 L15.675,8.100 L17.145,9.570 L11.580,15.660 Z'
STAR = 'm12 2.7 2.86 5.8 6.4.93-4.63 4.51 1.09 6.37L12 17.3l-5.72 3.01 1.09-6.37L2.74 9.43l6.4-.93Z'
COMPASS = 'M12.00 1.85 L13.47 8.44 L15.92 8.08 L15.56 10.53 L22.15 12.00 L15.56 13.47 L15.92 15.92 L13.47 15.56 L12.00 22.15 L10.53 15.56 L8.08 15.92 L8.44 13.47 L1.85 12.00 L8.44 10.53 L8.08 8.08 L10.53 8.44 Z M16.15 12.00A4.15 4.15 0 1 1 7.85 12.00A4.15 4.15 0 1 1 16.15 12.00Z'


class Ids:
    """Unique SVG ids per page (reset for every page so output is stable)."""
    def __init__(self):
        self.n = 0

    def __call__(self, p="u"):
        self.n += 1
        return f"{p}{self.n}"


IDS = Ids()


def tick(colour, size=15, cls=""):
    a, b, f = IDS("ta"), IDS("tb"), IDS("tf")
    c = f" {cls}" if cls else ""
    return Markup(
        f'<svg class="ico-rosette{c}" viewBox="0 0 24 24" width="{size}" height="{size}" aria-hidden="true"><defs>'
        f'<linearGradient id="{a}" x1="12" y1="1.5" x2="12" y2="22.5" gradientUnits="userSpaceOnUse"><stop offset="0" stop-color="#fff" stop-opacity=".26"/><stop offset=".45" stop-color="#fff" stop-opacity="0"/><stop offset="1" stop-color="#000" stop-opacity=".18"/></linearGradient>'
        f'<linearGradient id="{b}" x1="12" y1="1.5" x2="12" y2="9" gradientUnits="userSpaceOnUse"><stop offset="0" stop-color="#fff" stop-opacity=".48"/><stop offset="1" stop-color="#fff" stop-opacity="0"/></linearGradient>'
        f'<filter id="{f}" x="-35%" y="-35%" width="170%" height="170%" color-interpolation-filters="sRGB"><feDropShadow dx="0" dy="1" stdDeviation="1" flood-color="#000000" flood-opacity=".3"/></filter></defs>'
        f'<g filter="url(#{f})"><path fill="{colour}" d="{BODY}"/><path fill="url(#{a})" d="{BODY}"/><path fill="url(#{b})" d="{BODY}"/>'
        f'<path fill="none" stroke="#000000" stroke-opacity=".3" stroke-width=".75" d="{BODY}"/><path fill="#ffffff" d="{CHECK}"/></g></svg>')


def tick_dfe(size=15):
    return tick("#1D9BF0", size, "is-dfe")


def tick_ato(size=15):
    return tick("#F5B83D", size)


def compass(size=15):
    a, f = IDS("wa"), IDS("wf")
    return Markup(
        f'<svg class="ico-founding ico-compass" viewBox="0 0 24 24" width="{size}" height="{size}" aria-label="Founding Member" role="img"><title>Founding Member</title><defs>'
        f'<linearGradient id="{a}" x1="6" y1="4" x2="18" y2="18" gradientUnits="userSpaceOnUse"><stop offset="0" stop-color="#fff" stop-opacity=".35"/><stop offset=".55" stop-color="#fff" stop-opacity="0"/><stop offset="1" stop-color="#000" stop-opacity=".18"/></linearGradient>'
        f'<filter id="{f}" x="-40%" y="-40%" width="180%" height="180%" color-interpolation-filters="sRGB"><feDropShadow dx="0" dy="1" stdDeviation=".9" flood-color="#000" flood-opacity=".3"/></filter></defs>'
        f'<g filter="url(#{f})"><path fill="#1D9BF0" d="{COMPASS}"/><path fill="url(#{a})" d="{COMPASS}"/><path fill="none" stroke="#000" stroke-opacity=".22" stroke-width=".55" d="{COMPASS}"/></g></svg>')


def _w(frac):
    return max(0.0, min(1.0, frac)) * 18.52


def glass_star(frac):
    g, r, i, e, c = IDS("gg"), IDS("gr"), IDS("gi"), IDS("ge"), IDS("gc")
    return Markup(
        f'<span class="st"><span class="filter-glass"></span><svg viewBox="2 2 20 20" aria-hidden="true"><defs>'
        f'<linearGradient id="{g}" x1=".15" y1="0" x2=".78" y2="1"><stop stop-color="#FFF1C4" stop-opacity=".94"/><stop offset=".28" stop-color="#FFE09B" stop-opacity=".78"/><stop offset=".52" stop-color="#E4A02D" stop-opacity=".48"/><stop offset=".77" stop-color="#FFE4A2" stop-opacity=".70"/><stop offset="1" stop-color="#C9891B" stop-opacity=".34"/></linearGradient>'
        f'<linearGradient id="{r}" x1=".2" y1="0" x2=".7" y2="1"><stop stop-color="#FFFAE8" stop-opacity=".96"/><stop offset=".35" stop-color="#FFE5AB" stop-opacity=".78"/><stop offset=".65" stop-color="#DEA337" stop-opacity=".5"/><stop offset="1" stop-color="#FFD886" stop-opacity=".72"/></linearGradient>'
        f'<radialGradient id="{i}" cx=".38" cy=".24" r=".65"><stop stop-color="#FFF7DC" stop-opacity=".40"/><stop offset=".55" stop-color="#FFD578" stop-opacity=".12"/><stop offset="1" stop-color="#F5B83D" stop-opacity="0"/></radialGradient>'
        f'<linearGradient id="{e}" x1="0" y1="0" x2="0" y2="1"><stop stop-color="#F3E6FF" stop-opacity=".65"/><stop offset="1" stop-color="#DDC6FF" stop-opacity=".32"/></linearGradient>'
        f'<clipPath id="{c}"><rect x="2.74" y="0" width="{_w(frac):.3f}" height="24"/></clipPath>'
        f'</defs><path d="{STAR}" fill="none" stroke="url(#{e})" stroke-width=".65" stroke-linejoin="round"/>'
        f'<g clip-path="url(#{c})"><path d="{STAR}" fill="#F5B83D"/><path d="{STAR}" fill="url(#{g})" stroke="url(#{r})" stroke-width=".85" stroke-linejoin="round"/>'
        f'<path d="{STAR}" fill="url(#{i})"/>'
        f'<path d="M3.55 9.65 9.46 8.8 12 3.5 14.5 8.65" fill="none" stroke="#FFFBEA" stroke-opacity=".8" stroke-width=".44" stroke-linecap="round" stroke-linejoin="round"/>'
        f'<path d="M7.06 19.7 12 17.06 16.96 19.7" fill="none" stroke="#FFF0C4" stroke-opacity=".35" stroke-width=".36" stroke-linejoin="round"/>'
        f'</g></svg></span>')


def flat_star(frac):
    c = IDS("of")
    return Markup(
        f'<span class="st flat"><svg viewBox="2 2 20 20" aria-hidden="true"><defs><clipPath id="{c}"><rect x="2.74" width="{_w(frac):.3f}" height="24"/></clipPath></defs>'
        f'<path d="{STAR}" fill="rgba(255,255,255,.16)"/><path d="{STAR}" fill="#F5B83D" clip-path="url(#{c})"/></svg></span>')


def empty_star():
    return Markup(f'<span class="st empty"><svg viewBox="2 2 20 20" aria-hidden="true"><path d="{STAR}" fill="none" stroke="currentColor" stroke-width=".85" stroke-linejoin="round" opacity=".45"/></svg></span>')


def pyramid(rating, kind):
    """kind: 'glass' (>= 4.0, shiny), 'flat' (< 4.0), 'empty' (fewer than 3 reviews)."""
    fills = [0.0] * 5
    if kind != "empty":
        full = int(rating)
        for i in range(5):
            fills[i] = 1.0 if i < full else (rating - full if i == full else 0.0)
    s = [empty_star() if kind == "empty" else (flat_star(f) if kind == "flat" else glass_star(f)) for f in fills]
    label = "Rating not shown yet" if kind == "empty" else f"{rating} out of 5 stars"
    return Markup(f'<div class="pyr" role="img" aria-label="{label}"><div class="r">{s[4]}</div><div class="r">{s[2]}{s[3]}</div><div class="r">{s[0]}{s[1]}</div></div>')



def pyramid_line(rating, kind="glass"):
    """One horizontal row of five stars. Same fills as pyramid()."""
    fills = [0.0] * 5
    if kind != "empty":
        full = int(rating)
        frac = float(rating) - full
        for i in range(5):
            fills[i] = 1.0 if i < full else (frac if i == full else 0.0)
    s = [empty_star() if kind == "empty" else (flat_star(f) if kind == "flat" else glass_star(f)) for f in fills]
    label = "Rating not shown yet" if kind == "empty" else f"{rating} out of 5 stars"
    return Markup(f'<div class="pyr pyr-line" role="img" aria-label="{label}">{"".join(str(x) for x in s)}</div>')


def score_line(rating, shown=True):
    """Shared review score: pyramid stars + Clash Display number. Categories keep stars only."""
    try:
        n = float(rating) if rating is not None else 0.0
    except (TypeError, ValueError):
        n, shown = 0.0, False
    kind = "empty" if not shown else ("glass" if n >= 4 else "flat")
    num = f"{n:.1f}" if shown else "–"
    hi = " is-hi" if kind == "glass" else (" is-lo" if kind == "flat" else "")
    return Markup(f'<div class="ob-score{hi}">{pyramid_line(n if shown else 0, kind)}<b class="ob-score-num">{num}</b></div>')


def inline_stars(score):
    h = ""
    for i in range(5):
        fill = max(0.0, min(1.0, score - i)) * 100
        h += (f'<span class="star-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="{STAR}"/></svg>'
              f'<span class="star-crop" style="width:{fill:g}%"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="{STAR}"/></svg></span></span>')
    return Markup(f'<span class="stars" role="img" aria-label="{score} out of 5 stars">{h}</span>')
