import { Router } from 'express';
import { prisma } from '../config/database.js';
import { authenticate } from '../middleware/auth.js';
import { AppError } from '../middleware/errorHandler.js';

const router = Router();

router.get('/conversations', authenticate, async (req, res, next) => {
  try {
    const conversations = await prisma.conversation.findMany({
      where: { OR: [{ user1Id: req.userId }, { user2Id: req.userId }] },
      include: {
        user1: { select: { id: true, fullName: true, nickname: true, avatar: true } },
        user2: { select: { id: true, fullName: true, nickname: true, avatar: true } },
        listing: { select: { id: true, title: true, price: true, images: { take: 1 } } },
        messages: { orderBy: { createdAt: 'desc' }, take: 1 },
      },
      orderBy: { updatedAt: 'desc' },
    });

    const result = await Promise.all(conversations.map(async (conv) => {
      const unreadCount = await prisma.message.count({
        where: { conversationId: conv.id, senderId: { not: req.userId }, isRead: false },
      });
      return {
        ...conv,
        lastMessage: conv.messages[0] || null,
        unreadCount,
      };
    }));

    res.json(result);
  } catch (error) {
    next(error);
  }
});

router.get('/conversations/:id/messages', authenticate, async (req, res, next) => {
  try {
    const conv = await prisma.conversation.findUnique({ where: { id: req.params.id } });
    if (!conv) throw new AppError(404, 'Conversation not found');
    if (conv.user1Id !== req.userId && conv.user2Id !== req.userId) throw new AppError(403, 'Not authorized');

    const messages = await prisma.message.findMany({
      where: { conversationId: req.params.id },
      include: { sender: { select: { id: true, fullName: true, avatar: true } } },
      orderBy: { createdAt: 'asc' },
    });

    await prisma.message.updateMany({
      where: { conversationId: req.params.id, senderId: { not: req.userId }, isRead: false },
      data: { isRead: true },
    });

    res.json(messages);
  } catch (error) {
    next(error);
  }
});

router.post('/conversations', authenticate, async (req, res, next) => {
  try {
    const { recipientId, listingId, message } = req.body;
    if (!recipientId) throw new AppError(400, 'Recipient ID is required');
    if (recipientId === req.userId) throw new AppError(400, 'Cannot message yourself');

    const [id1, id2] = [req.userId!, recipientId].sort();
    const listingIdValue = listingId || undefined;

    let conversation = await prisma.conversation.findFirst({
      where: {
        user1Id: id1,
        user2Id: id2,
        ...(listingIdValue ? { listingId: listingIdValue } : { listingId: null }),
      },
    });

    if (!conversation) {
      conversation = await prisma.conversation.create({
        data: {
          user1Id: id1,
          user2Id: id2,
          ...(listingIdValue ? { listingId: listingIdValue } : {}),
        },
      });
    }

    if (message) {
      await prisma.message.create({
        data: { content: message, senderId: req.userId!, conversationId: conversation.id },
      });
      await prisma.conversation.update({ where: { id: conversation.id }, data: { updatedAt: new Date() } });
    }

    res.json(conversation);
  } catch (error) {
    next(error);
  }
});

export default router;
