"use client";

import { useEffect, useState } from "react";
import { useParams, useRouter } from "next/navigation";
import Link from "next/link";

const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL || "http://localhost:8000/api/v1";

export default function ActivatePage() {
  const params = useParams();
  const router = useRouter();
  const [loading, setLoading] = useState(true);
  const [success, setSuccess] = useState(false);
  const [error, setError] = useState("");

  useEffect(() => {
    const activateAccount = async () => {
      const { uid, token } = params;

      if (!uid || !token) {
        setError("Geçersiz aktivasyon linki.");
        setLoading(false);
        return;
      }

      try {
        const response = await fetch(`${API_BASE_URL}/auth/users/activation/`, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify({
            uid,
            token,
          }),
        });

        if (response.ok || response.status === 204) {
          setSuccess(true);
          setTimeout(() => {
            router.push("/auth/login");
          }, 3000);
        } else {
          const data = await response.json();
          setError(
            data.detail ||
              "Aktivasyon başarısız. Link geçersiz veya süresi dolmuş olabilir."
          );
        }
      } catch (err) {
        setError("Bir hata oluştu. Lütfen daha sonra tekrar deneyin.");
        console.error("Activation error:", err);
      } finally {
        setLoading(false);
      }
    };

    activateAccount();
  }, [params, router]);

  return (
    <div className="min-h-screen flex items-center justify-center bg-gray-50">
      <div className="max-w-md w-full space-y-8 p-8 bg-white rounded-lg shadow">
        <div className="text-center">
          {loading && (
            <>
              <div className="mx-auto flex items-center justify-center h-12 w-12 rounded-full bg-blue-100">
                <svg
                  className="animate-spin h-6 w-6 text-blue-600"
                  fill="none"
                  viewBox="0 0 24 24"
                >
                  <circle
                    className="opacity-25"
                    cx="12"
                    cy="12"
                    r="10"
                    stroke="currentColor"
                    strokeWidth="4"
                  />
                  <path
                    className="opacity-75"
                    fill="currentColor"
                    d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
                  />
                </svg>
              </div>
              <h2 className="mt-6 text-center text-3xl font-bold text-gray-900">
                Hesabınız Aktifleştiriliyor...
              </h2>
              <p className="mt-2 text-center text-sm text-gray-600">
                Lütfen bekleyin.
              </p>
            </>
          )}

          {success && (
            <>
              <div className="mx-auto flex items-center justify-center h-12 w-12 rounded-full bg-green-100">
                <svg
                  className="h-6 w-6 text-green-600"
                  fill="none"
                  stroke="currentColor"
                  viewBox="0 0 24 24"
                >
                  <path
                    strokeLinecap="round"
                    strokeLinejoin="round"
                    strokeWidth="2"
                    d="M5 13l4 4L19 7"
                  />
                </svg>
              </div>
              <h2 className="mt-6 text-center text-3xl font-bold text-gray-900">
                Aktivasyon Başarılı!
              </h2>
              <p className="mt-2 text-center text-sm text-gray-600">
                Hesabınız başarıyla aktifleştirildi. Artık giriş yapabilirsiniz.
              </p>
              <p className="mt-2 text-center text-sm text-gray-500">
                Giriş sayfasına yönlendiriliyorsunuz...
              </p>
            </>
          )}

          {error && (
            <>
              <div className="mx-auto flex items-center justify-center h-12 w-12 rounded-full bg-red-100">
                <svg
                  className="h-6 w-6 text-red-600"
                  fill="none"
                  stroke="currentColor"
                  viewBox="0 0 24 24"
                >
                  <path
                    strokeLinecap="round"
                    strokeLinejoin="round"
                    strokeWidth="2"
                    d="M6 18L18 6M6 6l12 12"
                  />
                </svg>
              </div>
              <h2 className="mt-6 text-center text-3xl font-bold text-gray-900">
                Aktivasyon Başarısız
              </h2>
              <p className="mt-2 text-center text-sm text-red-600">
                {error}
              </p>
              <div className="mt-6 space-y-3">
                <Link
                  href="/auth/resend-activation"
                  className="block w-full text-center py-2 px-4 border border-transparent text-sm font-medium rounded-md text-white bg-blue-600 hover:bg-blue-700"
                >
                  Aktivasyon Emaili Tekrar Gönder
                </Link>
                <Link
                  href="/auth/login"
                  className="block w-full text-center py-2 px-4 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50"
                >
                  Giriş Sayfasına Dön
                </Link>
              </div>
            </>
          )}
        </div>
      </div>
    </div>
  );
}

