import { auth } from "@/auth";
import { redirect } from "next/navigation";
import { listExportsForQueue } from "@/lib/render-pipeline/exports";

export const metadata = { title: "Render Queue — VFX Review" };

export default async function RenderQueuePage() {
  const session = await auth();
  if (!session?.user) redirect("/login");

  const exports = await listExportsForQueue();

  return (
    <div className="p-6 space-y-6">
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-2xl font-semibold">Render Queue</h1>
          <p className="text-sm text-muted-foreground">Queued and active exports from the pipeline.</p>
        </div>
      </div>

      <div className="rounded-lg border border-border overflow-hidden">
        <table className="w-full text-sm">
          <thead className="bg-zinc-900/80 text-muted-foreground">
            <tr>
              <th className="px-4 py-3 text-left">Shot</th>
              <th className="px-4 py-3 text-left">Version</th>
              <th className="px-4 py-3 text-left">Status</th>
              <th className="px-4 py-3 text-left">Created</th>
            </tr>
          </thead>
          <tbody>
            {exports.map((item: any) => (
              <tr key={item.id} className="border-t border-border/60">
                <td className="px-4 py-3 font-mono">{item.shot?.shotCode ?? item.shotId}</td>
                <td className="px-4 py-3">{item.versionString}</td>
                <td className="px-4 py-3">{item.status}</td>
                <td className="px-4 py-3">{new Date(item.createdAt).toLocaleString()}</td>
              </tr>
            ))}
            {exports.length === 0 && (
              <tr>
                <td colSpan={4} className="px-4 py-8 text-center text-muted-foreground">
                  No exports queued yet.
                </td>
              </tr>
            )}
          </tbody>
        </table>
      </div>
    </div>
  );
}
