import {ReportSchema} from '@crumbs/core/schemas'
import {Hono} from 'hono'

import {type GuardedVars} from '../auth/middleware'
import {db, schema} from '../db/client'
import {validate} from '../lib/validate'

// abuse reports are write-only for now: persisted for later human review,
// with no admin surface. accepting any well-formed subject ref keeps the
// client simple; triage decides relevance
const reports = new Hono<{Variables: GuardedVars}>().post(
  '/',
  validate('json', ReportSchema),
  async (c) => {
    const me = c.get('user')
    const {subjectType, subjectId, reason, detail} =
      c.req.valid('json')
    await db.insert(schema.reports).values({
      reporterId: me.id,
      subjectType,
      subjectId,
      reason,
      detail: detail ?? null,
    })
    return c.json({ok: true})
  },
)

export default reports
