import { useState } from 'react'
import { AdminTable, type Column } from '../../components/admin/AdminTable'
import { BulkImportModal } from '../../components/admin/BulkImportModal'
import { EvolutionFormModal } from '../../components/admin/EvolutionFormModal'
import {
  useEvolutionList,
  useCreateEvolution,
  useUpdateEvolution,
  useDeleteEvolution,
  useBulkImportEvolutions,
} from '../../hooks/useAdmin'
import { exportEvolutions } from '../../api/admin'
import { downloadJson } from '../../utils/download'
import type { EvolutionAdmin, CreateEvolutionInput, UpdateEvolutionInput } from '../../types'

const PAGE_SIZE = 50

const EVOLUTION_TRIGGERS = [
  { value: 'level-up', label: 'Level Up' },
  { value: 'trade', label: 'Trade' },
  { value: 'use-item', label: 'Use Item' },
  { value: 'shed', label: 'Shed' },
  { value: 'other', label: 'Other' },
]

export function AdminEvolutions() {
  const [search, setSearch] = useState('')
  const [triggerFilter, setTriggerFilter] = useState('')
  const [page, setPage] = useState(0)
  const offset = page * PAGE_SIZE
  const { data, isLoading } = useEvolutionList(search || undefined, PAGE_SIZE, offset, triggerFilter || undefined)
  const evolutions = data?.items ?? []
  const total = data?.total ?? 0
  const totalPages = Math.ceil(total / PAGE_SIZE)

  const createEvolution = useCreateEvolution()
  const updateEvolution = useUpdateEvolution()
  const deleteEvolution = useDeleteEvolution()
  const bulkImport = useBulkImportEvolutions()

  const [showCreate, setShowCreate] = useState(false)
  const [showBulkImport, setShowBulkImport] = useState(false)
  const [editing, setEditing] = useState<EvolutionAdmin | null>(null)

  const columns: Column<EvolutionAdmin>[] = [
    {
      header: 'From',
      accessor: (e) => (
        <div className="flex items-center gap-2">
          {e.fromPokemon.spriteUrl && (
            <img src={e.fromPokemon.spriteUrl} alt="" className="w-6 h-6" />
          )}
          <span>{e.fromPokemon.name}</span>
        </div>
      ),
    },
    {
      header: 'To',
      accessor: (e) => (
        <div className="flex items-center gap-2">
          {e.toPokemon.spriteUrl && (
            <img src={e.toPokemon.spriteUrl} alt="" className="w-6 h-6" />
          )}
          <span>{e.toPokemon.name}</span>
        </div>
      ),
    },
    { header: 'Trigger', accessor: (e) => e.trigger },
    { header: 'Level', accessor: (e) => e.minLevel ?? '-' },
    { header: 'Item', accessor: (e) => e.item ?? '-' },
  ]

  return (
    <div>
      <div className="flex justify-between items-center mb-4">
        <h2 className="text-xl font-semibold">Evolutions</h2>
        <div className="flex gap-2">
          <button
            onClick={async () => {
              const data = await exportEvolutions()
              downloadJson(data, 'evolutions.json')
            }}
            className="px-4 py-2 text-sm font-medium rounded-md border border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-800"
          >
            Export
          </button>
          <button
            onClick={() => setShowBulkImport(true)}
            className="px-4 py-2 text-sm font-medium rounded-md border border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-800"
          >
            Bulk Import
          </button>
          <button
            onClick={() => setShowCreate(true)}
            className="px-4 py-2 text-sm font-medium rounded-md bg-blue-600 text-white hover:bg-blue-700"
          >
            Add Evolution
          </button>
        </div>
      </div>

      <div className="mb-4 flex items-center gap-4">
        <input
          type="text"
          value={search}
          onChange={(e) => {
            setSearch(e.target.value)
            setPage(0)
          }}
          placeholder="Search by pokemon name, trigger, or item..."
          className="w-full max-w-sm px-3 py-2 border rounded-md dark:bg-gray-700 dark:border-gray-600"
        />
        <select
          value={triggerFilter}
          onChange={(e) => {
            setTriggerFilter(e.target.value)
            setPage(0)
          }}
          className="px-3 py-2 border rounded-md dark:bg-gray-700 dark:border-gray-600"
        >
          <option value="">All triggers</option>
          {EVOLUTION_TRIGGERS.map((t) => (
            <option key={t.value} value={t.value}>{t.label}</option>
          ))}
        </select>
        {(search || triggerFilter) && (
          <button
            onClick={() => { setSearch(''); setTriggerFilter(''); setPage(0) }}
            className="text-sm text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200"
          >
            Clear filters
          </button>
        )}
        <span className="text-sm text-gray-500 dark:text-gray-400 whitespace-nowrap">
          {total} evolutions
        </span>
      </div>

      <AdminTable
        columns={columns}
        data={evolutions}
        isLoading={isLoading}
        emptyMessage="No evolutions found."
        keyFn={(e) => e.id}
        onRowClick={(e) => setEditing(e)}
      />

      {totalPages > 1 && (
        <div className="mt-4 flex items-center justify-between">
          <div className="text-sm text-gray-500 dark:text-gray-400">
            Showing {offset + 1}-{Math.min(offset + PAGE_SIZE, total)} of {total}
          </div>
          <div className="flex items-center gap-2">
            <button
              onClick={() => setPage(0)}
              disabled={page === 0}
              className="px-3 py-1 text-sm rounded border border-gray-300 dark:border-gray-600 disabled:opacity-50 disabled:cursor-not-allowed hover:bg-gray-50 dark:hover:bg-gray-700"
            >
              First
            </button>
            <button
              onClick={() => setPage((p) => Math.max(0, p - 1))}
              disabled={page === 0}
              className="px-3 py-1 text-sm rounded border border-gray-300 dark:border-gray-600 disabled:opacity-50 disabled:cursor-not-allowed hover:bg-gray-50 dark:hover:bg-gray-700"
            >
              Prev
            </button>
            <span className="text-sm text-gray-600 dark:text-gray-300 px-2">
              Page {page + 1} of {totalPages}
            </span>
            <button
              onClick={() => setPage((p) => Math.min(totalPages - 1, p + 1))}
              disabled={page >= totalPages - 1}
              className="px-3 py-1 text-sm rounded border border-gray-300 dark:border-gray-600 disabled:opacity-50 disabled:cursor-not-allowed hover:bg-gray-50 dark:hover:bg-gray-700"
            >
              Next
            </button>
            <button
              onClick={() => setPage(totalPages - 1)}
              disabled={page >= totalPages - 1}
              className="px-3 py-1 text-sm rounded border border-gray-300 dark:border-gray-600 disabled:opacity-50 disabled:cursor-not-allowed hover:bg-gray-50 dark:hover:bg-gray-700"
            >
              Last
            </button>
          </div>
        </div>
      )}

      {showBulkImport && (
        <BulkImportModal
          title="Bulk Import Evolutions"
          example={`[\n  { "from_pokeapi_id": 1, "to_pokeapi_id": 2, "trigger": "level-up", "min_level": 16 }\n]`}
          onSubmit={(items) => bulkImport.mutateAsync(items)}
          onClose={() => setShowBulkImport(false)}
        />
      )}

      {showCreate && (
        <EvolutionFormModal
          onSubmit={(data) =>
            createEvolution.mutate(data as CreateEvolutionInput, {
              onSuccess: () => setShowCreate(false),
            })
          }
          onClose={() => setShowCreate(false)}
          isSubmitting={createEvolution.isPending}
        />
      )}

      {editing && (
        <EvolutionFormModal
          evolution={editing}
          onSubmit={(data) =>
            updateEvolution.mutate(
              { id: editing.id, data: data as UpdateEvolutionInput },
              { onSuccess: () => setEditing(null) },
            )
          }
          onClose={() => setEditing(null)}
          isSubmitting={updateEvolution.isPending}
          onDelete={() =>
            deleteEvolution.mutate(editing.id, {
              onSuccess: () => setEditing(null),
            })
          }
          isDeleting={deleteEvolution.isPending}
        />
      )}
    </div>
  )
}
