@page "/{slug:nonfile}"
@inject IPostService PostService
@inject NavigationManager NavigationManager
@inject IJSRuntime JSRuntime
@inject ILogger<Post> Logger

@if (BlogPost is null)
{
  return;
}
else
{
  var title = $"{BlogPost.Title} - journal";
  var description = BlogPost.Lead;

  <PageTitle>@title</PageTitle>

  <HeadContent>
    <meta name="description" content="@description">
    <OpenGraph 
      PageTitle="@title" 
      Description="@description" 
      Type="article"
      Image="@BlogPost.OpenGraphImage"
    />
    <link rel="stylesheet" href="markdown.css">
    <link rel="stylesheet" href="prism.css">
  </HeadContent>

  <article>
    <div class="heading-container">
      <h2>@BlogPost.Title</h2>
      <p>@BlogPost.PublishedAt.ToString("MMMM d, yyyy")</p>
    </div>
    <div class="markdown-body">
      @((MarkupString)BlogPost.Content)
    </div>
  </article>

  <PageScript Src="./Components/Pages/Post.razor.js" />
}

@code 
{
  [Parameter] 
  public string Slug { get; set; } = string.Empty;
  private PostWithContent? BlogPost;

  protected override async Task OnInitializedAsync()
  {
    var post = await PostService.GetPostAsync(Slug);

    if (post is null)
    {
      NavigationManager.NavigateTo("/Error/404");
      return;
    }

    BlogPost = post;
  }
}