import { useState, useEffect } from 'react';
import { useNavigate, useLocation } from 'react-router-dom';
import { FaEnvelopeOpenText } from 'react-icons/fa';

import styles from './Register.module.css';
import { useRegister, useMsalRegister, useUsernameAvailability } from '../hooks/useAuthentication';
import { generateAndStoreCSRFToken } from '../utils/csrfUtils';
import { PENDING_INVITATION_KEY } from './InvitePage';

interface RegisterFormData {
  username: string;
  email: string;
  fullName: string;
}

function Register() {
  const navigate = useNavigate();
  const location = useLocation();
  const { register, error: registerError, isLoading } = useRegister();
  const { error: msalError } = useMsalRegister();
  const { checkAvailability, isChecking, error: availabilityError } = useUsernameAvailability();
  const [formData, setFormData] = useState<RegisterFormData>({
    username: '',
    email: '',
    fullName: ''
  });
  const [validationError, setValidationError] = useState<string | null>(null);
  const [successMessage, setSuccessMessage] = useState<string | null>(null);
  const [usernameFocused, setUsernameFocused] = useState(false);
  const [emailFocused, setEmailFocused] = useState(false);
  const [fullNameFocused, setFullNameFocused] = useState(false);
  const [usernameHighlight, setUsernameHighlight] = useState(false);

  // Check for pending invitation
  const pendingInvitationToken = localStorage.getItem(PENDING_INVITATION_KEY);
  const hasPendingInvitation = !!pendingInvitationToken;

  // Set page title and generate CSRF token
  useEffect(() => {
    document.title = "PowerOn AI Platform - Registrieren";
    
    // Generate CSRF token for new security implementation
    generateAndStoreCSRFToken();
  }, []);

  const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const { name, value } = e.target;
    setFormData(prev => ({
      ...prev,
      [name]: value
    }));
    setValidationError(null);
    // Reset username highlight when user starts typing in username field
    if (name === 'username') {
      setUsernameHighlight(false);
    }
  };

  const validateForm = (): boolean => {
    if (!formData.username || !formData.email || !formData.fullName) {
      setValidationError('Bitte füllen Sie alle Pflichtfelder aus.');
      return false;
    }

    if (!formData.email.includes('@')) {
      setValidationError('Bitte geben Sie eine gültige E-Mail-Adresse ein.');
      return false;
    }

    return true;
  };

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

    try {
      // First check username availability
      const availabilityResult = await checkAvailability(formData.username, 'local');
      
      if (!availabilityResult.available) {
        // Check if the error message is about username being taken
        const errorMessage = availabilityResult.message || 'Username is not available';
        if (errorMessage === 'Username is already taken') {
          setValidationError('Benutzername ist bereits vergeben');
          setUsernameHighlight(true);
        } else {
          setValidationError('Benutzername ist nicht verfügbar');
        }
        return;
      }

      // Username is available, proceed with registration (no password - magic link flow)
      await register(formData);
      
      // Build success message
      let message = 'Registrierung erfolgreich! Bitte prüfen Sie Ihre E-Mail (auch den Spam-Ordner) für den Link zum Setzen Ihres Passworts.';
      if (hasPendingInvitation) {
        message += ' Nach dem Setzen Ihres Passworts können Sie sich anmelden und Ihre Einladung annehmen.';
      }
      
      // Show success message instead of immediate redirect
      setSuccessMessage(message);
      
      // Redirect to login page after delay
      setTimeout(() => {
        navigate('/login', { 
          state: { 
            registered: true,
            message: 'Registrierung erfolgreich. Bitte prüfen Sie Ihre E-Mail für den Passwort-Link.',
            // Pass along invitation state
            ...(location.state || {})
          } 
        });
      }, 5000);
    } catch (err) {
      console.error('Registration failed:', err);
    }
  };

  // Helper function to safely get error message
  const getErrorMessage = () => {
    if (validationError) return validationError;
    if (registerError) return typeof registerError === 'string' ? registerError : 'Registration failed';
    if (msalError) return typeof msalError === 'string' ? msalError : 'Microsoft registration failed';
    if (availabilityError) return typeof availabilityError === 'string' ? availabilityError : 'Username availability check failed';
    return null;
  };

  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}>
            <div className={styles.loginForm}>
              {/* Pending invitation notice */}
              {hasPendingInvitation && !successMessage && (
                <div className={styles.invitationNotice}>
                  <FaEnvelopeOpenText className={styles.invitationIcon} />
                  <span>Sie haben eine ausstehende Einladung. Nach der Registrierung und Anmeldung können Sie diese annehmen.</span>
                </div>
              )}
              
              {getErrorMessage() && (
                <div className={styles.error}>{getErrorMessage()}</div>
              )}
              
              {successMessage && (
                <div className={styles.success}>{successMessage}</div>
              )}
              
              {!successMessage && (
                <>
                  <div className={styles.floatingLabelInput}>
                    <input
                      type="text"
                      name="username"
                      placeholder=" "
                      value={formData.username}
                      onChange={handleInputChange}
                      onFocus={() => setUsernameFocused(true)}
                      onBlur={() => setUsernameFocused(false)}
                      className={`${styles.input} ${usernameFocused || formData.username ? styles.focused : ''} ${usernameHighlight ? styles.usernameError : ''}`}
                    />
                    <label className={usernameFocused || formData.username ? styles.focusedLabel : styles.label}>Benutzername</label>
                  </div>

                  <div className={styles.floatingLabelInput}>
                    <input
                      type="email"
                      name="email"
                      placeholder=" "
                      value={formData.email}
                      onChange={handleInputChange}
                      onFocus={() => setEmailFocused(true)}
                      onBlur={() => setEmailFocused(false)}
                      className={`${styles.input} ${emailFocused || formData.email ? styles.focused : ''}`}
                    />
                    <label className={emailFocused || formData.email ? styles.focusedLabel : styles.label}>E-Mail</label>
                  </div>

                  <div className={styles.floatingLabelInput}>
                    <input
                      type="text"
                      name="fullName"
                      placeholder=" "
                      value={formData.fullName}
                      onChange={handleInputChange}
                      onFocus={() => setFullNameFocused(true)}
                      onBlur={() => setFullNameFocused(false)}
                      className={`${styles.input} ${fullNameFocused || formData.fullName ? styles.focused : ''}`}
                    />
                    <label className={fullNameFocused || formData.fullName ? styles.focusedLabel : styles.label}>Vollständiger Name</label>
                  </div>

                  <div className={styles.infoMessage}>
                    <p>Nach der Registrierung erhalten Sie eine E-Mail mit einem Link zum Setzen Ihres Passworts.</p>
                  </div>

                  <div className={styles.disclaimer}>
                    <p>
                      Mit der Registrierung stimmen Sie unseren Datenschutzbestimmungen zur KI-Nutzung zu.
                    </p>
                  </div>

                  <button 
                    className={`${styles.button} ${styles.loginButton}`}
                    onClick={handleSubmit}
                    disabled={isLoading || isChecking}
                  >
                    {isLoading ? "Registrierung läuft..." : isChecking ? "Benutzername wird geprüft..." : "Registrieren"}
                  </button>
                </>
              )}

              <div className={styles.registerLink}>
                <span>Bereits registriert?</span>
                <button 
                  className={styles.textButton}
                  onClick={() => navigate("/login", { state: location.state })}
                >
                  Jetzt anmelden
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}

export default Register;
