import React, { useState, useRef } from "react";

export default function HomeScene() {
  const [, setSelectedFile] = useState<File | null>(null);
  const fileInputRef = useRef<HTMLInputElement | null>(null); // Référence pour l'input fichier

  const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    if (event.target.files && event.target.files.length > 0) {
      const file = event.target.files[0];
      setSelectedFile(file);
      handleUpload(file);
    }
  };

  const handleUpload = async (file: File) => {
    const formData = new FormData();
    formData.append("file", file);

    try {
      const response = await fetch("https://your-backend-api/upload", {
        method: "POST",
        body: formData,
      });

      if (response.ok) {
        console.log("Fichier uploadé avec succès");
      } else {
        console.error("Erreur lors de l'upload du fichier");
      }
    } catch (error) {
      console.error("Erreur lors de l'upload du fichier :", error);
    }
  };

  const handleButtonClick = () => {
    if (fileInputRef.current) {
      fileInputRef.current.click();
    }
  };

  return (
    <div className="flex h-screen bg-gray-100 items-center justify-center">
      <a href="verification" className="mr-4">
        <button className="bg-blue-600 text-white px-4 py-2 rounded hover:bg-blue-700">
          Verify Your Identity
        </button>
      </a>

      <input
        type="file"
        ref={fileInputRef}
        className="hidden"
        onChange={handleFileChange}
      />

      <button
        onClick={handleButtonClick}
        className="bg-purple-600 text-white px-4 py-2 rounded shadow-lg hover:bg-purple-700 transition"
      >
        Upload Private Document
      </button>
    </div>
  );
}
