package view

import (
	"fmt"
	"html/template"
	"net/http"
	"os"
	"time"

	"arimelody-web/controller"
	"arimelody-web/model"
	"arimelody-web/templates"

	"github.com/gomarkdown/markdown"
	"github.com/gomarkdown/markdown/html"
	"github.com/gomarkdown/markdown/parser"
)

var mdRenderer = html.NewRenderer(html.RendererOptions{
	Flags: html.CommonFlags | html.HrefTargetBlank,
})

func BlogHandler(app *model.AppState) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		blog := model.Blog{
			Title: "hello world!",
			Description: "lorem ipsum yadda yadda something boobies babababababababa",
			Visible: true,
			Date: time.Now(),
			AuthorID: "ari",
			Markdown:
`
**i'm ari!**

she/her 🏳️‍⚧️🏳️‍🌈💫🦆🇮🇪

i'm a musician, developer, streamer, youtuber, and probably a bunch of other things i forgot to mention!

you're very welcome to take a look around my little space on the internet here, or explore any of the other parts i inhabit!

if you're looking to support me financially, that's so cool of you!! if you like, you can buy some of my music over on bandcamp so you can at least get something for your money. thank you very much either way!! 💕

for anything else, you can reach me for any and all communications through ari@arimelody.me. if your message contains anything beyond a silly gag, i strongly recommend encrypting your message using my public pgp key, listed below!

thank you for stopping by- i hope you have a lovely rest of your day! 💫

## metadata

**my colours 🌈**

- primary: <span class="col-primary">#b7fd49</span>
- secondary: <span class="col-secondary">#f8e05b</span>
- tertiary: <span class="col-tertiary">#f788fe</span>

**my keys 🔑**

- pgp: [[link]](/keys/ari%20melody_0x92678188_public.asc)
- ssh (ed25519): [[link]](/keys/id_ari_ed25519.pub)

**where to find me 🛰️**

- youtube
- twitch
- spotify
- bandcamp
- github

**projects i've worked on 🛠️**

- catdance
- pride flag
- ipaddrgen
- impact meme
- OpenTerminal
- Silver.js

### code block test

~~~ c
#include <stdio.h>

int main(int argc, char *argv[]) {
	printf("hello world!~\n");
	return 0;
}
~~~

### aridoodle

this is `+"`"+`aridoodle`+"`"+`. please take care of her.

![aridoodle](/img/aridoodle.png)
`,
			BlueskyActorID: "did:plc:yct6cvgfipngizry5umzkxr3",
			BlueskyPostID: "3llsudsx7pc2u",
		}

		// blog.Markdown += " <i class=\"end-mark\"></i>"

		mdParser := parser.NewWithExtensions(parser.CommonExtensions | parser.AutoHeadingIDs)
		md := mdParser.Parse([]byte(blog.Markdown))
		blog.HTML = template.HTML(markdown.Render(md, mdRenderer))

		comments := []*model.ThreadViewPost{}
		blueskyPost, err := controller.FetchThreadViewPost(blog.BlueskyActorID, blog.BlueskyPostID)
		if err != nil {
			fmt.Fprintf(os.Stderr, "WARN: Failed to fetch blog post Bluesky thread: %v\n", err)
		} else {
			comments = append(comments, blueskyPost.Replies...)
		}

		type BlogView struct {
			*model.Blog
			Comments []*model.ThreadViewPost
			Likes int
			Reposts int
			BlueskyURL string
			MastodonURL string
		}

		err = templates.BlogTemplate.Execute(w, BlogView{
			Blog: &blog,
			Comments: blueskyPost.Replies,
			Likes: 10,
			Reposts: 10,
			BlueskyURL: fmt.Sprintf("https://bsky.app/profile/%s/post/%s", blog.BlueskyActorID, blog.BlueskyPostID),
			MastodonURL: "#",
		})
		if err != nil {
            fmt.Fprintf(os.Stderr, "Error rendering blog post: %v\n", err)
            http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
            return
		}
	})
}
