'use client';

import { useState } from "react";
import { shortenLink } from "./utils/api";
import { Clipboard, Minimize2 } from "lucide-react";

export default function Home() {
  const [errorMessage, setErrorMessage] = useState<string | null>(null);
  const [successMessage, setSuccessMessage] = useState<string | null>(null);
  const [loading, setLoading] = useState<boolean>(false);

  const [shortUrl, setShortUrl] = useState<string | null>(null);
  const [manageUrl, setManageUrl] = useState<string | null>(null);

  async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
    event.preventDefault();
    setLoading(true);
    try {
      const formData = new FormData(event.currentTarget);
      const longUrl = formData.get("longUrl") as string;
      const res = await shortenLink(longUrl);
      setSuccessMessage("Shortened URL!");
      setErrorMessage(null);
      setShortUrl(res.shortUrl);
      setManageUrl(res.manageUrl);
    } catch (err) {
      console.error(err);
      setErrorMessage("Failed to shorten the link. Please try again.");
      setSuccessMessage(null);
    } finally {
      setLoading(false);
    }
  }

  function copyToClipboard(text: string, title: string) {
    navigator.clipboard.writeText(text).then(
      () => {
        setSuccessMessage(`Copied ${title} to clipboard!`);
      },
      () => {
        setErrorMessage(`Failed to copy ${title} to clipboard.`);
      }
    );
  }

  return (
    <div className="bg-stone-300 flex min-h-screen flex-col items-center justify-center py-2 text-center">
      <h1 className="text-3xl font-bold text-red-950">halflink</h1>
      <p className="text-black">Shorten a link with ease.</p>

      <form className="mt-4 flex w-full max-w-xl" onSubmit={handleSubmit}>
        <input
          type="text"
          name="longUrl"
          placeholder="Enter your long URL here"
          className="bg-white flex-grow border border-stone-400 rounded-l px-4 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500"
        />
        <button
          type="submit"
          disabled={loading}
          className="bg-red-900 disabled:bg-red-300 text-white px-4 py-2 rounded-r hover:bg-red-600 focus:outline-none focus:ring-2 focus:ring-blue-500"
        >
          <Minimize2 />
        </button>
      </form>
      {errorMessage && (
        <p className="mt-4 text-red-800 font-bold">{errorMessage}</p>
      )}
      {successMessage && (
        <p className="mt-4 text-green-800 font-bold">{successMessage}</p>
      )}

      {shortUrl && manageUrl && (
        <div className="flex flex-col items-center w-full">

          <p className="mt-4">Shareable short link:</p>
          <div className="flex justify-center w-full max-w-xl">
            <input
              type="text"
              value={shortUrl}
              readOnly
              className="bg-white w-full max-w-md border border-stone-400 rounded-l px-4 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500"
            />
            <button
              type="submit"
              className="flex justify-center w-16 bg-red-900 text-white px-4 py-2 rounded-r hover:bg-red-600 focus:outline-none focus:ring-2 focus:ring-blue-500"
              onClick={() => copyToClipboard(shortUrl, "short link")}
            >
              <Clipboard />
            </button>
          </div>

          <p className="mt-4">View analytics and manage your short link at:</p>
          <a
            href={manageUrl}
            className="mb-2 text-red-900 underline hover:text-red-600"
          >{manageUrl}</a>

        </div>
      )}
    </div>
  );
}
