import { database } from "~/database/context";
import * as schema from "~/database/schema";

import type { Route } from "./+types/home";
import { CastrationTracker } from "../components/castration-tracker";

export function meta({}: Route.MetaArgs) {
  return [
    { title: "Castration Tracker" },
    { name: "description", content: "Track animal castrations" },
  ];
}

export async function loader({ context }: Route.LoaderArgs) {
  const db = database();

  const castrations = await db.query.castrations.findMany({
    orderBy: (castrations, { desc }) => [desc(castrations.timestamp)],
  });

  const totalCount = castrations.length;

  return {
    castrations,
    totalCount,
    message: context.VALUE_FROM_EXPRESS,
  };
}

export default function Home({ loaderData }: Route.ComponentProps) {
  return (
    <CastrationTracker
      castrations={loaderData.castrations}
      totalCount={loaderData.totalCount}
    />
  );
}
