import { Component, createSignal } from 'solid-js';
import { getClerk } from '../../lib/auth/clerk-client';

export const SignUp: Component = () => {
  const [email, setEmail] = createSignal('');
  const [name, setName] = createSignal('');
  const [password, setPassword] = createSignal('');
  const [error, setError] = createSignal<string | null>(null);
  const [loading, setLoading] = createSignal(false);

  const handleSubmit = async (e: Event) => {
    e.preventDefault();
    setLoading(true);
    setError(null);

    try {
      const clerk = getClerk();
      if (!clerk) {
        setError('Authentication service unavailable');
        return;
      }

      await clerk.openSignUp({
        initialValues: {
          emailAddress: email(),
          firstName: name().split(' ')[0] || '',
          lastName: name().split(' ')[1] || '',
        },
      });

      window.location.href = '/';
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to create account');
    } finally {
      setLoading(false);
    }
  };

  return (
    <div class="freno-auth-container">
      <div class="freno-auth-card">
        <div class="freno-auth-header">
          <h1 class="freno-auth-title">Create your account</h1>
          <p class="freno-auth-subtitle">Start writing collaboratively today</p>
        </div>

        {error() && (
          <div class="freno-alert freno-alert-error" role="alert">
            {error()}
          </div>
        )}

        <form class="freno-auth-form" onSubmit={handleSubmit}>
          <div class="freno-form-group">
            <label class="freno-label" for="name">Full Name</label>
            <input
              class="freno-input"
              id="name"
              type="text"
              placeholder="John Doe"
              value={name()}
              onChange={(e) => setName(e.target.value)}
            />
          </div>

          <div class="freno-form-group">
            <label class="freno-label" for="email">Email</label>
            <input
              class="freno-input"
              id="email"
              type="email"
              placeholder="john@example.com"
              value={email()}
              onChange={(e) => setEmail(e.target.value)}
            />
          </div>

          <div class="freno-form-group">
            <label class="freno-label" for="password">Password</label>
            <input
              class="freno-input"
              id="password"
              type="password"
              placeholder="Create a strong password"
              minlength={8}
              pattern=".{8,}"
              required
              value={password()}
              onChange={(e) => setPassword(e.target.value)}
            />
          </div>

          <button class="freno-btn freno-btn-primary freno-btn-full" type="submit">
            Create Account
          </button>
        </form>

        <div class="freno-auth-divider">
          <span>or</span>
        </div>

        <div class="freno-auth-actions">
          <button 
            class="freno-btn freno-btn-outline freno-btn-full"
            type="button"
            onClick={() => {
              const clerk = getClerk();
              if (clerk) clerk.openSignIn();
            }}
          >
            Sign up with Google
          </button>
          <button 
            class="freno-btn freno-btn-outline freno-btn-full"
            type="button"
            onClick={() => {
              const clerk = getClerk();
              if (clerk) clerk.openSignIn();
            }}
          >
            Sign up with GitHub
          </button>
        </div>

        <div class="freno-auth-footer">
          <p>
            Already have an account?{' '}
            <a href="/sign-in">Sign in</a>
          </p>
        </div>
      </div>
    </div>
  );
};
