import { useState, useEffect } from 'react';
import { useNavigate, useSearchParams } from 'react-router-dom';

import styles from './Reset.module.css';
import { usePasswordReset } from '../hooks/useAuthentication';
import { generateAndStoreCSRFToken } from '../utils/csrfUtils';

function Reset() {
  const navigate = useNavigate();
  const [searchParams] = useSearchParams();
  const { resetPassword, isLoading, error } = usePasswordReset();
  
  const [password, setPassword] = useState('');
  const [confirmPassword, setConfirmPassword] = useState('');
  const [passwordFocused, setPasswordFocused] = useState(false);
  const [confirmPasswordFocused, setConfirmPasswordFocused] = useState(false);
  const [validationError, setValidationError] = useState<string | null>(null);
  const [successMessage, setSuccessMessage] = useState<string | null>(null);
  const [tokenError, setTokenError] = useState<string | null>(null);
  
  // Get token from URL
  const token = searchParams.get('token');

  // Set page title and generate CSRF token
  useEffect(() => {
    document.title = "PowerOn AI Platform - Neues Passwort setzen";
    generateAndStoreCSRFToken();
    
    // Validate token exists and format
    if (!token) {
      setTokenError('Ungültiger Reset-Link. Bitte fordern Sie einen neuen Link an.');
    } else if (!_isValidUUID(token)) {
      setTokenError('Ungültiger Reset-Link. Bitte fordern Sie einen neuen Link an.');
    }
  }, [token]);

  const _isValidUUID = (str: string): boolean => {
    const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
    return uuidRegex.test(str);
  };

  const validateForm = (): boolean => {
    if (!password || password.length < 8) {
      setValidationError('Passwort muss mindestens 8 Zeichen lang sein.');
      return false;
    }

    if (password !== confirmPassword) {
      setValidationError('Die Passwörter stimmen nicht überein.');
      return false;
    }

    return true;
  };

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    setValidationError(null);
    
    if (!validateForm()) {
      return;
    }

    if (!token) {
      setValidationError('Token fehlt. Bitte fordern Sie einen neuen Reset-Link an.');
      return;
    }

    try {
      await resetPassword(token, password);
      setSuccessMessage('Passwort erfolgreich gesetzt! Sie werden zum Login weitergeleitet...');
      
      // Redirect to login after delay
      setTimeout(() => {
        navigate('/login', { 
          state: { 
            passwordReset: true,
            message: 'Passwort erfolgreich geändert. Bitte melden Sie sich an.' 
          } 
        });
      }, 3000);
    } catch (err: any) {
      // Error is already set by the hook
      const errorMessage = err?.response?.data?.detail || err?.message || 'Passwort-Zurücksetzung fehlgeschlagen.';
      if (errorMessage.includes('abgelaufen') || errorMessage.includes('expired') || errorMessage.includes('Ungültig') || errorMessage.includes('invalid')) {
        setValidationError('Der Reset-Link ist ungültig oder abgelaufen. Bitte fordern Sie einen neuen Link an.');
      } else {
        setValidationError(errorMessage);
      }
    }
  };

  // Show token error if invalid
  if (tokenError) {
    return (
      <div className={styles.container}>
        <div className={styles.mainContent}>
          <div className={styles.logo}>
            <div className={styles.logoText}>
              <span className={styles.logoPower}>Power</span>
              <span className={styles.logoOn}>On</span>
            </div>
          </div>
          <div className={styles.loginSection}>
            <div className={styles.loginBox}>
              <h2 className={styles.title}>Neues Passwort setzen</h2>
              <div className={styles.loginForm}>
                <div className={styles.error}>{tokenError}</div>
                <div className={styles.registerLink}>
                  <button 
                    className={styles.textButton}
                    onClick={() => navigate("/password-reset-request")}
                  >
                    Neuen Reset-Link anfordern
                  </button>
                </div>
                <div className={styles.registerLink}>
                  <span>oder zurück zum</span>
                  <button 
                    className={styles.textButton}
                    onClick={() => navigate("/login")}
                  >
                    Login
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    );
  }

  return (
    <div className={styles.container}>
      <div className={styles.mainContent}>
        <div className={styles.logo}>
          <div className={styles.logoText}>
            <span className={styles.logoPower}>Power</span>
            <span className={styles.logoOn}>On</span>
          </div>
        </div>
        <div className={styles.loginSection}>
          <div className={styles.loginBox}>
            <h2 className={styles.title}>Neues Passwort setzen</h2>
            <div className={styles.loginForm}>
              {(validationError || error) && (
                <div className={styles.error}>{validationError || error}</div>
              )}
              
              {successMessage && (
                <div className={styles.success}>{successMessage}</div>
              )}
              
              {!successMessage && (
                <form onSubmit={handleSubmit}>
                  <div className={styles.floatingLabelInput}>
                    <input
                      type="password"
                      placeholder=" "
                      value={password}
                      onChange={(e) => {
                        setPassword(e.target.value);
                        setValidationError(null);
                      }}
                      onFocus={() => setPasswordFocused(true)}
                      onBlur={() => setPasswordFocused(false)}
                      className={`${styles.input} ${passwordFocused || password ? styles.focused : ''}`}
                      autoComplete="new-password"
                    />
                    <label className={passwordFocused || password ? styles.focusedLabel : styles.label}>Neues Passwort</label>
                  </div>
                  <div className={styles.passwordHint}>Mindestens 8 Zeichen</div>

                  <div className={styles.floatingLabelInput}>
                    <input
                      type="password"
                      placeholder=" "
                      value={confirmPassword}
                      onChange={(e) => {
                        setConfirmPassword(e.target.value);
                        setValidationError(null);
                      }}
                      onFocus={() => setConfirmPasswordFocused(true)}
                      onBlur={() => setConfirmPasswordFocused(false)}
                      className={`${styles.input} ${confirmPasswordFocused || confirmPassword ? styles.focused : ''}`}
                      autoComplete="new-password"
                    />
                    <label className={confirmPasswordFocused || confirmPassword ? styles.focusedLabel : styles.label}>Passwort bestätigen</label>
                  </div>

                  <button 
                    type="submit"
                    className={`${styles.button} ${styles.loginButton}`}
                    disabled={isLoading}
                  >
                    {isLoading ? "Wird gespeichert..." : "Passwort setzen"}
                  </button>
                </form>
              )}

              <div className={styles.registerLink}>
                <span>Zurück zum</span>
                <button 
                  className={styles.textButton}
                  onClick={() => navigate("/login")}
                >
                  Login
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}

export default Reset;
