use v6.e.PREVIEW;

use HTML::Functional;
use DB::BlogMeta;
use DB::Post;

unit class Config;

method generate-head(Str:D $title, BlogMeta:D $meta, $description?) {
    head [
        meta :charset<utf-8>;
        meta :name<viewport>, :content<width=device-width, initial-scale=1>;
        meta :author :content<Nathan McCarty>;
        title "{$meta.title} — $title";
        # Add description, if one exists
        do if $description ~~ Str:D {
            meta :description :content($description)
        } else {
            []
        }
        # Preconnect to all our resource sources
        link :rel<preconnect> :href<https://static.stranger.systems>;
        link :rel<preconnect> :href<https://fonts.googleapis.com>;
        link :rel<preconnect> :href<https://fonts.gstatic.com> :crossorigin;
        # Load fonts, Iosevka Alie for code, and Open Sans for content
        link :rel<stylesheet>,
            :href<https://static.stranger.systems/fonts/IosevkaAlie/IosevkaAile.css>;
        link :rel<stylesheet>,
            :href<https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,300..800;1,300..800&display=swap>;
        # Inline our style sheets
        style %?RESOURCES<main.css>.slurp;
        style %?RESOURCES<code.css>.slurp;
        # TODO: Add header links
    ];
}

# TODO: Support GFM admonitions
method generate-post(Post:D $post, BlogMeta:D $meta) {
    my $content = $post.render-html;
    my $head = self.generate-head($post.title, $meta, $post.description);
    my $body =
    body [
       div :class<post-body>, [
          $content
       ]
    ];
    # TODO: Setup footer
    # my $footer;

   my $html = html :lang<en>, [
       $head,
       $body
   ];

   "<!doctype html>$html"
}