/**
 * GET /api/v1/admin/subscriptions
 *
 * List all subscriptions with pagination.
 *
 * Query Parameters:
 *   - limit: number (default: 50, max: 250)
 *   - offset: number (default: 0)
 *   - plan: string (optional) - Filter by plan (free, pro, team, enterprise)
 *   - status: string (optional) - Filter by status (active, canceled, etc.)
 *
 * Response: AdminListResponse<AdminSubscription>
 */

import { db } from '@sim/db'
import { subscription } from '@sim/db/schema'
import { createLogger } from '@sim/logger'
import { and, count, eq, type SQL } from 'drizzle-orm'
import { adminV1ListSubscriptionsContract } from '@/lib/api/contracts/v1/admin'
import { parseRequest } from '@/lib/api/server'
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
import { withAdminAuth } from '@/app/api/v1/admin/middleware'
import {
  adminValidationErrorResponse,
  internalErrorResponse,
  listResponse,
} from '@/app/api/v1/admin/responses'
import {
  type AdminSubscription,
  createPaginationMeta,
  toAdminSubscription,
} from '@/app/api/v1/admin/types'

const logger = createLogger('AdminSubscriptionsAPI')

export const GET = withRouteHandler(
  withAdminAuth(async (request) => {
    const parsed = await parseRequest(
      adminV1ListSubscriptionsContract,
      request,
      {},
      {
        validationErrorResponse: adminValidationErrorResponse,
      }
    )
    if (!parsed.success) return parsed.response

    const { limit, offset, plan: planFilter, status: statusFilter } = parsed.data.query

    try {
      const conditions: SQL<unknown>[] = []
      if (planFilter) {
        conditions.push(eq(subscription.plan, planFilter))
      }
      if (statusFilter) {
        conditions.push(eq(subscription.status, statusFilter))
      }

      const whereClause = conditions.length > 0 ? and(...conditions) : undefined

      const [countResult, subscriptions] = await Promise.all([
        db.select({ total: count() }).from(subscription).where(whereClause),
        db
          .select()
          .from(subscription)
          .where(whereClause)
          .orderBy(subscription.plan)
          .limit(limit)
          .offset(offset),
      ])

      const total = countResult[0].total
      const data: AdminSubscription[] = subscriptions.map(toAdminSubscription)
      const pagination = createPaginationMeta(total, limit, offset)

      logger.info(`Admin API: Listed ${data.length} subscriptions (total: ${total})`)

      return listResponse(data, pagination)
    } catch (error) {
      logger.error('Admin API: Failed to list subscriptions', { error })
      return internalErrorResponse('Failed to list subscriptions')
    }
  })
)
