"use client"

import React, { useState } from "react"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"
import { Checkbox } from "@/components/ui/checkbox"
import { useAuth } from "@/lib/auth-context"
import { type UserRole } from "@/lib/types"
import { Mail, Lock, User, UserPlus, AlertCircle, Building2 } from "lucide-react"

interface RegisterFormProps {
  onSwitchToLogin: () => void
}

export function RegisterForm({ onSwitchToLogin }: RegisterFormProps) {
  // Access authentication context for registration functionality and room data
  const { register, rooms } = useAuth()
  // State variables for form inputs and UI feedback
  const [firstname, setFirstname] = useState("")
  const [lastname, setLastname] = useState("")
  const [email, setEmail] = useState("")
  const [password, setPassword] = useState("")
  const [role, setRole] = useState<UserRole>("LEHRKRAFT") // Default role for new users
  const [selectedRoomIds, setSelectedRoomIds] = useState<number[]>([]) // Stores IDs of rooms for 'RAUMBETREUER' role
  const [error, setError] = useState("") // Stores error messages
  const [isLoading, setIsLoading] = useState(false) // Tracks loading state for the form
  const [privacyAccepted, setPrivacyAccepted] = useState(false) // Tracks privacy policy acceptance

  // Handles toggling selection of supervised rooms for 'RAUMBETREUER'
  const handleRoomToggle = (roomId: number) => {
    setSelectedRoomIds((prev) => (prev.includes(roomId) ? prev.filter((r) => r !== roomId) : [...prev, roomId]))
  }

  // Handles form submission for user registration
  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault() // Prevent default form submission behavior
    setError("") // Clear previous errors

    // Validate if a 'RAUMBETREUER' has selected at least one room
    if (role === "RAUMBETREUER" && selectedRoomIds.length === 0) {
      setError("Bitte wählen Sie mindestens einen Raum aus")
      return
    }

    if (!privacyAccepted) {
      setError("Bitte akzeptieren Sie die Datenschutzerklärung")
      return
    }

    setIsLoading(true) // Show loading indicator

    // Attempt to register the new user with provided details
    const success = await register(
      {
        firstname,
        lastname,
        email,
        role,
        roomIds: role === "RAUMBETREUER" ? selectedRoomIds : undefined, // Only send room IDs if role is 'RAUMBETREUER'
        password
      }
    )

    // Handle registration success or failure
    if (!success) {
      setError("Registrierung fehlgeschlagen")
    }
    if (success) {
      onSwitchToLogin() // Switch to login form upon successful registration
    }
    setIsLoading(false) // Hide loading indicator
  }

  return (
    <Card className="w-full max-w-md border-border/60 shadow-lg">
      <CardHeader className="space-y-1 pb-6">
        <CardTitle className="text-2xl font-semibold tracking-tight">Konto erstellen</CardTitle>
        <CardDescription className="text-muted-foreground">Geben Sie Ihre Daten ein</CardDescription>
      </CardHeader>
      <form onSubmit={handleSubmit}>
        <CardContent className="space-y-4">
          {/* Display error message if present */}
          {error && (
            <div className="flex items-center gap-2 rounded-lg bg-destructive/10 p-3 text-sm text-destructive">
              <AlertCircle className="h-4 w-4" />
              {error}
            </div>
          )}

          {/* First Name and Last Name inputs */}
          <div className="grid grid-cols-2 gap-4">
            <div className="space-y-2">
              <Label htmlFor="firstname" className="text-sm font-medium">
                Vorname
              </Label>
              <div className="relative">
                <User className="absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />
                <Input
                  id="firstname"
                  type="text"
                  placeholder="Max"
                  value={firstname}
                  onChange={(e) => setFirstname(e.target.value)}
                  className="pl-10"
                  required
                />
              </div>
            </div>
            <div className="space-y-2">
              <Label htmlFor="lastname" className="text-sm font-medium">
                Nachname
              </Label>
              <div className="relative">
                <User className="absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />
                <Input
                  id="lastname"
                  type="text"
                  placeholder="Mustermann"
                  value={lastname}
                  onChange={(e) => setLastname(e.target.value)}
                  className="pl-10"
                  required
                />
              </div>
            </div>
          </div>

          {/* Email input field */}
          <div className="space-y-2">
            <Label htmlFor="reg-email" className="text-sm font-medium">
              E-Mail
            </Label>
            <div className="relative">
              <Mail className="absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />
              <Input
                id="reg-email"
                type="email"
                placeholder="name@schule.de"
                value={email}
                onChange={(e) => setEmail(e.target.value)}
                className="pl-10"
                required
              />
            </div>
          </div>

          {/* Password input field */}
          <div className="space-y-2">
            <Label htmlFor="reg-password" className="text-sm font-medium">
              Kennwort
            </Label>
            <div className="relative">
              <Lock className="absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />
              <Input
                id="reg-password"
                type="password"
                placeholder="Kennwort erstellen"
                value={password}
                onChange={(e) => setPassword(e.target.value)}
                className="pl-10"
                required
                minLength={4}
              />
            </div>
          </div>

          {/* Role selection */}
          <div className="space-y-3">
            <Label className="text-sm font-medium">Rolle</Label>
            <div className="grid grid-cols-2 gap-3">
              <button
                type="button"
                onClick={() => setRole("LEHRKRAFT")}
                className={`flex flex-col items-center gap-2 rounded-lg border-2 p-4 transition-all ${role === "LEHRKRAFT"
                  ? "border-primary bg-primary/5"
                  : "border-border hover:border-primary/50 hover:bg-muted/50"
                  }`}
              >
                <User className={`h-5 w-5 ${role === "LEHRKRAFT" ? "text-primary" : "text-muted-foreground"}`} />
                <span className={`text-sm font-medium ${role === "LEHRKRAFT" ? "text-primary" : "text-foreground"}`}>
                  Lehrkraft
                </span>
              </button>
              <button
                type="button"
                onClick={() => setRole("RAUMBETREUER")}
                className={`flex flex-col items-center gap-2 rounded-lg border-2 p-4 transition-all ${role === "RAUMBETREUER"
                  ? "border-primary bg-primary/5"
                  : "border-border hover:border-primary/50 hover:bg-muted/50"
                  }`}
              >
                <Building2
                  className={`h-5 w-5 ${role === "RAUMBETREUER" ? "text-primary" : "text-muted-foreground"}`}
                />
                <span className={`text-sm font-medium ${role === "RAUMBETREUER" ? "text-primary" : "text-foreground"}`}>
                  Raumbetreuer

                </span>
              </button>
            </div>
          </div>

          {/* Privacy Policy Checkbox - ADDED */}
          <div className="flex items-start space-x-3 pt-2">
            <Checkbox
              id="privacy"
              checked={privacyAccepted}
              onCheckedChange={(checked) => setPrivacyAccepted(checked as boolean)}
            />
            <div className="grid gap-1.5 leading-none">
              <label
                htmlFor="privacy"
                className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70 cursor-pointer"
              >
                Ich akzeptiere die{" "}
                <a
                  href="/datenschutz"
                  target="_blank"
                  rel="noopener noreferrer"
                  className="font-medium text-primary underline-offset-4 hover:underline"
                >
                  Datenschutzerklärung
                </a>
              </label>
              <p className="text-xs text-muted-foreground">
                Die Zustimmung ist erforderlich für die Registrierung.
              </p>
            </div>
          </div>

          {role === "RAUMBETREUER" && (
            <div className="space-y-3">
              <Label className="text-sm font-medium">Betreute Räume</Label>
              <div className="max-h-48 space-y-2 overflow-y-auto rounded-lg border border-border p-3">
                {rooms.map((room) => (
                  <div key={room.id} className="flex items-center space-x-3">
                    <Checkbox
                      id={String(room.id)}
                      checked={selectedRoomIds.includes(room.id)}
                      onCheckedChange={() => handleRoomToggle(room.id)}
                    />
                    <label
                      htmlFor={String(room.id)}
                      className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70 cursor-pointer"
                    >
                      {room.name}
                    </label>
                  </div>
                ))}
              </div>
              {selectedRoomIds.length > 0 && (
                <p className="text-sm text-muted-foreground">
                  {selectedRoomIds.length} Raum/Räume ausgewählt
                </p>
              )}
            </div>
          )}

        </CardContent>
        <CardFooter className="flex flex-col gap-4 pt-2">
          {/* Submit button with loading state */}
          <Button type="submit" className="w-full" disabled={isLoading}>
            {isLoading ? (
              <span className="flex items-center gap-2">
                <span className="h-4 w-4 animate-spin rounded-full border-2 border-primary-foreground border-t-transparent" />
                Registrierung läuft...
              </span>
            ) : (
              <span className="flex items-center gap-2">
                <UserPlus className="h-4 w-4" />
                Registrieren
              </span>
            )}
          </Button>
          {/* Link to switch to login form */}
          <p className="text-sm text-muted-foreground">
            Bereits registriert?{" "}
            <button
              type="button"
              onClick={onSwitchToLogin}
              className="font-medium text-primary underline-offset-4 hover:underline"
            >
              Anmelden
            </button>
          </p>
        </CardFooter>
      </form>
    </Card>
  )
}
