'use client'

import { useState } from 'react'
import { Search, UserPlus, Mail } from 'lucide-react'
import { toast } from 'sonner'
import { trpc } from '@/lib/trpc/client'
import { Button } from '@/components/ui/button'
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from '@/components/ui/dialog'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Slider } from '@/components/ui/slider'
import { Textarea } from '@/components/ui/textarea'
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from '@/components/ui/select'
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'

interface AddMemberDialogProps {
  juryGroupId: string
  open: boolean
  onOpenChange: (open: boolean) => void
}

export function AddMemberDialog({ juryGroupId, open, onOpenChange }: AddMemberDialogProps) {
  const [tab, setTab] = useState<'search' | 'invite'>('search')

  // Search existing user state
  const [searchQuery, setSearchQuery] = useState('')
  const [selectedUserId, setSelectedUserId] = useState<string>('')
  const [maxAssignments, setMaxAssignments] = useState<string>('')
  const [capMode, setCapMode] = useState<string>('')
  const [role, setRole] = useState<string>('MEMBER')
  const [startupRatio, setStartupRatio] = useState<number | null>(null)
  const [availabilityNotes, setAvailabilityNotes] = useState('')

  // Invite new user state
  const [inviteName, setInviteName] = useState('')
  const [inviteEmail, setInviteEmail] = useState('')
  const [inviteMaxAssignments, setInviteMaxAssignments] = useState<string>('')
  const [inviteCapMode, setInviteCapMode] = useState<string>('')
  const [inviteRole, setInviteRole] = useState<string>('MEMBER')
  const [inviteStartupRatio, setInviteStartupRatio] = useState<number | null>(null)
  const [inviteAvailabilityNotes, setInviteAvailabilityNotes] = useState('')
  const [inviteExpertise, setInviteExpertise] = useState('')

  const utils = trpc.useUtils()

  const { data: userResponse, isLoading: isSearching } = trpc.user.list.useQuery(
    { search: searchQuery, perPage: 20 },
    { enabled: searchQuery.length > 0 }
  )

  const users = userResponse?.users || []

  const { mutate: addMember, isPending: isAdding } = trpc.juryGroup.addMember.useMutation({
    onSuccess: () => {
      utils.juryGroup.getById.invalidate({ id: juryGroupId })
      toast.success('Member added successfully')
      onOpenChange(false)
      resetForm()
    },
    onError: (err) => {
      toast.error(err.message)
    },
  })

  const { mutate: createUser, isPending: isCreating } = trpc.user.create.useMutation({
    onSuccess: (newUser) => {
      // Immediately add the newly created user to the jury group
      addMember({
        juryGroupId,
        userId: newUser.id,
        role: inviteRole as 'CHAIR' | 'MEMBER' | 'OBSERVER',
        maxAssignmentsOverride: inviteMaxAssignments ? parseInt(inviteMaxAssignments, 10) : null,
        capModeOverride: inviteCapMode && inviteCapMode !== 'DEFAULT' ? (inviteCapMode as 'HARD' | 'SOFT' | 'NONE') : null,
        preferredStartupRatio: inviteStartupRatio,
        availabilityNotes: inviteAvailabilityNotes.trim() || null,
      })
      // Send invitation email
      sendInvitation({ userId: newUser.id, juryGroupId })
    },
    onError: (err) => {
      toast.error(err.message)
    },
  })

  const { mutate: sendInvitation } = trpc.user.sendInvitation.useMutation({
    onSuccess: (result) => {
      toast.success(`Invitation sent to ${result.email}`)
      utils.user.list.invalidate()
    },
    onError: (err) => {
      // Don't block — user was created and added, just invitation failed
      toast.error(`Member added but invitation email failed: ${err.message}`)
    },
  })

  const resetForm = () => {
    setSearchQuery('')
    setSelectedUserId('')
    setMaxAssignments('')
    setCapMode('')
    setRole('MEMBER')
    setStartupRatio(null)
    setAvailabilityNotes('')
    setInviteName('')
    setInviteEmail('')
    setInviteMaxAssignments('')
    setInviteCapMode('')
    setInviteRole('MEMBER')
    setInviteStartupRatio(null)
    setInviteAvailabilityNotes('')
    setInviteExpertise('')
  }

  const handleSearchSubmit = (e: React.FormEvent) => {
    e.preventDefault()

    if (!selectedUserId) {
      toast.error('Please select a user')
      return
    }

    addMember({
      juryGroupId,
      userId: selectedUserId,
      role: role as 'CHAIR' | 'MEMBER' | 'OBSERVER',
      maxAssignmentsOverride: maxAssignments ? parseInt(maxAssignments, 10) : null,
      capModeOverride: capMode && capMode !== 'DEFAULT' ? (capMode as 'HARD' | 'SOFT' | 'NONE') : null,
      preferredStartupRatio: startupRatio,
      availabilityNotes: availabilityNotes.trim() || null,
    })
  }

  const handleInviteSubmit = (e: React.FormEvent) => {
    e.preventDefault()

    if (!inviteEmail.trim()) {
      toast.error('Please enter an email address')
      return
    }

    const expertiseTags = inviteExpertise
      .split(',')
      .map((t) => t.trim())
      .filter(Boolean)

    createUser({
      email: inviteEmail.trim(),
      name: inviteName.trim() || undefined,
      role: 'JURY_MEMBER',
      expertiseTags: expertiseTags.length > 0 ? expertiseTags : undefined,
      maxAssignments: inviteMaxAssignments ? parseInt(inviteMaxAssignments, 10) : undefined,
    })
  }

  const isPending = isAdding || isCreating

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="sm:max-w-lg">
        <DialogHeader>
          <DialogTitle>Add Member to Jury Group</DialogTitle>
          <DialogDescription>
            Search for an existing user or invite a new juror to the platform
          </DialogDescription>
        </DialogHeader>

        <Tabs value={tab} onValueChange={(v) => setTab(v as 'search' | 'invite')}>
          <TabsList className="grid w-full grid-cols-2">
            <TabsTrigger value="search" className="flex items-center gap-2">
              <Search className="h-3.5 w-3.5" />
              Search Existing
            </TabsTrigger>
            <TabsTrigger value="invite" className="flex items-center gap-2">
              <Mail className="h-3.5 w-3.5" />
              Invite New
            </TabsTrigger>
          </TabsList>

          {/* Search existing user tab */}
          <TabsContent value="search">
            <form onSubmit={handleSearchSubmit} className="space-y-4 pt-2">
              <div className="space-y-2">
                <Label htmlFor="search">Search User</Label>
                <div className="relative">
                  <Search className="absolute left-3 top-3 h-4 w-4 text-muted-foreground" />
                  <Input
                    id="search"
                    placeholder="Search by name or email..."
                    className="pl-9"
                    value={searchQuery}
                    onChange={(e) => setSearchQuery(e.target.value)}
                  />
                </div>
                {isSearching && (
                  <p className="text-sm text-muted-foreground">Searching...</p>
                )}
                {users && users.length > 0 && (
                  <div className="max-h-40 overflow-y-auto border rounded-md">
                    {users.map((user) => (
                      <button
                        key={user.id}
                        type="button"
                        className={`w-full px-3 py-2 text-left text-sm hover:bg-accent ${
                          selectedUserId === user.id ? 'bg-accent' : ''
                        }`}
                        onClick={() => {
                          setSelectedUserId(user.id)
                          setSearchQuery(user.email)
                        }}
                      >
                        <div className="font-medium">{user.name || 'Unnamed User'}</div>
                        <div className="text-muted-foreground text-xs">{user.email}</div>
                      </button>
                    ))}
                  </div>
                )}
              </div>

              <div className="grid grid-cols-2 gap-3">
                <div className="space-y-2">
                  <Label htmlFor="role">Role</Label>
                  <Select value={role} onValueChange={setRole}>
                    <SelectTrigger id="role">
                      <SelectValue />
                    </SelectTrigger>
                    <SelectContent>
                      <SelectItem value="MEMBER">Member</SelectItem>
                      <SelectItem value="CHAIR">Chair</SelectItem>
                      <SelectItem value="OBSERVER">Observer</SelectItem>
                    </SelectContent>
                  </Select>
                </div>
                <div className="space-y-2">
                  <Label htmlFor="capMode">Cap Mode</Label>
                  <Select value={capMode || 'DEFAULT'} onValueChange={setCapMode}>
                    <SelectTrigger id="capMode">
                      <SelectValue />
                    </SelectTrigger>
                    <SelectContent>
                      <SelectItem value="DEFAULT">Group Default</SelectItem>
                      <SelectItem value="HARD">Hard Cap</SelectItem>
                      <SelectItem value="SOFT">Soft Cap</SelectItem>
                      <SelectItem value="NONE">No Cap</SelectItem>
                    </SelectContent>
                  </Select>
                </div>
              </div>

              <div className="space-y-2">
                <Label htmlFor="maxAssignments">Max Assignments Override (optional)</Label>
                <Input
                  id="maxAssignments"
                  type="number"
                  min="1"
                  placeholder="Leave empty to use group default"
                  value={maxAssignments}
                  onChange={(e) => setMaxAssignments(e.target.value)}
                />
              </div>

              <div className="space-y-2">
                <Label>Category Preference</Label>
                <div className="flex items-center gap-3">
                  <span className="text-xs text-muted-foreground w-20 shrink-0">Startup</span>
                  <Slider
                    value={[startupRatio !== null ? startupRatio * 100 : 50]}
                    onValueChange={([v]) => setStartupRatio(v / 100)}
                    min={0}
                    max={100}
                    step={10}
                    className="flex-1"
                  />
                  <span className="text-xs text-muted-foreground w-20 shrink-0 text-right">Concept</span>
                </div>
                <p className="text-xs text-muted-foreground">
                  {startupRatio !== null
                    ? `~${Math.round(startupRatio * 100)}% startups / ~${Math.round((1 - startupRatio) * 100)}% concepts`
                    : 'No preference set (balanced distribution)'}
                </p>
              </div>

              <div className="space-y-2">
                <Label htmlFor="availabilityNotes">Availability Notes (optional)</Label>
                <Textarea
                  id="availabilityNotes"
                  placeholder="e.g. Available only in March, limited to 5 reviews/week..."
                  rows={2}
                  value={availabilityNotes}
                  onChange={(e) => setAvailabilityNotes(e.target.value)}
                />
              </div>

              <DialogFooter>
                <Button type="button" variant="outline" onClick={() => onOpenChange(false)}>
                  Cancel
                </Button>
                <Button type="submit" disabled={isPending || !selectedUserId}>
                  {isAdding ? 'Adding...' : 'Add Member'}
                </Button>
              </DialogFooter>
            </form>
          </TabsContent>

          {/* Invite new user tab */}
          <TabsContent value="invite">
            <form onSubmit={handleInviteSubmit} className="space-y-4 pt-2">
              <div className="grid grid-cols-2 gap-3">
                <div className="space-y-2">
                  <Label htmlFor="inviteName">Full Name</Label>
                  <Input
                    id="inviteName"
                    placeholder="Jane Doe"
                    value={inviteName}
                    onChange={(e) => setInviteName(e.target.value)}
                  />
                </div>

                <div className="space-y-2">
                  <Label htmlFor="inviteEmail">
                    Email <span className="text-destructive">*</span>
                  </Label>
                  <Input
                    id="inviteEmail"
                    type="email"
                    placeholder="jane@example.com"
                    required
                    value={inviteEmail}
                    onChange={(e) => setInviteEmail(e.target.value)}
                  />
                </div>
              </div>

              <div className="grid grid-cols-2 gap-3">
                <div className="space-y-2">
                  <Label htmlFor="inviteRole">Role</Label>
                  <Select value={inviteRole} onValueChange={setInviteRole}>
                    <SelectTrigger id="inviteRole">
                      <SelectValue />
                    </SelectTrigger>
                    <SelectContent>
                      <SelectItem value="MEMBER">Member</SelectItem>
                      <SelectItem value="CHAIR">Chair</SelectItem>
                      <SelectItem value="OBSERVER">Observer</SelectItem>
                    </SelectContent>
                  </Select>
                </div>
                <div className="space-y-2">
                  <Label htmlFor="inviteCapMode">Cap Mode</Label>
                  <Select value={inviteCapMode || 'DEFAULT'} onValueChange={setInviteCapMode}>
                    <SelectTrigger id="inviteCapMode">
                      <SelectValue />
                    </SelectTrigger>
                    <SelectContent>
                      <SelectItem value="DEFAULT">Group Default</SelectItem>
                      <SelectItem value="HARD">Hard Cap</SelectItem>
                      <SelectItem value="SOFT">Soft Cap</SelectItem>
                      <SelectItem value="NONE">No Cap</SelectItem>
                    </SelectContent>
                  </Select>
                </div>
              </div>

              <div className="space-y-2">
                <Label htmlFor="inviteMaxAssignments">Max Assignments Override (optional)</Label>
                <Input
                  id="inviteMaxAssignments"
                  type="number"
                  min="1"
                  placeholder="Leave empty to use group default"
                  value={inviteMaxAssignments}
                  onChange={(e) => setInviteMaxAssignments(e.target.value)}
                />
              </div>

              <div className="space-y-2">
                <Label>Category Preference</Label>
                <div className="flex items-center gap-3">
                  <span className="text-xs text-muted-foreground w-20 shrink-0">Startup</span>
                  <Slider
                    value={[inviteStartupRatio !== null ? inviteStartupRatio * 100 : 50]}
                    onValueChange={([v]) => setInviteStartupRatio(v / 100)}
                    min={0}
                    max={100}
                    step={10}
                    className="flex-1"
                  />
                  <span className="text-xs text-muted-foreground w-20 shrink-0 text-right">Concept</span>
                </div>
                <p className="text-xs text-muted-foreground">
                  {inviteStartupRatio !== null
                    ? `~${Math.round(inviteStartupRatio * 100)}% startups / ~${Math.round((1 - inviteStartupRatio) * 100)}% concepts`
                    : 'No preference set (balanced distribution)'}
                </p>
              </div>

              <div className="space-y-2">
                <Label htmlFor="inviteAvailabilityNotes">Availability Notes (optional)</Label>
                <Textarea
                  id="inviteAvailabilityNotes"
                  placeholder="e.g. Available only in March, limited to 5 reviews/week..."
                  rows={2}
                  value={inviteAvailabilityNotes}
                  onChange={(e) => setInviteAvailabilityNotes(e.target.value)}
                />
              </div>

              <div className="space-y-2">
                <Label htmlFor="inviteExpertise">Expertise Tags (optional)</Label>
                <Input
                  id="inviteExpertise"
                  placeholder="marine biology, policy, finance"
                  value={inviteExpertise}
                  onChange={(e) => setInviteExpertise(e.target.value)}
                />
                <p className="text-xs text-muted-foreground">
                  Comma-separated tags for smart assignment matching
                </p>
              </div>

              <div className="rounded-md border border-blue-200 bg-blue-50 px-3 py-2">
                <p className="text-xs text-blue-700">
                  <UserPlus className="mr-1 inline h-3 w-3" />
                  This will create a new user account and send an invitation email to join the platform as a jury member.
                </p>
              </div>

              <DialogFooter>
                <Button type="button" variant="outline" onClick={() => onOpenChange(false)}>
                  Cancel
                </Button>
                <Button type="submit" disabled={isPending || !inviteEmail.trim()}>
                  {isCreating || isAdding ? 'Creating & Inviting...' : 'Create & Invite'}
                </Button>
              </DialogFooter>
            </form>
          </TabsContent>
        </Tabs>
      </DialogContent>
    </Dialog>
  )
}
