import { NextRequest, NextResponse } from "next/server"
import { getSessionUser } from "@/lib/auth"
import { query } from "@/lib/db"

function stageToStatus(name: string): string {
  switch (name) {
    case "New": return "open"
    case "Contacted": return "contacted"
    case "Qualified":
    case "Interested":
    case "Demo Scheduled":
    case "Negotiation": return "pending"
    case "Closed Won": return "closed"
    case "Closed Lost": return "ignored"
    default: return "open"
  }
}

function getPeriodDateRange(period: string): { start: Date; end: Date } | null {
  if (period === "all") return null
  const end = new Date()
  let start: Date
  switch (period) {
    case "7days": start = new Date(end); start.setDate(start.getDate() - 7); break
    case "30days": start = new Date(end); start.setDate(start.getDate() - 30); break
    case "12months": start = new Date(end); start.setFullYear(start.getFullYear() - 12); break
    case "6months": default: start = new Date(end); start.setMonth(start.getMonth() - 6); break
  }
  return { start, end }
}

export async function GET(request: NextRequest) {
  try {
    const user = await getSessionUser()
    if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })

    const { searchParams } = new URL(request.url)
    const search = searchParams.get("search") || ""
    const status = searchParams.get("status") || "all"
    const period = searchParams.get("period") || "all"

    let sql = `SELECT l.id, l.company_name, l.contact_name, l.email, l.phone, l.score,
                      l.assigned_to, l.created_at, l.updated_at, l.notes, l.source_id,
                      ls.name AS stage_name,
                      u.id AS user_id, u.first_name, u.last_name, u.email AS user_email, u.avatar_url
               FROM leads l
               JOIN lead_stages ls ON ls.id = l.stage_id
               LEFT JOIN users u ON u.id = l.assigned_to
               WHERE l.deleted_at IS NULL`

    const params: any[] = []
    let paramIdx = 1

    if (period !== "all") {
      const range = getPeriodDateRange(period)
      if (range) {
        sql += ` AND l.created_at >= $${paramIdx} AND l.created_at <= $${paramIdx + 1}`
        params.push(range.start.toISOString(), range.end.toISOString())
        paramIdx += 2
      }
    }

    if (search) {
      sql += ` AND (l.contact_name ILIKE $${paramIdx} OR l.company_name ILIKE $${paramIdx} OR l.email ILIKE $${paramIdx} OR l.phone ILIKE $${paramIdx})`
      params.push(`%${search}%`)
      paramIdx++
    }

    sql += ` ORDER BY l.created_at DESC`

    const result = await query(sql, params)

    let leads = result.rows.map((r: any) => {
      const s = stageToStatus(r.stage_name)
      return {
        id: r.id,
        companyName: r.company_name || "",
        contactName: r.contact_name,
        email: r.email || "",
        phone: r.phone || "",
        source: "",
        description: r.notes || "",
        status: s,
        assignedUserId: r.assigned_to,
        assignedUser: r.assigned_to ? {
          id: r.user_id,
          name: `${r.first_name} ${r.last_name}`,
          email: r.user_email,
          avatar: r.avatar_url || `https://ui-avatars.com/api/?name=${encodeURIComponent(`${r.first_name} ${r.last_name}`)}&background=1d4ed8&color=fff&size=128`,
        } : null,
        createdAt: r.created_at,
        updatedAt: r.updated_at,
      }
    })

    if (status !== "all") {
      leads = leads.filter((l: any) => l.status === status)
    }

    return NextResponse.json(leads)
  } catch (error) {
    console.error("Leads API error:", error)
    return NextResponse.json({ error: "Failed to load leads" }, { status: 500 })
  }
}
