import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card";
import { getAgentVersionInfo, getBaseUrlFromRequest } from "@/lib/agent-version";
import { headers } from "next/headers";

export const dynamic = "force-dynamic";

export default async function DownloadPage() {
  const h = await headers();
  const proto = h.get("x-forwarded-proto") ?? "https";
  const host = h.get("x-forwarded-host") ?? h.get("host") ?? "";
  const baseUrl = host ? `${proto}://${host}` : undefined;
  const info = getAgentVersionInfo(baseUrl);
  const { version, downloadUrls } = info;

  return (
    <div className="space-y-6">
      <h1 className="text-3xl font-bold">Téléchargements Agent</h1>
      <p className="text-sm text-muted-foreground">Version actuelle : <strong>{version}</strong></p>
      <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
        <Card>
          <CardHeader>
            <CardTitle>Windows (.exe)</CardTitle>
          </CardHeader>
          <CardContent>
            <p className="text-sm text-muted-foreground mb-4">Agent studioE5 pour Windows (64 bits). Nécessite Tailscale installé séparément ou les binaires dans <code>tailscale-bin/windows/</code>.</p>
            <a href={downloadUrls.windows} download className="inline-flex items-center justify-center rounded-md text-sm font-medium bg-primary text-primary-foreground hover:bg-primary/90 h-10 px-4 py-2 w-full">Télécharger (.exe)</a>
          </CardContent>
        </Card>

        <Card>
          <CardHeader>
            <CardTitle>Windows (archive)</CardTitle>
          </CardHeader>
          <CardContent>
            <p className="text-sm text-muted-foreground mb-4">Archive complète incluant l&apos;agent, Tailscale et le README Windows.</p>
            <a href={downloadUrls.windowsZip} download className="inline-flex items-center justify-center rounded-md text-sm font-medium bg-primary text-primary-foreground hover:bg-primary/90 h-10 px-4 py-2 w-full">Télécharger (.zip)</a>
          </CardContent>
        </Card>

        <Card>
          <CardHeader>
            <CardTitle>Linux</CardTitle>
          </CardHeader>
          <CardContent>
            <p className="text-sm text-muted-foreground mb-4">Agent studioE5 pour Linux (64 bits).</p>
            <a href={downloadUrls.linux} download className="inline-flex items-center justify-center rounded-md text-sm font-medium bg-primary text-primary-foreground hover:bg-primary/90 h-10 px-4 py-2 w-full">Télécharger (Linux)</a>
          </CardContent>
        </Card>
      </div>
    </div>
  );
}
