"use client";

import Link from "next/link";
import { Copy, ExternalLink } from "lucide-react";
import { toast } from "sonner";

import { PdfDownloadButton } from "@/components/reports/pdf-download-button";
import { Button } from "@/components/ui/button";
import { getAbsoluteShareUrl, getSharePath } from "@/utils/report-sharing";

interface ShareReportActionsProps {
  reportId: string;
  reportToken: string;
  appBaseUrl: string;
}

export function ShareReportActions({
  reportId,
  reportToken,
  appBaseUrl,
}: ShareReportActionsProps) {
  const sharePath = getSharePath(reportToken);

  const copyLink = async () => {
    try {
      const url = getAbsoluteShareUrl(reportToken, appBaseUrl);
      await navigator.clipboard.writeText(url);
      toast.success("Share link copied.");
    } catch {
      toast.error("Unable to copy link.");
    }
  };

  return (
    <div className="flex flex-wrap gap-2">
      <PdfDownloadButton
        endpoint={`/api/reports/${reportId}/pdf`}
        filename={`alwirasah-${reportId}.pdf`}
        variant="outline"
      />
      <Button variant="outline" onClick={copyLink}>
        <Copy className="size-4" />
        Copy Share Link
      </Button>
      <Button asChild>
        <Link href={sharePath} target="_blank">
          <ExternalLink className="size-4" />
          Open Shared View
        </Link>
      </Button>
    </div>
  );
}
