import { useEffect, useState } from 'react'
import { Link } from 'react-router-dom'
import { identity } from '@/api/client'
import type { User } from '@/types/api'
import { PageHeader } from '@/components/PageHeader'
import { Loading } from '@/components/Loading'
import { ErrorBox } from '@/components/ErrorBox'
import { DataTable, type Column } from '@/components/DataTable'

export function UsersPage() {
  const [rows, setRows] = useState<User[]>([])
  const [error, setError] = useState<Error | null>(null)
  const [loading, setLoading] = useState(true)

  useEffect(() => {
    identity
      .listUsers()
      .then(setRows)
      .catch((e: unknown) => setError(e instanceof Error ? e : new Error(String(e))))
      .finally(() => setLoading(false))
  }, [])

  const columns: Column<User>[] = [
    {
      header: 'Username',
      key: 'username',
      render: (r) => (
        <Link to={`/users/${r.id}`} className="font-mono text-brand-600 hover:underline">
          {r.username}
        </Link>
      ),
    },
    { header: 'Display name', key: 'displayName' },
    { header: 'Email', key: 'email', render: (r) => r.email ?? '—' },
    {
      header: 'Enabled',
      key: 'enabled',
      render: (r) =>
        r.enabled ? (
          <span className="text-emerald-600">Active</span>
        ) : (
          <span className="text-slate-400">Disabled</span>
        ),
    },
  ]

  return (
    <div>
      <PageHeader
        title="Users"
        subtitle="User accounts in this instance. The admin role has all permissions."
        actions={<Link to="/users/new" className="btn-primary">+ New User</Link>}
      />
      {loading && <Loading />}
      {error && <ErrorBox error={error} />}
      {!loading && !error && <DataTable rows={rows} columns={columns} />}
    </div>
  )
}
