import { Handlers, PageProps } from "$fresh/server.ts";
import { Database, Option } from "types";
import { get_db_obj } from "db";
import Interactive from "../../../islands/Interactive.tsx";

export const handler: Handlers<Option<Database>> = {
    async GET(req, ctx) {
        const db = await get_db_obj();
        if (
            db.isNone() ||
            (db.isSome() &&
                !db.unwrap().conversations[ctx.params.conversation_id])
        ) {
            return ctx.renderNotFound();
        }
        return ctx.render(db);
    },
};

export default function ConversationPreview({
    data,
    params,
}: PageProps<Option<Database>>) {
    if (
        data.isSome() &&
        params.conversation_id &&
        data.unwrap().conversations[params.conversation_id]
    ) {
        return <Interactive conv={params.conversation_id} db={data.unwrap()} />;
    }
}

//TODO: keyboard shortcut (advertised elsewhere) that activates interactive mode
