'use client';

import { Suspense, useEffect, useState } from 'react';
import { useRouter, useSearchParams } from 'next/navigation';

interface TokenInfo {
  groupName: string;
  expiresAt: string;
  isConsumed: boolean;
  isExpired: boolean;
}

function ClaimGroupContent() {
  const router = useRouter();
  const searchParams = useSearchParams();
  const token = searchParams.get('token');
  const [info, setInfo] = useState<TokenInfo | null>(null);
  const [error, setError] = useState<string | null>(null);
  const [busy, setBusy] = useState(false);
  const [success, setSuccess] = useState(false);

  useEffect(() => {
    if (!token) {
      setError('No claim token provided');
      return;
    }
    fetch(`/api/groups/claim-token-info?token=${encodeURIComponent(token)}`)
      .then((r) => r.json().catch(() => null))
      .then((data) => {
        if (data?.groupName) setInfo(data);
        else setError('Invalid or expired claim link');
      })
      .catch(() => setError('Failed to load claim info'));
  }, [token]);

  async function handleClaim() {
    if (!token) return;
    setBusy(true);
    setError(null);
    try {
      const res = await fetch('/api/groups/claim-with-token', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ token }),
      });
      if (res.status === 401) {
        router.push(`/signup?redirect=/claim-group?token=${encodeURIComponent(token)}`);
        return;
      }
      if (!res.ok) {
        const body = await res.json().catch(() => ({}));
        setError(body.message ?? 'Claim failed');
        return;
      }
      setSuccess(true);
    } finally {
      setBusy(false);
    }
  }

  if (error && !info) {
    return (
      <div className="max-w-md mx-auto mt-16 text-center">
        <h1 className="text-xl font-semibold mb-4">Claim Group</h1>
        <p className="text-red-600 text-sm">{error}</p>
      </div>
    );
  }

  if (success) {
    return (
      <div className="max-w-md mx-auto mt-16 text-center">
        <h1 className="text-xl font-semibold mb-4">Group Claimed!</h1>
        <p className="text-green-600 text-sm mb-4">{info?.groupName ?? 'Group'} has been added to your tenant.</p>
        <a href="/groups" className="text-blue-600 hover:underline text-sm">Go to Groups</a>
      </div>
    );
  }

  return (
    <div className="max-w-md mx-auto mt-16">
      <h1 className="text-xl font-semibold mb-4">Claim Group</h1>
      {info && (
        <div className="border border-gray-200 rounded-lg p-4 mb-4">
          <p className="text-sm mb-1">
            <span className="text-gray-500">Group:</span>{' '}
            <span className="font-medium">{info.groupName}</span>
          </p>
          <p className="text-sm text-gray-500">
            Expires: {new Date(info.expiresAt).toLocaleDateString()}
          </p>
          {info.isConsumed && (
            <p className="text-sm text-amber-600 mt-2">This link has already been used.</p>
          )}
          {info.isExpired && (
            <p className="text-sm text-red-600 mt-2">This link has expired.</p>
          )}
        </div>
      )}
      {error && (
        <p className="text-red-600 text-sm mb-4 bg-red-50 border border-red-200 rounded px-3 py-2">{error}</p>
      )}
      <button
        onClick={handleClaim}
        disabled={busy || !info || info.isConsumed || info.isExpired}
        className="w-full rounded-lg bg-blue-600 px-4 py-2 text-sm font-medium text-white hover:bg-blue-700 disabled:opacity-50"
      >
        {busy ? 'Claiming…' : 'Claim this group'}
      </button>
    </div>
  );
}

export default function ClaimGroupPage() {
  return (
    <Suspense fallback={<div className="max-w-md mx-auto mt-16 text-center"><p className="text-gray-500 text-sm">Loading…</p></div>}>
      <ClaimGroupContent />
    </Suspense>
  );
}
