#!/usr/bin/env python3
"""Convert src/content/{posts,pages}.json → src/content/{posts,pages}/<slug>.md
for Astro Content Collections."""
import json
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
CONTENT = ROOT / "src" / "content"
POSTS_JSON = CONTENT / "posts.json"
PAGES_JSON = CONTENT / "pages.json"
POSTS_DIR = CONTENT / "posts"
PAGES_DIR = CONTENT / "pages"

POSTS_DIR.mkdir(parents=True, exist_ok=True)
PAGES_DIR.mkdir(parents=True, exist_ok=True)

# Ручные флаги по slug'у для главной ленты.
# featured=True — пинится наверх как hero (отдельная карточка с рамкой).
# hideFromList=True — не показывается в общей ленте (виден только в рубрике/архиве).
# featuredImage — переопределяет первую <img> из тела для миниатюры/hero.
SLUG_FLAGS: dict[str, dict] = {
    'voronino': {'featured': True, 'featuredImage': '/uploads/IMG_2156.jpg'},
    'staroe-staroe-selo': {'featured': True, 'featuredImage': '/uploads/IMG_2754.jpg'},
    'vnimanie-texnicheskie-raboty': {'hideFromList': True},
    'vnimanie-texnicheskie-raboty-2': {'hideFromList': True},
    'c-nastupayushhim-novym-2014-godom': {'hideFromList': True},
}

def yaml_escape(s: str) -> str:
    return s.replace('"', '\\"')

def make_md(item: dict, kind: str) -> str:
    fm = [
        '---',
        f'title: "{yaml_escape(item["title"])}"',
        f'slug: {item["slug"]}',
        f'legacyId: {item["id"]}',
    ]
    if item.get('date'):
        date = item['date'].replace(' ', 'T') + '+03:00'
        if kind == 'post':
            fm.append(f'pubDate: {date}')
        else:
            fm.append(f'pubDate: {date}')
    if item.get('excerpt'):
        fm.append(f'description: "{yaml_escape(item["excerpt"])}"')
    else:
        fm.append('description: ""')
    if kind == 'post':
        if item.get('categories'):
            fm.append('categories:')
            for c in item['categories']:
                fm.append(f'  - "{yaml_escape(c)}"')
        else:
            fm.append('categories: []')
        if item.get('categorySlugs'):
            fm.append('categorySlugs:')
            for s in item['categorySlugs']:
                fm.append(f'  - "{s}"')
        else:
            fm.append('categorySlugs: []')
        fm.append('author: "История города Пушкино"')
    if item.get('oldSlug') and item['oldSlug'] != item['slug']:
        fm.append(f'oldSlug: "{item["oldSlug"]}"')
    if kind == 'post':
        flags = SLUG_FLAGS.get(item['slug'], {})
        if flags.get('featured'):
            fm.append('featured: true')
        if flags.get('hideFromList'):
            fm.append('hideFromList: true')
        if flags.get('featuredImage'):
            fm.append(f'featuredImage: "{flags["featuredImage"]}"')
    fm.append('---')
    fm.append('')
    fm.append(item['html'])
    return '\n'.join(fm) + '\n'

def convert(json_path: Path, out_dir: Path, kind: str) -> int:
    items = json.loads(json_path.read_text(encoding='utf-8'))
    for item in items:
        md = make_md(item, kind)
        (out_dir / f'{item["slug"]}.md').write_text(md, encoding='utf-8')
    return len(items)

if __name__ == '__main__':
    n_posts = convert(POSTS_JSON, POSTS_DIR, 'post')
    n_pages = convert(PAGES_JSON, PAGES_DIR, 'page')
    print(f'posts: {n_posts} → src/content/posts/')
    print(f'pages: {n_pages} → src/content/pages/')
