import { db } from '@sim/db'
import { credentialSet, credentialSetInvitation, organization, user } from '@sim/db/schema'
import { createLogger } from '@sim/logger'
import { and, eq, gt } from 'drizzle-orm'
import { NextResponse } from 'next/server'
import { getSession } from '@/lib/auth'
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'

const logger = createLogger('CredentialSetInvitations')

export const GET = withRouteHandler(async () => {
  const session = await getSession()

  if (!session?.user?.id || !session?.user?.email) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
  }

  try {
    // Scope to invitations addressed to the caller's own email only. Open-link
    // (null-email) invites carry a bearer token redeemed via the out-of-band URL
    // and must never be listed, or any user could accept another org's invite.
    const invitations = await db
      .select({
        invitationId: credentialSetInvitation.id,
        token: credentialSetInvitation.token,
        status: credentialSetInvitation.status,
        expiresAt: credentialSetInvitation.expiresAt,
        createdAt: credentialSetInvitation.createdAt,
        credentialSetId: credentialSet.id,
        credentialSetName: credentialSet.name,
        providerId: credentialSet.providerId,
        organizationId: organization.id,
        organizationName: organization.name,
        invitedByName: user.name,
        invitedByEmail: user.email,
      })
      .from(credentialSetInvitation)
      .innerJoin(credentialSet, eq(credentialSetInvitation.credentialSetId, credentialSet.id))
      .innerJoin(organization, eq(credentialSet.organizationId, organization.id))
      .leftJoin(user, eq(credentialSetInvitation.invitedBy, user.id))
      .where(
        and(
          eq(credentialSetInvitation.email, session.user.email),
          eq(credentialSetInvitation.status, 'pending'),
          gt(credentialSetInvitation.expiresAt, new Date())
        )
      )

    return NextResponse.json({ invitations })
  } catch (error) {
    logger.error('Error fetching credential set invitations', error)
    return NextResponse.json({ error: 'Failed to fetch invitations' }, { status: 500 })
  }
})
