"use client";

import React from "react";
import { useToast } from "@/hooks/use-toast";
import { toast as toastMessage } from "sonner";
import { Download } from "lucide-react";
import { saveAs } from "file-saver";
import { Button } from "@/components/ui/button";
import { getReportTempFileData } from "@/lib/forms/services";

export const TempDownloadBtn = () => {
  const { toast } = useToast();

  const downloadTempFile = async () => {
    try {
      const { fileName, fileType, base64 } = await getReportTempFileData();

      saveAs(`data:${fileType};base64,${base64}`, fileName);

      toastMessage.success("Report Sample File 다운로드 완료!");
    } catch (err) {
      console.log(err);
      toast({
        title: "Error",
        description: "Sample File을 찾을 수가 없습니다.",
        variant: "destructive",
      });
    }
  };
  return (
    <Button
      variant="ghost"
      className="relative p-2"
      aria-label="Template Sample Download"
      onClick={downloadTempFile}
    >
      <Download />
    </Button>
  );
};
