import { PageProps, RouteContext } from "$fresh/server.ts";
import { MainLayout } from "@components/layouts/main.tsx";
import { getMovie, Movie } from "@lib/resource/movies.ts";
import { HashTags } from "@components/HashTags.tsx";
import { renderMarkdown } from "@lib/documents.ts";
import { KMenu } from "@islands/KMenu.tsx";
import { RedirectSearchHandler } from "@islands/Search.tsx";
import { Recommendations } from "@islands/Recommendations.tsx";
import PageHero from "@components/PageHero.tsx";
import { Star } from "@components/Stars.tsx";

export default async function Greet(
  props: PageProps<{ movie: Movie; session: Record<string, string> }>,
  ctx: RouteContext,
) {
  const movie = await getMovie(ctx.params.name);
  const session = ctx.state.session;

  const { author = "", date = "" } = movie.meta;

  const content = renderMarkdown(movie.description || "");

  return (
    <MainLayout url={props.url} title={`Movie > ${movie.name}`} context={movie}>
      <RedirectSearchHandler />
      <KMenu type="main" context={movie} />
      <PageHero
        image={movie.meta.image}
        thumbnail={movie.meta.thumbnail}
      >
        <PageHero.Header>
          <PageHero.BackLink href="/movies" />
          {session && (
            <PageHero.EditLink
              href={`https://notes.max-richter.dev/Media/movies/${movie.id}`}
            />
          )}
        </PageHero.Header>
        <PageHero.Footer>
          <PageHero.Title>{movie.name}</PageHero.Title>
          <PageHero.Subline
            entries={[
              author && {
                title: author,
                href: `/?q=${encodeURIComponent(author)}`,
              },
              date.toString(),
            ]}
          >
            {movie.meta.rating && <Star rating={movie.meta.rating} />}
          </PageHero.Subline>
        </PageHero.Footer>
      </PageHero>
      <Recommendations
        id={movie.id}
        type="movie"
      />
      {movie.tags.length > 0 && (
        <>
          <br />
          <HashTags tags={movie.tags} />
        </>
      )}
      <div class="px-8 text-white mt-10">
        {movie?.description?.length > 80
          ? <h2 class="text-4xl font-bold mb-4">Review</h2>
          : <></>}
        <pre
          class="whitespace-break-spaces"
          dangerouslySetInnerHTML={{ __html: content || "" }}
        >
          {content}
        </pre>
      </div>
    </MainLayout>
  );
}
