"""Legal pages — 6 markdown-rendered pages (B-2.9).

Each page extends templates/legal/_layout.html and is publicly indexable
(see src/app.py:_PUBLIC_INDEXABLE_PREFIXES = ('marketing.', 'legal.')).
"""
from pathlib import Path

import markdown
from flask import abort, render_template

from src.legal import LEGAL_VERSION, legal_bp

CONTENT_DIR = Path(__file__).parent / 'content'

# All slugs that have a markdown file rendered by this blueprint.
VALID_PAGES = (
    'conditions',
    'confidentialite',
    'cookies',
    'remboursement',
    'accessibilite',
    'mentions',
)

# Display order on the index page (mentions kept in VALID_PAGES but
# rendered last on /legal/ — purely a presentation choice).
DISPLAY_ORDER = (
    'conditions',
    'confidentialite',
    'cookies',
    'remboursement',
    'accessibilite',
    'mentions',
)

PAGE_TITLES = {
    'conditions': "Conditions d'utilisation",
    'confidentialite': "Politique de confidentialité (Loi 25)",
    'cookies': "Politique de cookies",
    'remboursement': "Politique de remboursement",
    'accessibilite': "Déclaration d'accessibilité (WCAG 2.2 AA)",
    'mentions': "Mentions légales",
}

PAGE_DESCRIPTIONS = {
    'conditions': "Conditions d'utilisation du service DictIA — droits, obligations, responsabilités.",
    'confidentialite': "Politique de confidentialité conforme à la Loi 25 du Québec — collecte, conservation, droits des utilisateurs.",
    'cookies': "Utilisation des cookies et traceurs sur les sites DictIA.",
    'remboursement': "Politique de remboursement des abonnements DictIA.",
    'accessibilite': "Engagement DictIA en matière d'accessibilité numérique (WCAG 2.2 AA).",
    'mentions': "Mentions légales — DictIA Inc. (filiale d'InnovA AI S.E.N.C.).",
}

# Inline SVG icons (semantic, brand-colored). Each opens with a 24x24 viewBox.
PAGE_ICONS = {
    'conditions': (
        '<svg xmlns="http://www.w3.org/2000/svg" width="28" height="28" viewBox="0 0 24 24" '
        'fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" '
        'stroke-linejoin="round" aria-hidden="true">'
        '<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/>'
        '<polyline points="14 2 14 8 20 8"/>'
        '<line x1="9" y1="13" x2="15" y2="13"/>'
        '<line x1="9" y1="17" x2="15" y2="17"/>'
        '</svg>'
    ),
    'confidentialite': (
        '<svg xmlns="http://www.w3.org/2000/svg" width="28" height="28" viewBox="0 0 24 24" '
        'fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" '
        'stroke-linejoin="round" aria-hidden="true">'
        '<path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/>'
        '<polyline points="9 12 11 14 15 10"/>'
        '</svg>'
    ),
    'cookies': (
        '<svg xmlns="http://www.w3.org/2000/svg" width="28" height="28" viewBox="0 0 24 24" '
        'fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" '
        'stroke-linejoin="round" aria-hidden="true">'
        '<path d="M12 2a10 10 0 1 0 10 10 4 4 0 0 1-5-5 4 4 0 0 1-5-5"/>'
        '<circle cx="8.5" cy="10.5" r="0.6" fill="currentColor"/>'
        '<circle cx="13" cy="14" r="0.6" fill="currentColor"/>'
        '<circle cx="16" cy="9" r="0.6" fill="currentColor"/>'
        '<circle cx="9" cy="15.5" r="0.6" fill="currentColor"/>'
        '</svg>'
    ),
    'remboursement': (
        '<svg xmlns="http://www.w3.org/2000/svg" width="28" height="28" viewBox="0 0 24 24" '
        'fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" '
        'stroke-linejoin="round" aria-hidden="true">'
        '<path d="M21 12V8a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-1"/>'
        '<path d="M16 12h5"/>'
        '<circle cx="17" cy="12" r="2"/>'
        '<path d="M3 9l4-4 2 2"/>'
        '</svg>'
    ),
    'accessibilite': (
        '<svg xmlns="http://www.w3.org/2000/svg" width="28" height="28" viewBox="0 0 24 24" '
        'fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" '
        'stroke-linejoin="round" aria-hidden="true">'
        '<circle cx="12" cy="12" r="10"/>'
        '<circle cx="12" cy="6.5" r="1.2" fill="currentColor"/>'
        '<path d="M5 9h14"/>'
        '<path d="M12 9v4l-3 6"/>'
        '<path d="M12 13l3 6"/>'
        '</svg>'
    ),
    'mentions': (
        '<svg xmlns="http://www.w3.org/2000/svg" width="28" height="28" viewBox="0 0 24 24" '
        'fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" '
        'stroke-linejoin="round" aria-hidden="true">'
        '<rect x="3" y="4" width="18" height="16" rx="2"/>'
        '<line x1="7" y1="9" x2="17" y2="9"/>'
        '<line x1="7" y1="13" x2="17" y2="13"/>'
        '<line x1="7" y1="17" x2="13" y2="17"/>'
        '</svg>'
    ),
    'agpl': (
        '<svg xmlns="http://www.w3.org/2000/svg" width="28" height="28" viewBox="0 0 24 24" '
        'fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" '
        'stroke-linejoin="round" aria-hidden="true">'
        '<polyline points="16 18 22 12 16 6"/>'
        '<polyline points="8 6 2 12 8 18"/>'
        '<line x1="14" y1="4" x2="10" y2="20"/>'
        '</svg>'
    ),
}

