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 createConversationSchema = z.object({
  title: z.string().min(1).max(200),
  language: z.enum(['ro', 'en']).default('ro'),
})

const getConversationsSchema = z.object({
  language: z.enum(['ro', 'en']).optional(),
  limit: z.string().optional().default('20').transform(Number).pipe(z.number().min(1).max(50)),
  offset: z.string().optional().default('0').transform(Number).pipe(z.number().min(0)),
})

// GET /api/chat/conversations - List user's conversations
export async function GET(request: Request) {
  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

    // Parse query parameters
    const url = new URL(request.url)
    const queryParams = {
      language: url.searchParams.get('language') || undefined,
      limit: url.searchParams.get('limit') || '20',
      offset: url.searchParams.get('offset') || '0'
    }
    const { language, limit, offset } = getConversationsSchema.parse(queryParams)

    // Build where clause
    const where: any = {
      userId,
      isActive: true
    }

    if (language) {
      where.language = language
    }

    // Get conversations with message count
    const conversations = await prisma.chatConversation.findMany({
      where,
      orderBy: { lastMessageAt: 'desc' },
      take: limit,
      skip: offset,
      include: {
        _count: {
          select: { messages: true }
        },
        messages: {
          take: 1,
          orderBy: { timestamp: 'desc' },
          select: {
            content: true,
            role: true,
            timestamp: true
          }
        }
      }
    })

    // Get total count for pagination
    const total = await prisma.chatConversation.count({ where })

    return NextResponse.json({
      success: true,
      conversations: conversations.map(conv => ({
        id: conv.id,
        title: conv.title,
        language: conv.language,
        messageCount: conv._count.messages,
        lastMessage: conv.messages[0] || null,
        lastMessageAt: conv.lastMessageAt,
        createdAt: conv.createdAt
      })),
      pagination: {
        total,
        limit,
        offset,
        hasMore: offset + limit < total
      }
    })

  } catch (error) {
    console.error('Error fetching conversations:', error)

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

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

// POST /api/chat/conversations - Create new conversation
export async function POST(request: Request) {
  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

    // Parse request body
    const body = await request.json()
    const { title, language } = createConversationSchema.parse(body)

    // Create new conversation
    const conversation = await prisma.chatConversation.create({
      data: {
        userId,
        title,
        language,
        lastMessageAt: 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,
        lastMessage: null,
        lastMessageAt: conversation.lastMessageAt,
        createdAt: conversation.createdAt
      }
    }, { status: 201 })

  } catch (error) {
    console.error('Error creating 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 create conversation'
      },
      { status: 500 }
    )
  }
}
