#!/usr/bin/env python3
"""
content_index.py  --  read the content/ source tree (U1) as structured data.

Implements the date half of U3: the hand-maintained ARTICLE_DATES registry in
seo_inject.py is replaced by dates derived from content/ front-matter, so adding
a post via the CMS needs no manual registry edit. Standalone (only os + yaml) so
seo_inject can import it without a cycle.

See docs/content-model.md for the front-matter schema.
"""
import os

import yaml

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
CONTENT = os.path.join(ROOT, "content")
LANGS = ("en", "mk", "si")
_TYPE_DIRS = ("blog", "news", "events")


def _read_front_matter(path):
    with open(path, encoding="utf-8") as f:
        text = f.read()
    if not text.startswith("---\n"):
        return None
    _, front, _body = text.split("---\n", 2)
    return yaml.safe_load(front) or {}


def _canonical_meta(slug_dir):
    """Front-matter for an entry, from en.md if present else any language.
    Dates/source_folder are language-independent, so any file will do."""
    for lang in LANGS:
        p = os.path.join(slug_dir, f"{lang}.md")
        if os.path.exists(p):
            meta = _read_front_matter(p)
            if meta:
                return meta
    return None


def load_dates():
    """Return {"<source_folder>/<slug>": {"published"|"modified": "YYYY-MM-DD"}}
    built from content/ front-matter. Empty dict if content/ is absent."""
    dates = {}
    if not os.path.isdir(CONTENT):
        return dates
    for tdir in _TYPE_DIRS:
        base = os.path.join(CONTENT, tdir)
        if not os.path.isdir(base):
            continue
        for slug in sorted(os.listdir(base)):
            meta = _canonical_meta(os.path.join(base, slug))
            if not meta or not meta.get("date"):
                continue
            key = f"{meta.get('source_folder', tdir)}/{slug}"
            kind = meta.get("date_type", "published")  # published is the default
            dates[key] = {kind: str(meta["date"])}
    return dates


if __name__ == "__main__":
    import json
    print(json.dumps(load_dates(), indent=2, ensure_ascii=False))
