/**
 * WorthDoing.ai
 * Author: Simon-Pierre Boucher
 * Contact: contact@spboucher.ai
 * File: src/app/explore/page.tsx
 * Description: Explore page — the accumulated opportunity database, ranked by Worth Score.
 */
import { desc, eq } from "drizzle-orm";
import { db } from "@/lib/db/client";
import { opportunities, investigations } from "@/lib/db/schema";
import { OpportunityCard } from "@/components/wd/OpportunityCard";

export const dynamic = "force-dynamic";

export default async function ExplorePage() {
  const rows = await db
    .select({
      id: opportunities.id,
      title: opportunities.title,
      summary: opportunities.summary,
      worthScore: opportunities.worthScore,
      evidenceConfidence: opportunities.evidenceConfidence,
      objective: investigations.objective,
      reportMd: opportunities.reportMd,
    })
    .from(opportunities)
    .innerJoin(investigations, eq(investigations.id, opportunities.investigationId))
    .orderBy(desc(opportunities.worthScore))
    .limit(100);

  return (
    <div className="mx-auto w-full max-w-6xl px-4 py-10 sm:px-6">
      <h1 className="font-heading text-3xl font-semibold tracking-tight text-ink sm:text-4xl">
        The opportunity database
      </h1>
      <p className="mt-2 max-w-2xl text-ink-soft">
        Everything the agent has judged worth doing — ranked by Worth Score, with evidence
        confidence shown separately.
      </p>

      {rows.length === 0 ? (
        <div className="mt-16 rounded-xl border border-dashed border-line bg-card/50 p-12 text-center">
          <p className="font-heading text-xl text-ink">Nothing here yet</p>
          <p className="mt-2 text-sm text-ink-soft">
            Run an investigation from the home page — validated opportunities land here.
          </p>
        </div>
      ) : (
        <div className="mt-8 grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3">
          {rows.map((o) => (
            <OpportunityCard
              key={o.id}
              id={o.id}
              title={o.title}
              summary={o.summary}
              worthScore={o.worthScore}
              evidenceConfidence={o.evidenceConfidence}
              objective={o.objective}
              hasReport={!!o.reportMd}
            />
          ))}
        </div>
      )}
    </div>
  );
}
