<script lang="ts">
  import { goto } from '$app/navigation';

  let identifier = $state('');
  let error = $state('');
  let success = $state(false);
  let loading = $state(false);

  async function handleSubmit(e: Event) {
    e.preventDefault();
    error = '';
    loading = true;

    try {
      const res = await fetch('/api/auth/forgot', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ identifier }),
      });

      const data = await res.json();

      if (!res.ok) {
        error = data.error || 'Request failed';
        return;
      }

      success = true;
    } catch (err) {
      error = 'Connection error';
    } finally {
      loading = false;
    }
  }
</script>

<div class="login-container">
  <div class="login-card">
    <h1>Reset Password</h1>
    
    {#if error}
      <div class="error">{error}</div>
    {/if}

    {#if success}
      <div class="success">
        <p>If an account exists with that email or username, a password reset link has been sent.</p>
        <p>Please check your inbox.</p>
      </div>
      <div class="links">
        <a href="/auth/login">Back to Login</a>
      </div>
    {:else}
      <p class="info">Enter your email address or username and we'll send you a link to reset your password.</p>

      <form onsubmit={handleSubmit}>
        <label>
          Email or Username
          <input 
            type="text" 
            bind:value={identifier} 
            placeholder="user@example.org or username"
            required 
            disabled={loading}
          />
        </label>

        <button type="submit" disabled={loading}>
          {loading ? 'Sending...' : 'Send Reset Link'}
        </button>
      </form>

      <div class="links">
        <a href="/auth/login">Back to Login</a>
      </div>
    {/if}
  </div>
</div>

<style>
  .login-container {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 1rem;
  }

  .login-card {
    background: white;
    padding: 2rem;
    border-radius: 12px;
    box-shadow: 0 10px 40px rgba(0,0,0,0.2);
    width: 100%;
    max-width: 400px;
  }

  h1 {
    text-align: center;
    margin: 0 0 1rem;
    color: #2c3e50;
  }

  .info {
    color: #666;
    text-align: center;
    margin-bottom: 1.5rem;
    font-size: 0.9rem;
  }

  .error {
    background: #fee;
    color: #c00;
    padding: 0.75rem;
    border-radius: 4px;
    margin-bottom: 1rem;
    font-size: 0.9rem;
  }

  .success {
    background: #efe;
    color: #060;
    padding: 1rem;
    border-radius: 4px;
    margin-bottom: 1rem;
    text-align: center;
  }

  .success p {
    margin: 0.5rem 0;
  }

  form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
  }

  label {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    font-weight: 500;
    color: #555;
  }

  input {
    padding: 0.75rem;
    border: 1px solid #ddd;
    border-radius: 6px;
    font-size: 1rem;
  }

  input:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
  }

  button {
    padding: 0.75rem;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border: none;
    border-radius: 6px;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: opacity 0.2s;
  }

  button:hover:not(:disabled) {
    opacity: 0.9;
  }

  button:disabled {
    opacity: 0.6;
    cursor: not-allowed;
  }

  .links {
    margin-top: 1.5rem;
    text-align: center;
    padding-top: 1rem;
    border-top: 1px solid #eee;
  }

  .links a {
    color: #667eea;
    text-decoration: none;
    font-size: 0.9rem;
  }

  .links a:hover {
    text-decoration: underline;
  }
</style>
