using Microsoft.AspNetCore.Mvc.RazorPages;
using TaxBaik.Application.Services;
using TaxBaik.Domain.Entities;
using System.Net;

namespace TaxBaik.Web.Pages.Blog;

public class BlogPostModel : PageModel
{
    private readonly BlogService _blogService;

    public BlogPost? Post { get; set; }
    public string? HtmlContent { get; set; }

    public BlogPostModel(BlogService blogService)
    {
        _blogService = blogService;
    }

    public async Task OnGetAsync(string slug)
    {
        Post = await _blogService.GetBySlugAsync(slug);
        if (Post != null)
        {
            HtmlContent = WebUtility.HtmlEncode(Post.Content ?? "").Replace("\r\n", "<br />").Replace("\n", "<br />");
            _ = _blogService.IncrementViewCountAsync(Post.Id);
        }
    }
}
