import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';

export async function GET(
  req: NextRequest,
  { params }: { params: Promise<{ contactId: string }> }
) {
  try {
    const session = await getServerSession(authOptions);
    
    if (!session?.user?.email) {
      return NextResponse.json(
        { error: 'Unauthorized' },
        { status: 401 }
      );
    }

    const { contactId } = await params;

    if (!contactId) {
      return NextResponse.json(
        { error: 'Contact ID is required' },
        { status: 400 }
      );
    }

    // TODO: Add Conversation and Message models to schema
    // Return mock conversation data for now
    const mockMessages = [
      {
        id: '1',
        conversationId: contactId,
        senderId: session.user.email,
        receiverId: contactId,
        content: 'How the hell am I supposed to get a jury to believe you when I am not even sure that I do?!',
        timestamp: '10:45 AM',
        messageType: 'text' as const,
        fileUrl: null,
        fileName: null,
        isRead: true
      },
      {
        id: '2',
        conversationId: contactId,
        senderId: contactId,
        receiverId: session.user.email,
        content: 'When you\'re backed against the wall, break the god damn thing down.',
        timestamp: '10:50 AM',
        messageType: 'text' as const,
        fileUrl: null,
        fileName: null,
        isRead: true
      },
      {
        id: '3',
        conversationId: contactId,
        senderId: contactId,
        receiverId: session.user.email,
        content: 'Excuses don\'t win championships.',
        timestamp: '10:51 AM',
        messageType: 'text' as const,
        fileUrl: null,
        fileName: null,
        isRead: true
      },
      {
        id: '4',
        conversationId: contactId,
        senderId: session.user.email,
        receiverId: contactId,
        content: 'Oh yeah, did Michael Jordan tell you that?',
        timestamp: '10:53 AM',
        messageType: 'text' as const,
        fileUrl: null,
        fileName: null,
        isRead: true
      },
      {
        id: '5',
        conversationId: contactId,
        senderId: contactId,
        receiverId: session.user.email,
        content: 'No, I told him that.',
        timestamp: '10:55 AM',
        messageType: 'text' as const,
        fileUrl: null,
        fileName: null,
        isRead: true
      }
    ];

    const mockConversation = {
      id: contactId,
      contact: {
        id: contactId,
        name: contactId === '2' ? 'Harvey Specter' : 'John Doe',
        jobTitle: contactId === '2' ? 'PHP Developer' : 'Software Engineer',
        avatar: contactId === '2' ? '/assets/image/harveyspecter.png' : '/assets/image/louislitt.png',
        status: 'online' as const
      },
      messages: mockMessages
    };

    return NextResponse.json({
      success: true,
      data: mockConversation
    });

  } catch (error) {
    console.error('Error fetching conversation:', error);
    return NextResponse.json(
      { 
        error: error instanceof Error ? error.message : 'Failed to fetch conversation',
        type: 'server_error'
      },
      { status: 500 }
    );
  }
}