import { useState, useMemo } from 'react'
import type { EncounterDetail, UpdateEncounterInput, CreateEncounterInput } from '../types'
import { useEvolutions, useForms } from '../hooks/useEncounters'
import { TypeBadge } from './TypeBadge'
import { formatEvolutionMethod } from '../utils/formatEvolution'

interface StatusChangeModalProps {
  encounter: EncounterDetail
  onUpdate: (data: { id: number; data: UpdateEncounterInput }) => void
  onClose: () => void
  isPending: boolean
  region?: string
  onCreateEncounter?: (data: CreateEncounterInput) => void
}

export function StatusChangeModal({
  encounter,
  onUpdate,
  onClose,
  isPending,
  region,
  onCreateEncounter,
}: StatusChangeModalProps) {
  const { pokemon, currentPokemon, route, nickname, catchLevel, faintLevel, deathCause } = encounter
  const isDead = faintLevel !== null
  const displayPokemon = currentPokemon ?? pokemon
  const [showConfirm, setShowConfirm] = useState(false)
  const [showEvolve, setShowEvolve] = useState(false)
  const [showFormChange, setShowFormChange] = useState(false)
  const [showShedConfirm, setShowShedConfirm] = useState(false)
  const [pendingEvolutionId, setPendingEvolutionId] = useState<number | null>(null)
  const [shedNickname, setShedNickname] = useState('')
  const [deathLevel, setDeathLevel] = useState('')
  const [cause, setCause] = useState('')

  const activePokemonId = currentPokemon?.id ?? pokemon.id
  const { data: evolutions, isLoading: evolutionsLoading } = useEvolutions(
    showEvolve || showShedConfirm ? activePokemonId : null,
    region
  )
  const { data: forms } = useForms(isDead ? null : activePokemonId)

  const { normalEvolutions, shedCompanion } = useMemo(() => {
    if (!evolutions) return { normalEvolutions: [], shedCompanion: null }
    return {
      normalEvolutions: evolutions.filter((e) => e.trigger !== 'shed'),
      shedCompanion: evolutions.find((e) => e.trigger === 'shed') ?? null,
    }
  }, [evolutions])

  const handleConfirmDeath = () => {
    onUpdate({
      id: encounter.id,
      data: {
        faintLevel: deathLevel ? Number(deathLevel) : undefined,
        deathCause: cause || undefined,
      },
    })
  }

  const handleEvolve = (toPokemonId: number) => {
    if (shedCompanion && onCreateEncounter) {
      setPendingEvolutionId(toPokemonId)
      setShowEvolve(false)
      setShowShedConfirm(true)
      return
    }
    onUpdate({
      id: encounter.id,
      data: { currentPokemonId: toPokemonId },
    })
  }

  const applyEvolution = (includeShed: boolean) => {
    if (pendingEvolutionId === null) return
    onUpdate({
      id: encounter.id,
      data: { currentPokemonId: pendingEvolutionId },
    })
    if (includeShed && shedCompanion && onCreateEncounter) {
      onCreateEncounter({
        routeId: encounter.routeId,
        pokemonId: shedCompanion.toPokemon.id,
        nickname: shedNickname || undefined,
        status: 'caught',
        origin: 'shed_evolution',
      })
    }
  }

  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center p-4">
      <div className="fixed inset-0 bg-black/50" onClick={onClose} />
      <div className="relative bg-surface-1 rounded-xl shadow-xl max-w-sm w-full">
        {/* Header */}
        <div className="flex items-center justify-between px-6 py-4 border-b border-border-default">
          <h2 className="text-lg font-semibold text-text-primary">
            {isDead ? 'Death Details' : 'Pokemon Status'}
          </h2>
          <button onClick={onClose} className="text-text-tertiary hover:text-text-primary">
            <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path
                strokeLinecap="round"
                strokeLinejoin="round"
                strokeWidth={2}
                d="M6 18L18 6M6 6l12 12"
              />
            </svg>
          </button>
        </div>

        <div className="px-6 py-4">
          {/* Pokemon info */}
          <div className="flex items-center gap-4 mb-4">
            {displayPokemon.spriteUrl ? (
              <img
                src={displayPokemon.spriteUrl}
                alt={displayPokemon.name}
                className={`w-16 h-16 ${isDead ? 'grayscale opacity-60' : ''}`}
              />
            ) : (
              <div className="w-16 h-16 rounded-full bg-surface-3 flex items-center justify-center text-xl font-bold text-text-secondary">
                {displayPokemon.name[0]?.toUpperCase()}
              </div>
            )}
            <div>
              <div className="font-semibold text-text-primary">
                {nickname || displayPokemon.name}
              </div>
              {nickname && (
                <div className="text-xs text-text-tertiary capitalize">{displayPokemon.name}</div>
              )}
              <div className="flex flex-col items-start gap-0.5 mt-1">
                {displayPokemon.types.map((type) => (
                  <TypeBadge key={type} type={type} />
                ))}
              </div>
              <div className="text-xs text-text-tertiary mt-1">
                Lv. {catchLevel ?? '?'} &middot; {route.name}
              </div>
              {currentPokemon && (
                <div className="text-[10px] text-text-muted mt-0.5">Originally: {pokemon.name}</div>
              )}
            </div>
          </div>

          {/* Dead pokemon: view-only details */}
          {isDead && (
            <div className="bg-status-failed-bg rounded-lg p-4 space-y-2">
              <div className="flex items-center gap-2 text-status-failed font-medium text-sm">
                <span className="w-2 h-2 rounded-full bg-red-500" />
                Deceased
              </div>
              {faintLevel !== null && (
                <div className="text-sm text-text-secondary">
                  <span className="text-text-tertiary">Level at death:</span> {faintLevel}
                </div>
              )}
              {deathCause && (
                <div className="text-sm text-text-secondary">
                  <span className="text-text-tertiary">Cause:</span> {deathCause}
                </div>
              )}
            </div>
          )}

          {/* Alive pokemon: actions */}
          {!isDead && !showConfirm && !showEvolve && !showFormChange && !showShedConfirm && (
            <div className="flex gap-3">
              <button
                type="button"
                onClick={() => setShowEvolve(true)}
                className="flex-1 px-4 py-2 bg-accent-600 text-white rounded-lg font-medium hover:bg-accent-500 transition-colors"
              >
                Evolve
              </button>
              {forms && forms.length > 0 && (
                <button
                  type="button"
                  onClick={() => setShowFormChange(true)}
                  className="flex-1 px-4 py-2 bg-purple-600 text-white rounded-lg font-medium hover:bg-purple-700 transition-colors"
                >
                  Change Form
                </button>
              )}
              <button
                type="button"
                onClick={() => setShowConfirm(true)}
                className="flex-1 px-4 py-2 bg-red-600 text-white rounded-lg font-medium hover:bg-red-700 transition-colors"
              >
                Mark as Dead
              </button>
            </div>
          )}

          {/* Evolution selection */}
          {!isDead && showEvolve && (
            <div className="space-y-3">
              <div className="flex items-center justify-between">
                <h3 className="text-sm font-medium text-text-secondary">Evolve into:</h3>
                <button
                  type="button"
                  onClick={() => setShowEvolve(false)}
                  className="text-xs text-text-tertiary hover:text-text-secondary"
                >
                  Back
                </button>
              </div>
              {evolutionsLoading && (
                <p className="text-sm text-text-tertiary">Loading evolutions...</p>
              )}
              {!evolutionsLoading && normalEvolutions.length === 0 && (
                <p className="text-sm text-text-tertiary">No evolutions available</p>
              )}
              {!evolutionsLoading && normalEvolutions.length > 0 && (
                <div className="space-y-2">
                  {normalEvolutions.map((evo) => (
                    <button
                      key={evo.id}
                      type="button"
                      disabled={isPending}
                      onClick={() => handleEvolve(evo.toPokemon.id)}
                      className="w-full flex items-center gap-3 p-3 rounded-lg border border-border-default hover:bg-accent-900/20 hover:border-accent-600 transition-colors disabled:opacity-50"
                    >
                      {evo.toPokemon.spriteUrl ? (
                        <img
                          src={evo.toPokemon.spriteUrl}
                          alt={evo.toPokemon.name}
                          className="w-10 h-10"
                        />
                      ) : (
                        <div className="w-10 h-10 rounded-full bg-surface-3 flex items-center justify-center text-sm font-bold text-text-secondary">
                          {evo.toPokemon.name[0]?.toUpperCase()}
                        </div>
                      )}
                      <div className="text-left">
                        <div className="font-medium text-text-primary text-sm">
                          {evo.toPokemon.name}
                        </div>
                        <div className="text-xs text-text-tertiary">
                          {formatEvolutionMethod(evo)}
                        </div>
                      </div>
                    </button>
                  ))}
                </div>
              )}
            </div>
          )}

          {/* Shed evolution confirmation (Nincada → Ninjask + Shedinja) */}
          {!isDead && showShedConfirm && shedCompanion && (
            <div className="space-y-3">
              <div className="flex items-center justify-between">
                <h3 className="text-sm font-medium text-text-secondary">Shed Evolution</h3>
                <button
                  type="button"
                  onClick={() => {
                    setShowShedConfirm(false)
                    setPendingEvolutionId(null)
                    setShedNickname('')
                    setShowEvolve(true)
                  }}
                  className="text-xs text-text-tertiary hover:text-text-secondary"
                >
                  Back
                </button>
              </div>
              <div className="bg-amber-900/20 border border-amber-700 rounded-lg p-3">
                <div className="flex items-center gap-3">
                  {shedCompanion.toPokemon.spriteUrl ? (
                    <img
                      src={shedCompanion.toPokemon.spriteUrl}
                      alt={shedCompanion.toPokemon.name}
                      className="w-12 h-12"
                    />
                  ) : (
                    <div className="w-12 h-12 rounded-full bg-surface-3 flex items-center justify-center text-sm font-bold text-text-secondary">
                      {shedCompanion.toPokemon.name[0]?.toUpperCase()}
                    </div>
                  )}
                  <p className="text-sm text-amber-300">
                    {displayPokemon.name} shed its shell! Would you also like to add{' '}
                    <span className="font-semibold">{shedCompanion.toPokemon.name}</span>?
                  </p>
                </div>
              </div>
              <div>
                <label
                  htmlFor="shed-nickname"
                  className="block text-sm font-medium text-text-secondary mb-1"
                >
                  Nickname <span className="font-normal text-text-tertiary">(optional)</span>
                </label>
                <input
                  id="shed-nickname"
                  type="text"
                  maxLength={30}
                  value={shedNickname}
                  onChange={(e) => setShedNickname(e.target.value)}
                  placeholder={shedCompanion.toPokemon.name}
                  className="w-full px-3 py-2 rounded-lg border border-border-default bg-surface-2 text-text-primary focus:outline-none focus:ring-2 focus:ring-amber-500"
                />
              </div>
              <div className="flex gap-3 pt-1">
                <button
                  type="button"
                  disabled={isPending}
                  onClick={() => applyEvolution(false)}
                  className="flex-1 px-4 py-2 bg-surface-2 text-text-secondary rounded-lg font-medium hover:bg-surface-3 disabled:opacity-50 transition-colors"
                >
                  Skip
                </button>
                <button
                  type="button"
                  disabled={isPending}
                  onClick={() => applyEvolution(true)}
                  className="flex-1 px-4 py-2 bg-amber-600 text-white rounded-lg font-medium hover:bg-amber-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
                >
                  {isPending ? 'Saving...' : `Add ${shedCompanion.toPokemon.name}`}
                </button>
              </div>
            </div>
          )}

          {/* Form change selection */}
          {!isDead && showFormChange && (
            <div className="space-y-3">
              <div className="flex items-center justify-between">
                <h3 className="text-sm font-medium text-text-secondary">Change form to:</h3>
                <button
                  type="button"
                  onClick={() => setShowFormChange(false)}
                  className="text-xs text-text-tertiary hover:text-text-secondary"
                >
                  Back
                </button>
              </div>
              {forms && forms.length > 0 && (
                <div className="space-y-2">
                  {forms.map((form) => (
                    <button
                      key={form.id}
                      type="button"
                      disabled={isPending}
                      onClick={() => handleEvolve(form.id)}
                      className="w-full flex items-center gap-3 p-3 rounded-lg border border-border-default hover:bg-purple-900/20 hover:border-purple-600 transition-colors disabled:opacity-50"
                    >
                      {form.spriteUrl ? (
                        <img src={form.spriteUrl} alt={form.name} className="w-10 h-10" />
                      ) : (
                        <div className="w-10 h-10 rounded-full bg-surface-3 flex items-center justify-center text-sm font-bold text-text-secondary">
                          {form.name[0]?.toUpperCase()}
                        </div>
                      )}
                      <div className="text-left">
                        <div className="font-medium text-text-primary text-sm">{form.name}</div>
                        <div className="flex gap-1">
                          {form.types.map((type) => (
                            <TypeBadge key={type} type={type} />
                          ))}
                        </div>
                      </div>
                    </button>
                  ))}
                </div>
              )}
            </div>
          )}

          {/* Confirmation form */}
          {!isDead && showConfirm && (
            <div className="space-y-3">
              <div className="bg-status-failed-bg rounded-lg p-3">
                <p className="text-sm text-status-failed font-medium">
                  This cannot be undone (Nuzlocke rules).
                </p>
              </div>

              <div>
                <label
                  htmlFor="death-level"
                  className="block text-sm font-medium text-text-secondary mb-1"
                >
                  Level at Death <span className="font-normal text-text-tertiary">(optional)</span>
                </label>
                <input
                  id="death-level"
                  type="number"
                  min={1}
                  max={100}
                  value={deathLevel}
                  onChange={(e) => setDeathLevel(e.target.value)}
                  placeholder="Level"
                  className="w-24 px-3 py-2 rounded-lg border border-border-default bg-surface-2 text-text-primary focus:outline-none focus:ring-2 focus:ring-red-500"
                />
              </div>

              <div>
                <label
                  htmlFor="death-cause"
                  className="block text-sm font-medium text-text-secondary mb-1"
                >
                  Cause of Death <span className="font-normal text-text-tertiary">(optional)</span>
                </label>
                <input
                  id="death-cause"
                  type="text"
                  maxLength={100}
                  value={cause}
                  onChange={(e) => setCause(e.target.value)}
                  placeholder="e.g. Crit from rival's Charizard"
                  className="w-full px-3 py-2 rounded-lg border border-border-default bg-surface-2 text-text-primary focus:outline-none focus:ring-2 focus:ring-red-500"
                />
              </div>

              <div className="flex gap-3 pt-1">
                <button
                  type="button"
                  onClick={() => setShowConfirm(false)}
                  className="flex-1 px-4 py-2 bg-surface-2 text-text-secondary rounded-lg font-medium hover:bg-surface-3 transition-colors"
                >
                  Cancel
                </button>
                <button
                  type="button"
                  disabled={isPending}
                  onClick={handleConfirmDeath}
                  className="flex-1 px-4 py-2 bg-red-600 text-white rounded-lg font-medium hover:bg-red-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
                >
                  {isPending ? 'Saving...' : 'Confirm Death'}
                </button>
              </div>
            </div>
          )}
        </div>

        {/* Footer for dead/no-confirm/no-evolve views */}
        {(isDead ||
          (!isDead && !showConfirm && !showEvolve && !showFormChange && !showShedConfirm)) && (
          <div className="px-6 py-4 border-t border-border-default flex justify-end">
            <button
              type="button"
              onClick={onClose}
              className="px-4 py-2 bg-surface-2 text-text-secondary rounded-lg font-medium hover:bg-surface-3 transition-colors"
            >
              Close
            </button>
          </div>
        )}
      </div>
    </div>
  )
}
