import { NextResponse } from 'next/server'
import { z } from 'zod'
import { PrismaClient } from '@prisma/client'
import { verifyToken } from '@/lib/auth'

const prisma = new PrismaClient()

export const runtime = 'nodejs'

const updateConversationSchema = z.object({
  title: z.string().min(1).max(200).optional(),
  isActive: z.boolean().optional(),
})

// GET /api/chat/conversations/[id] - Get conversation with messages
export async function GET(
  request: Request,
  { params }: any
) {
  try {
    // Get user from authentication
    const authHeader = request.headers.get('authorization')
    if (!authHeader?.startsWith('Bearer ')) {
      return NextResponse.json(
        { success: false, error: 'Authentication required' },
        { status: 401 }
      )
    }

    const token = authHeader.substring(7)
    const payload = await verifyToken(token)

    if (!payload) {
      return NextResponse.json(
        { success: false, error: 'Invalid token' },
        { status: 401 }
      )
    }

    const userId = payload.userId as string
    const conversationId = params.id

    // Get conversation with messages
    const conversation = await prisma.chatConversation.findUnique({
      where: {
        id: conversationId,
        userId, // Ensure user owns this conversation
        isActive: true
      },
      include: {
        messages: {
          orderBy: { timestamp: 'asc' },
          select: {
            id: true,
            role: true,
            content: true,
            timestamp: true,
            metadata: true
          }
        }
      }
    })

    if (!conversation) {
      return NextResponse.json(
        { success: false, error: 'Conversation not found' },
        { status: 404 }
      )
    }

    return NextResponse.json({
      success: true,
      conversation: {
        id: conversation.id,
        title: conversation.title,
        language: conversation.language,
        createdAt: conversation.createdAt,
        lastMessageAt: conversation.lastMessageAt,
        messages: conversation.messages.map(msg => ({
          id: msg.id,
          role: msg.role.toLowerCase(), // Convert enum to lowercase for frontend compatibility
          content: msg.content,
          timestamp: msg.timestamp,
          metadata: msg.metadata
        }))
      }
    })

  } catch (error) {
    console.error('Error fetching conversation:', error)
    return NextResponse.json(
      {
        success: false,
        error: 'Failed to fetch conversation'
      },
      { status: 500 }
    )
  }
}

// PUT /api/chat/conversations/[id] - Update conversation
export async function PUT(
  request: Request,
  { params }: any
) {
  try {
    // Get user from authentication
    const authHeader = request.headers.get('authorization')
    if (!authHeader?.startsWith('Bearer ')) {
      return NextResponse.json(
        { success: false, error: 'Authentication required' },
        { status: 401 }
      )
    }

    const token = authHeader.substring(7)
    const payload = await verifyToken(token)

    if (!payload) {
      return NextResponse.json(
        { success: false, error: 'Invalid token' },
        { status: 401 }
      )
    }

    const userId = payload.userId as string
    const conversationId = params.id

    // Parse request body
    const body = await request.json()
    const updateData = updateConversationSchema.parse(body)

    // Verify conversation ownership
    const existingConversation = await prisma.chatConversation.findUnique({
      where: {
        id: conversationId,
        userId
      }
    })

    if (!existingConversation) {
      return NextResponse.json(
        { success: false, error: 'Conversation not found' },
        { status: 404 }
      )
    }

    // Update conversation
    const conversation = await prisma.chatConversation.update({
      where: { id: conversationId },
      data: {
        ...updateData,
        updatedAt: new Date()
      },
      include: {
        _count: {
          select: { messages: true }
        }
      }
    })

    return NextResponse.json({
      success: true,
      conversation: {
        id: conversation.id,
        title: conversation.title,
        language: conversation.language,
        messageCount: conversation._count.messages,
        lastMessageAt: conversation.lastMessageAt,
        createdAt: conversation.createdAt,
        isActive: conversation.isActive
      }
    })

  } catch (error) {
    console.error('Error updating conversation:', error)

    if (error instanceof z.ZodError) {
      return NextResponse.json(
        {
          success: false,
          error: 'Invalid request format',
          details: error.errors
        },
        { status: 400 }
      )
    }

    return NextResponse.json(
      {
        success: false,
        error: 'Failed to update conversation'
      },
      { status: 500 }
    )
  }
}

// DELETE /api/chat/conversations/[id] - Delete conversation
export async function DELETE(
  request: Request,
  { params }: any
) {
  try {
    // Get user from authentication
    const authHeader = request.headers.get('authorization')
    if (!authHeader?.startsWith('Bearer ')) {
      return NextResponse.json(
        { success: false, error: 'Authentication required' },
        { status: 401 }
      )
    }

    const token = authHeader.substring(7)
    const payload = await verifyToken(token)

    if (!payload) {
      return NextResponse.json(
        { success: false, error: 'Invalid token' },
        { status: 401 }
      )
    }

    const userId = payload.userId as string
    const conversationId = params.id

    // Verify conversation ownership
    const existingConversation = await prisma.chatConversation.findUnique({
      where: {
        id: conversationId,
        userId
      }
    })

    if (!existingConversation) {
      return NextResponse.json(
        { success: false, error: 'Conversation not found' },
        { status: 404 }
      )
    }

    // Soft delete (mark as inactive) instead of hard delete to preserve data
    await prisma.chatConversation.update({
      where: { id: conversationId },
      data: {
        isActive: false,
        updatedAt: new Date()
      }
    })

    return NextResponse.json({
      success: true,
      message: 'Conversation deleted successfully'
    })

  } catch (error) {
    console.error('Error deleting conversation:', error)
    return NextResponse.json(
      {
        success: false,
        error: 'Failed to delete conversation'
      },
      { status: 500 }
    )
  }
}
