#!/usr/bin/env python3
"""
Convert template JSON to Notion API blocks format.
Usage: python3 build-notion-blocks.py bug-template.json "Bug description here"
"""

import sys
import json

def build_block(block, description):
    """Convert a template block to Notion API format."""
    text = block.get("text", "").replace("{{DESCRIPTION}}", description)

    if block["type"] == "callout":
        return {
            "type": "callout",
            "callout": {
                "rich_text": [{"text": {"content": text}}],
                "icon": {"emoji": block.get("emoji", "📌")}
            }
        }
    elif block["type"] == "heading_2":
        return {
            "type": "heading_2",
            "heading_2": {
                "rich_text": [{"text": {"content": text}}]
            }
        }
    elif block["type"] == "paragraph":
        return {
            "type": "paragraph",
            "paragraph": {
                "rich_text": [{"text": {"content": text}}]
            }
        }
    elif block["type"] == "to_do":
        return {
            "type": "to_do",
            "to_do": {
                "rich_text": [{"text": {"content": text}}],
                "checked": block.get("checked", False)
            }
        }
    else:
        return None

def main():
    if len(sys.argv) < 3:
        print("Usage: python3 build-notion-blocks.py <template.json> <description>")
        sys.exit(1)

    template_file = sys.argv[1]
    description = sys.argv[2]

    with open(template_file, 'r') as f:
        template = json.load(f)

    blocks = []
    for block in template.get("blocks", []):
        notion_block = build_block(block, description)
        if notion_block:
            blocks.append(notion_block)

    # Output the blocks as JSON
    output = {"children": blocks}
    print(json.dumps(output))

if __name__ == "__main__":
    main()