# External links surfaced on the legal index alongside the markdown pages.
EXTERNAL_LINKS = (
    {
        'slug': 'agpl',
        'title': 'Code source AGPL',
        'description': "Code source de DictIA publié sous licence AGPL-3.0 — conformité art. 13 de la licence.",
        'url': 'https://gitea.dictia.ca',
        'external': True,
    },
)


def _render_markdown(page: str) -> str:
    """Read the markdown file for `page` and return rendered HTML."""
    md_path = CONTENT_DIR / f'{page}.md'
    if not md_path.exists():
        abort(404)
    raw = md_path.read_text(encoding='utf-8')
    return markdown.markdown(
        raw,
        extensions=['toc', 'tables', 'fenced_code', 'attr_list'],
        output_format='html5',
    )


def _neighbour(slug: str, offset: int):
    """Return the (slug, title) of the previous/next page in DISPLAY_ORDER."""
    if slug not in DISPLAY_ORDER:
        return None, None
    idx = DISPLAY_ORDER.index(slug) + offset
    if idx < 0 or idx >= len(DISPLAY_ORDER):
        return None, None
    neighbour_slug = DISPLAY_ORDER[idx]
    return neighbour_slug, PAGE_TITLES[neighbour_slug]


@legal_bp.route('/<page>')
def legal_page(page):
    """Render one of the 6 legal pages by slug."""
    if page not in VALID_PAGES:
        abort(404)
    prev_slug, prev_title = _neighbour(page, -1)
    next_slug, next_title = _neighbour(page, +1)
    return render_template(
        'legal/_layout.html',
        title=PAGE_TITLES[page],
        description=PAGE_DESCRIPTIONS[page],
        content=_render_markdown(page),
        page=page,
        legal_version=LEGAL_VERSION,
        prev_page=prev_slug,
        prev_title=prev_title,
        next_page=next_slug,
        next_title=next_title,
    )


@legal_bp.route('/')
def legal_index():
    """Index page listing all internal legal pages plus external links."""
    pages = [
        {
            'slug': slug,
            'title': PAGE_TITLES[slug],
            'description': PAGE_DESCRIPTIONS[slug],
            'icon': PAGE_ICONS.get(slug, ''),
            'external': False,
        }
        for slug in DISPLAY_ORDER
    ] + [
        {
            'slug': link['slug'],
            'title': link['title'],
            'description': link['description'],
            'icon': PAGE_ICONS.get(link['slug'], ''),
            'url': link['url'],
            'external': True,
        }
        for link in EXTERNAL_LINKS
    ]
    return render_template(
        'legal/index.html',
        title="Documents légaux DictIA",
        description="Index des documents légaux DictIA — conditions, confidentialité, cookies, remboursement, accessibilité, mentions, code source AGPL.",
        pages=pages,
        legal_version=LEGAL_VERSION,
    )
