import { useState, useEffect, useCallback } from 'react'
import { motion, AnimatePresence } from 'framer-motion'
import { useAuth } from '../context/AuthContext'
import { useAlert } from '../context/AlertContext'
import ConfirmModal from '../components/ConfirmModal'
import FormField from '../components/FormField'
import Forbidden from '../components/Forbidden'
import useModalLock from '../hooks/useModalLock'

import apiFetch from '../utils/api'
const API_BASE = '/api/admin'

interface User {
  id: number
  username: string
  email: string
  first_name: string
  last_name: string
  role_id: number
  roles?: { id: number; name: string; display_name: string } | null
  is_active: boolean
}

interface Role {
  id: number
  name: string
  display_name: string
}

interface FormData {
  username: string
  email: string
  password: string
  first_name: string
  last_name: string
  role_id: number | string
  is_active: boolean
}

export default function Users() {
  const { user: currentUser, updateUser, hasPermission } = useAuth()
  const alert = useAlert()
  const [users, setUsers] = useState<User[]>([])
  const [roles, setRoles] = useState<Role[]>([])
  const [loading, setLoading] = useState(true)
  const [showModal, setShowModal] = useState(false)
  const [editingUser, setEditingUser] = useState<User | null>(null)
  const [deleteModal, setDeleteModal] = useState<{ isOpen: boolean; user: User | null }>({ isOpen: false, user: null })
  const [deleting, setDeleting] = useState(false)
  const [formData, setFormData] = useState<FormData>({
    username: '',
    email: '',
    password: '',
    first_name: '',
    last_name: '',
    role_id: '',
    is_active: true
  })

  useModalLock(showModal)

  const fetchUsers = useCallback(async () => {
    try {
      const usersRes = await apiFetch(`${API_BASE}/users`)
      const usersData = await usersRes.json()

      if (usersData.success) {
        setUsers(Array.isArray(usersData.data) ? usersData.data : [])
      } else {
        alert.error(usersData.error || 'Nepodařilo se načíst uživatele')
      }

      // Roles fetch — gracefully handle 403 if user lacks settings.roles permission
      try {
        const rolesRes = await apiFetch(`${API_BASE}/roles`)
        const rolesData = await rolesRes.json()
        if (rolesData.success) {
          setRoles(Array.isArray(rolesData.data) ? rolesData.data : [])
        }
      } catch { /* roles not accessible */ }
    } catch {
      alert.error('Chyba připojení')
    } finally {
      setLoading(false)
    }
  }, []) // eslint-disable-line react-hooks/exhaustive-deps

  useEffect(() => {
    fetchUsers()
  }, [fetchUsers])

  if (!hasPermission('users.view')) return <Forbidden />

  const openCreateModal = () => {
    setEditingUser(null)
    setFormData({
      username: '',
      email: '',
      password: '',
      first_name: '',
      last_name: '',
      role_id: roles[0]?.id || '',
      is_active: true
    })
    setShowModal(true)
  }

  const openEditModal = (user: User) => {
    setEditingUser(user)
    setFormData({
      username: user.username,
      email: user.email,
      password: '',
      first_name: user.first_name,
      last_name: user.last_name,
      role_id: user.role_id,
      is_active: user.is_active
    })
    setShowModal(true)
  }

  const closeModal = () => {
    setShowModal(false)
    setEditingUser(null)
  }

  const handleSubmit = async (e?: React.FormEvent) => {
    e?.preventDefault()

    const dataToSave = { ...formData }
    const wasEditing = editingUser
    const editingId = editingUser?.id

    try {
      const url = wasEditing
        ? `${API_BASE}/users/${editingId}`
        : `${API_BASE}/users`

      const method = wasEditing ? 'PUT' : 'POST'

      const response = await apiFetch(url, {
        method,
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(dataToSave)
      })

      const data = await response.json()

      if (data.success) {
        if (wasEditing && currentUser && Number(editingId) === Number(currentUser.id)) {
          updateUser({
            username: dataToSave.username,
            email: dataToSave.email,
            fullName: `${dataToSave.first_name} ${dataToSave.last_name}`.trim()
          })
        }
        closeModal()
        await new Promise(resolve => setTimeout(resolve, 300))
        alert.success(wasEditing ? 'Uživatel byl upraven' : 'Uživatel byl vytvořen')
        fetchUsers()
      } else {
        alert.error(data.error || 'Nepodařilo se uložit uživatele')
      }
    } catch {
      alert.error('Chyba připojení')
    }
  }

  const openDeleteModal = (user: User) => {
    setDeleteModal({ isOpen: true, user })
  }

  const closeDeleteModal = () => {
    setDeleteModal({ isOpen: false, user: null })
  }

  const handleDelete = async () => {
    if (!deleteModal.user) return

    setDeleting(true)
    try {
      const response = await apiFetch(`${API_BASE}/users/${deleteModal.user.id}`, {
        method: 'DELETE',
      })

      const data = await response.json()

      if (data.success) {
        closeDeleteModal()
        fetchUsers()
        alert.success('Uživatel byl smazán')
      } else {
        alert.error(data.error || 'Nepodařilo se smazat uživatele')
      }
    } catch {
      alert.error('Chyba připojení')
    } finally {
      setDeleting(false)
    }
  }

  const toggleActive = async (user: User) => {
    try {
      const response = await apiFetch(`${API_BASE}/users/${user.id}`, {
        method: 'PUT',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          is_active: !user.is_active
        })
      })

      const data = await response.json()

      if (data.success) {
        fetchUsers()
        alert.success(user.is_active ? 'Uživatel byl deaktivován' : 'Uživatel byl aktivován')
      } else {
        alert.error(data.error || 'Nepodařilo se změnit stav uživatele')
      }
    } catch {
      alert.error('Chyba připojení')
    }
  }

  const getRoleBadgeClass = (roleName: string): string => {
    switch (roleName) {
      case 'admin': return 'admin-badge admin-badge-admin'
      default: return 'admin-badge admin-badge-viewer'
    }
  }

  if (loading) {
    return (
      <div className="admin-skeleton" style={{ padding: 0, gap: '1.5rem' }}>
        <div className="admin-skeleton-row" style={{ justifyContent: 'space-between' }}>
          <div>
            <div className="admin-skeleton-line h-8" style={{ width: '200px', marginBottom: '0.5rem' }} />
            <div className="admin-skeleton-line" style={{ width: '140px' }} />
          </div>
          <div className="admin-skeleton-line h-10" style={{ width: '160px', borderRadius: '8px' }} />
        </div>
        <div className="admin-card">
          <div className="admin-skeleton" style={{ gap: '1.25rem' }}>
            {[0, 1, 2, 3, 4].map(i => (
              <div key={i} className="admin-skeleton-row">
                <div className="admin-skeleton-line circle" />
                <div className="flex-1">
                  <div className="admin-skeleton-line w-1/3 mb-2" />
                  <div className="admin-skeleton-line w-1/4" style={{ height: '10px' }} />
                </div>
                <div className="admin-skeleton-line w-1/4" />
              </div>
            ))}
          </div>
        </div>
      </div>
    )
  }

  return (
    <div>
      <motion.div
        className="admin-page-header"
        initial={{ opacity: 0, y: 12 }}
        animate={{ opacity: 1, y: 0 }}
        transition={{ duration: 0.25 }}
      >
        <div>
          <h1 className="admin-page-title">Uživatelé</h1>
          <p className="admin-page-subtitle">Správa uživatelských účtů a oprávnění</p>
        </div>
        <button onClick={openCreateModal} className="admin-btn admin-btn-primary">
          <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
            <line x1="12" y1="5" x2="12" y2="19" />
            <line x1="5" y1="12" x2="19" y2="12" />
          </svg>
          Přidat uživatele
        </button>
      </motion.div>

      <motion.div
        className="admin-card"
        initial={{ opacity: 0, y: 12 }}
        animate={{ opacity: 1, y: 0 }}
        transition={{ duration: 0.25, delay: 0.06 }}
      >
        <div className="admin-card-body">
          <div className="admin-table-responsive">
            <table className="admin-table">
              <thead>
                <tr>
                  <th>Uživatel</th>
                  <th>E-mail</th>
                  <th>Role</th>
                  <th>Stav</th>
                  <th>Akce</th>
                </tr>
              </thead>
              <tbody>
                {users.map((user) => (
                  <tr key={user.id}>
                    <td>
                      <div className="admin-table-user">
                        <div className="admin-table-avatar">
                          {(user.first_name || user.username).charAt(0).toUpperCase()}
                        </div>
                        <div>
                          <div className="admin-table-name">
                            {user.first_name} {user.last_name}
                          </div>
                          <div className="admin-table-username">@{user.username}</div>
                        </div>
                      </div>
                    </td>
                    <td>{user.email}</td>
                    <td>
                      <span className={getRoleBadgeClass(user.roles?.name ?? '')}>
                        {user.roles?.display_name || user.roles?.name || '—'}
                      </span>
                    </td>
                    <td>
                      <button
                        onClick={() => user.id !== currentUser?.id && toggleActive(user)}
                        disabled={user.id === currentUser?.id}
                        className={`admin-badge ${user.is_active ? 'admin-badge-active' : 'admin-badge-inactive'}`}
                        style={{ cursor: user.id === currentUser?.id ? 'not-allowed' : 'pointer' }}
                      >
                        {user.is_active ? 'Aktivní' : 'Neaktivní'}
                      </button>
                    </td>
                    <td>
                      <div className="admin-table-actions">
                        <button
                          onClick={() => openEditModal(user)}
                          className="admin-btn-icon"
                          title="Upravit"
                          aria-label="Upravit"
                        >
                          <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
                            <path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7" />
                            <path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z" />
                          </svg>
                        </button>
                        {user.id !== currentUser?.id && (
                          <button
                            onClick={() => openDeleteModal(user)}
                            className="admin-btn-icon danger"
                            title="Smazat"
                            aria-label="Smazat"
                          >
                            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
                              <polyline points="3 6 5 6 21 6" />
                              <path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" />
                            </svg>
                          </button>
                        )}
                      </div>
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        </div>
      </motion.div>

      <AnimatePresence>
        {showModal && (
          <motion.div
            className="admin-modal-overlay"
            initial={{ opacity: 0 }}
            animate={{ opacity: 1 }}
            exit={{ opacity: 0 }}
            transition={{ duration: 0.2 }}
          >
            <div className="admin-modal-backdrop" onClick={closeModal} />
            <motion.div
              className="admin-modal"
              initial={{ opacity: 0, scale: 0.95, y: 20 }}
              animate={{ opacity: 1, scale: 1, y: 0 }}
              exit={{ opacity: 0, scale: 0.95, y: 20 }}
              transition={{ duration: 0.2 }}
            >
              <div className="admin-modal-header">
                <h2 className="admin-modal-title">
                  {editingUser ? 'Upravit uživatele' : 'Přidat nového uživatele'}
                </h2>
              </div>

              <div className="admin-modal-body">
                <div className="admin-form">
                  <div className="admin-form-row">
                    <FormField label="Jméno">
                      <input
                        type="text"
                        value={formData.first_name}
                        onChange={(e) => setFormData({ ...formData, first_name: e.target.value })}
                        required
                        className="admin-form-input"
                      />
                    </FormField>
                    <FormField label="Příjmení">
                      <input
                        type="text"
                        value={formData.last_name}
                        onChange={(e) => setFormData({ ...formData, last_name: e.target.value })}
                        required
                        className="admin-form-input"
                      />
                    </FormField>
                  </div>

                  <FormField label="Uživatelské jméno">
                    <input
                      type="text"
                      value={formData.username}
                      onChange={(e) => setFormData({ ...formData, username: e.target.value })}
                      required
                      className="admin-form-input"
                    />
                  </FormField>

                  <FormField label="E-mail">
                    <input
                      type="email"
                      value={formData.email}
                      onChange={(e) => setFormData({ ...formData, email: e.target.value })}
                      required
                      className="admin-form-input"
                    />
                  </FormField>

                  <FormField label={`Heslo ${editingUser ? '(ponechte prázdné pro zachování stávajícího)' : ''}`}>
                    <input
                      type="password"
                      value={formData.password}
                      onChange={(e) => setFormData({ ...formData, password: e.target.value })}
                      required={!editingUser}
                      className="admin-form-input"
                    />
                  </FormField>

                  <FormField label="Role">
                    <select
                      value={formData.role_id}
                      onChange={(e) => setFormData({ ...formData, role_id: e.target.value })}
                      required
                      className="admin-form-select"
                    >
                      {roles.map((role) => (
                        <option key={role.id} value={role.id}>
                          {role.display_name}
                        </option>
                      ))}
                    </select>
                  </FormField>

                  <label className="admin-form-checkbox">
                    <input
                      type="checkbox"
                      checked={formData.is_active}
                      onChange={(e) => setFormData({ ...formData, is_active: e.target.checked })}
                    />
                    <span>Účet je aktivní</span>
                  </label>
                </div>
              </div>

              <div className="admin-modal-footer">
                <button type="button" onClick={closeModal} className="admin-btn admin-btn-secondary">
                  Zrušit
                </button>
                <button type="button" onClick={handleSubmit} className="admin-btn admin-btn-primary">
                  {editingUser ? 'Uložit změny' : 'Vytvořit uživatele'}
                </button>
              </div>
            </motion.div>
          </motion.div>
        )}
      </AnimatePresence>

      <ConfirmModal
        isOpen={deleteModal.isOpen}
        onClose={closeDeleteModal}
        onConfirm={handleDelete}
        title="Smazat uživatele"
        message={`Opravdu chcete smazat uživatele "${deleteModal.user?.first_name} ${deleteModal.user?.last_name}"? Tato akce je nevratná.`}
        confirmText="Smazat"
        cancelText="Zrušit"
        type="danger"
        loading={deleting}
      />
    </div>
  )
}
