import { prisma } from '../../lib/prisma.js'
import { normalizeEmail } from '../../lib/auth.js'

export async function registerAdminUserRoutes(fastify) {
  fastify.get(
    '/api/admin/users',
    { preHandler: [fastify.verifyAdmin] },
    async (request, reply) => {
      const qRaw = request.query?.q
      const q = typeof qRaw === 'string' ? qRaw.trim() : ''

      const pageRaw = request.query?.page
      const pageParsed = typeof pageRaw === 'string' ? Number(pageRaw) : Number(pageRaw)
      const page = Number.isFinite(pageParsed) && pageParsed > 0 ? Math.floor(pageParsed) : 1

      const pageSizeRaw = request.query?.pageSize
      const pageSizeParsed = typeof pageSizeRaw === 'string' ? Number(pageSizeRaw) : Number(pageSizeRaw)
      const pageSize = Number.isFinite(pageSizeParsed) && pageSizeParsed > 0 ? Math.floor(pageSizeParsed) : 20

      if (pageSize > 100) {
        reply.code(400).send({ error: 'pageSize должен быть ≤ 100' })
        return
      }

      const where = q
        ? {
            OR: [{ email: { contains: q } }, { name: { contains: q } }],
          }
        : undefined

      const total = await prisma.user.count({ where })

      const users = await prisma.user.findMany({
        where,
        select: {
          id: true,
          email: true,
          name: true,
          createdAt: true,
          updatedAt: true,
        },
        orderBy: { updatedAt: 'desc' },
        skip: (page - 1) * pageSize,
        take: pageSize,
      })
      const items = users.map((u) => ({
        id: u.id,
        email: u.email,
        name: u.name,
        createdAt: u.createdAt,
        updatedAt: u.updatedAt,
      }))

      return { items, total, page, pageSize }
    },
  )

  fastify.post(
    '/api/admin/users',
    { preHandler: [fastify.verifyAdmin] },
    async (request, reply) => {
      const body = request.body ?? {}

      const email = normalizeEmail(body.email)
      if (!email || !email.includes('@')) {
        reply.code(400).send({ error: 'Некорректная почта' })
        return
      }

      const nameRaw = body.name
      const name = nameRaw === null || nameRaw === undefined ? null : String(nameRaw).trim()
      if (name !== null && name.length > 40) {
        reply.code(400).send({ error: 'Имя/ник максимум 40 символов' })
        return
      }

      const exists = await prisma.user.findUnique({ where: { email } })
      if (exists) {
        reply.code(409).send({ error: 'Почта уже занята' })
        return
      }

      const user = await prisma.user.create({
        data: {
          email,
          name: name && name.length ? name : null,
        },
      })

      reply.code(201).send({
        id: user.id,
        email: user.email,
        name: user.name,
        createdAt: user.createdAt,
        updatedAt: user.updatedAt,
      })
    },
  )

  fastify.patch(
    '/api/admin/users/:id',
    { preHandler: [fastify.verifyAdmin] },
    async (request, reply) => {
      const { id } = request.params
      const body = request.body ?? {}

      const existing = await prisma.user.findUnique({ where: { id } })
      if (!existing) {
        reply.code(404).send({ error: 'Пользователь не найден' })
        return
      }

      const data = {}

      if (body.email !== undefined) {
        const email = normalizeEmail(body.email)
        if (!email || !email.includes('@')) {
          reply.code(400).send({ error: 'Некорректная почта' })
          return
        }
        if (email !== existing.email) {
          const clash = await prisma.user.findUnique({ where: { email } })
          if (clash) {
            reply.code(409).send({ error: 'Почта уже занята' })
            return
          }
          data.email = email
        }
      }

      if (body.name !== undefined) {
        const nameRaw = body.name
        const name = nameRaw === null || nameRaw === undefined ? null : String(nameRaw).trim()
        if (name !== null && name.length > 40) {
          reply.code(400).send({ error: 'Имя/ник максимум 40 символов' })
          return
        }
        data.name = name && name.length ? name : null
      }

      const user = await prisma.user.update({ where: { id }, data })
      return {
        id: user.id,
        email: user.email,
        name: user.name,
        createdAt: user.createdAt,
        updatedAt: user.updatedAt,
      }
    },
  )

  fastify.delete(
    '/api/admin/users/:id',
    { preHandler: [fastify.verifyAdmin] },
    async (request, reply) => {
      const { id } = request.params
      try {
        await prisma.user.delete({ where: { id } })
        reply.code(204).send()
      } catch {
        reply.code(404).send({ error: 'Пользователь не найден' })
      }
    },
  )
}

