import { Type } from "@sinclair/typebox";
import { FirRouteInput, FirRouteOptions } from "../util/routewrap.ts";
import { getRelease, getReleases } from "../dbal/dbal.ts";

const method = "GET";
const url = "/api/release";

const payloadT = Type.Any();

const handler = async ({payload}: FirRouteInput<typeof payloadT>) => {
	const {author, slug, version} = payload;

	if (typeof author !== "string") {
		return {
			release: null,
			versions: [],
		};
	}
	if (typeof slug !== "string") {
		return {
			release: null,
			versions: [],
		};
	}

	const release = await getRelease({author, slug, version});
	const releases = await getReleases({author, slug});

	const versions = releases.map(r => r.version);
	
	return {
		release,
		versions,
	}
};

export default {
	method,
	url,
	payloadT,
	handler,
} as const satisfies FirRouteOptions<typeof payloadT>;