'use client';

import React, { useState } from 'react';

export default function TestLogout() {
  const [refreshToken, setRefreshToken] = useState('');
  const [result, setResult] = useState<string | null>(null);

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    setResult(null);

    try {
      const response = await fetch('/api/users/logout', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'x-api-key': process.env.NEXT_PUBLIC_API_KEY ?? '',
        },
        body: JSON.stringify({
          refreshToken
        })
      });
      const data = await response.json() as {message: string}
      setResult(JSON.stringify(data, null, 2));
    } catch (error) {
      console.error('Error:', error);
      setResult('An error occurred');
    }
  };
  return (
    <main className="flex min-h-screen flex-col items-center justify-center
      bg-gradient-to-b from-pink-500 to-orange-400 text-white cursor-pointer">
      <div className="p-4">
        <h1 className="text-2xl mb-4">Test Logout</h1>
        <form onSubmit={handleSubmit} className="space-y-4">
          <div>
            <label htmlFor="refreshToken" className="block">Token:</label>
            <input
              type="text"
              id="refreshToken"
              value={refreshToken}
              onChange={(e) => setRefreshToken(e.target.value)}
              className="border p-2 w-full bg-black"
              required
            />
          </div>
          <button type='submit' className='bg-blue-500 p-2 rounded'>
            Logout
          </button>
        </form>
        {result && (
          <div className='mt-4 p-2 rounded bg-black'>
            <pre>{result}</pre>
          </div>
        )}
      </div>
    </main>
  );
};
