import { Router, Request, Response } from 'express';
import { prisma } from '../db/prisma';
import { requireAuth, requires } from '../middleware/auth';

const router = Router();

router.get(
  '/hidden',
  requireAuth,
  requires('moderation.act'),
  async (_req: Request, res: Response) => {
    try {
      const hidden = await prisma.hiddenContent.findMany({
        orderBy: { createdAt: 'desc' },
      });
      res.json(hidden);
    } catch (err) {
      console.error('List hidden content error:', err);
      res.status(500).json({ error: 'Internal server error' });
    }
  }
);

router.post(
  '/hide',
  requireAuth,
  requires('moderation.act'),
  async (req: Request, res: Response) => {
    try {
      const { nostrEventId, reason } = req.body;
      if (!nostrEventId) {
        res.status(400).json({ error: 'nostrEventId is required' });
        return;
      }

      const hidden = await prisma.hiddenContent.create({
        data: {
          nostrEventId,
          reason: reason || null,
          hiddenBy: req.user!.pubkey,
        },
      });

      res.status(201).json(hidden);
    } catch (err) {
      console.error('Hide content error:', err);
      res.status(500).json({ error: 'Internal server error' });
    }
  }
);

router.delete(
  '/unhide/:id',
  requireAuth,
  requires('moderation.act'),
  async (req: Request, res: Response) => {
    try {
      const item = await prisma.hiddenContent.findUnique({
        where: { id: req.params.id as string },
      });
      if (!item) {
        res.status(404).json({ error: 'Hidden content not found' });
        return;
      }

      await prisma.hiddenContent.delete({ where: { id: req.params.id as string } });
      res.json({ success: true });
    } catch (err) {
      console.error('Unhide content error:', err);
      res.status(500).json({ error: 'Internal server error' });
    }
  }
);

router.get(
  '/blocked',
  requireAuth,
  requires('moderation.act'),
  async (_req: Request, res: Response) => {
    try {
      const blocked = await prisma.blockedPubkey.findMany({
        orderBy: { createdAt: 'desc' },
      });
      res.json(blocked);
    } catch (err) {
      console.error('List blocked pubkeys error:', err);
      res.status(500).json({ error: 'Internal server error' });
    }
  }
);

router.post(
  '/block',
  requireAuth,
  requires('moderation.act'),
  async (req: Request, res: Response) => {
    try {
      const { pubkey, reason } = req.body;
      if (!pubkey) {
        res.status(400).json({ error: 'pubkey is required' });
        return;
      }

      const blocked = await prisma.blockedPubkey.create({
        data: {
          pubkey,
          reason: reason || null,
          blockedBy: req.user!.pubkey,
        },
      });

      res.status(201).json(blocked);
    } catch (err) {
      console.error('Block pubkey error:', err);
      res.status(500).json({ error: 'Internal server error' });
    }
  }
);

router.delete(
  '/unblock/:id',
  requireAuth,
  requires('moderation.act'),
  async (req: Request, res: Response) => {
    try {
      const item = await prisma.blockedPubkey.findUnique({
        where: { id: req.params.id as string },
      });
      if (!item) {
        res.status(404).json({ error: 'Blocked pubkey not found' });
        return;
      }

      await prisma.blockedPubkey.delete({ where: { id: req.params.id as string } });
      res.json({ success: true });
    } catch (err) {
      console.error('Unblock pubkey error:', err);
      res.status(500).json({ error: 'Internal server error' });
    }
  }
);

export default router;
