import { Router } from 'express'
import { requireCompanyAuth } from '../../middleware/requireCompanyAuth'
import { requireTenant } from '../../middleware/requireTenant'
import { requireSubscription } from '../../middleware/requireSubscription'
import { requireRenterAuth } from '../../middleware/requireRenterAuth'
import { parseBody, parseQuery, parseParams } from '../../http/validate'
import { ok } from '../../http/respond'
import * as service from './notification.service'
import { preferencesSchema, idParamSchema, unreadQuerySchema, historyQuerySchema } from './notification.schemas'

const router = Router()

const companyAuth = [requireCompanyAuth, requireTenant, requireSubscription] as const

// ─── Company notifications ────────────────────────────────────

router.get('/company', ...companyAuth, async (req, res, next) => {
  try {
    const { unread } = parseQuery(unreadQuerySchema, req)
    ok(res, await service.listCompany(req.companyId, unread))
  } catch (err) { next(err) }
})

router.get('/unread-count', ...companyAuth, async (req, res, next) => {
  try {
    ok(res, { unread: await service.countUnread(req.companyId) })
  } catch (err) { next(err) }
})

router.post('/company/:id/read', ...companyAuth, async (req, res, next) => {
  try {
    const { id } = parseParams(idParamSchema, req)
    await service.markRead(id, req.companyId)
    ok(res, { success: true })
  } catch (err) { next(err) }
})

router.post('/company/read-all', ...companyAuth, async (req, res, next) => {
  try {
    await service.markAllRead(req.companyId)
    ok(res, { success: true })
  } catch (err) { next(err) }
})

router.get('/company/preferences', ...companyAuth, async (req, res, next) => {
  try {
    ok(res, await service.getPreferences(req.employee.id))
  } catch (err) { next(err) }
})

router.patch('/company/preferences', ...companyAuth, async (req, res, next) => {
  try {
    const prefs = parseBody(preferencesSchema, req)
    await service.setPreferences(req.employee.id, prefs)
    ok(res, { success: true })
  } catch (err) { next(err) }
})

router.get('/history', ...companyAuth, async (req, res, next) => {
  try {
    const { channel, status, limit } = parseQuery(historyQuerySchema, req)
    ok(res, await service.listCompanyHistory(req.companyId, { channel, status, limit }))
  } catch (err) { next(err) }
})

// ─── Renter notifications ─────────────────────────────────────

router.get('/renter', requireRenterAuth, async (req, res, next) => {
  try {
    ok(res, await service.listRenter(req.renterId))
  } catch (err) { next(err) }
})

router.post('/renter/:id/read', requireRenterAuth, async (req, res, next) => {
  try {
    const { id } = parseParams(idParamSchema, req)
    await service.markRenterRead(id, req.renterId)
    ok(res, { success: true })
  } catch (err) { next(err) }
})

router.post('/renter/read-all', requireRenterAuth, async (req, res, next) => {
  try {
    await service.markAllRenterRead(req.renterId)
    ok(res, { success: true })
  } catch (err) { next(err) }
})

router.get('/renter/preferences', requireRenterAuth, async (req, res, next) => {
  try {
    ok(res, await service.getRenterPreferences(req.renterId))
  } catch (err) { next(err) }
})

router.patch('/renter/preferences', requireRenterAuth, async (req, res, next) => {
  try {
    const prefs = parseBody(preferencesSchema, req)
    await service.setRenterPreferences(req.renterId, prefs)
    ok(res, { success: true })
  } catch (err) { next(err) }
})

export default router
