import { Handlers, PageProps } from "$fresh/server.ts";
import { MainLayout } from "@components/layouts/main.tsx";
import { getMovie, Movie } from "@lib/resource/movies.ts";
import { RecipeHero } from "@components/RecipeHero.tsx";
import { KMenu } from "@islands/KMenu.tsx";
import { HashTags } from "@components/HashTags.tsx";

export const handler: Handlers<Movie | null> = {
  async GET(_, ctx) {
    const movie = await getMovie(ctx.params.name);
    return ctx.render(movie);
  },
};

export default function Greet(props: PageProps<Movie>) {
  const movie = props.data;

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

  return (
    <MainLayout url={props.url} title={`Movie > ${movie.name}`}>
      <RecipeHero
        data={movie}
        subline={[author, date.toString()]}
        editLink={`https://notes.max-richter.dev/Media/movies/${movie.id}`}
        backlink="/movies"
      />
      <KMenu type="main" context={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: movie.description || "" }}
        >
          {movie.description}
        </pre>
      </div>
    </MainLayout>
  );
}
