#!/usr/bin/env python3

from argparser import argparser
from classes import *
from functions import * 

def build_site_old():
    logger.info(f"Building the '{Config.MAIN_PAGE_TITLE}' site.")      

    # Recreate the output dir if needed
    if output_dir.exists():
        shutil.rmtree(output_dir)
    output_dir.mkdir()

    # Create public subdirs
    subdirs = [img_dir, assets_css_dir, assets_js_dir]
    for subdir in subdirs:
        subdir.mkdir(parents=True, exist_ok=True)

    # Copy static files if exist
    if static_dir.exists():
        shutil.copytree(static_dir, output_dir / "static", dirs_exist_ok=True)

     # Parse the content files
    content_items = []
    for md_file in content_dir.glob("*.md"):
        current_content_item = ContentItemPrototype(md_file)
        current_content_item.render_content()
        content_items.append({
            "slug": current_content_item.slug,
            "title": current_content_item.title,
            "date": current_content_item.date,
    #        "preview": markdown.markdown(current_content_item.preview),
            "image": current_content_item.image,
        })

    # Render the index file
    with (output_dir / "index.html").open("w", encoding="utf-8") as f:
        f.write(index_template.render(page_title = Config.MAIN_PAGE_TITLE, content_items=content_items))

    # Render the about file
    about_content = ContentItemPrototype(Path('static/about.md'))
    about_content.render_content()

    # Move 'robots.txt' into output_dir
    shutil.copyfile(static_dir / 'robots.txt', output_dir / 'robots.txt')

    logger.info(f"Created {len(content_items)} content items.")




def main():
    args = argparser.parse_args()
    match args.command:
        case "build":
            build_site()
        case "init":
            init_site()
        case "new" | "create" | "edit":
            edit_content(args.filename)

if __name__ == '__main__':
    main()