import { v4 as uuidv4 } from "uuid"

/**
 * Builds the joinConfirmation payload shape for work-session guests.
 * Keeps virtual composition and simulated interaction-link logic out of socketManager.
 */

interface WorkSessionLike {
  _id: string
  userId: string
  createdAt?: Date | string
  updatedAt?: Date | string
  [key: string]: unknown
}

interface CompositionLike {
  title?: string
  description?: string
  tags?: unknown[]
  icon?: string
  slotLayout?: string
  widgetsLayout?: unknown[]
  status?: string
  createdAt?: string
  updatedAt?: string
  [key: string]: unknown
}

/** Virtual composition sent to frontend (pathname.default matches route /:workSession/id=...) */
function buildVirtualComposition(
  composition: CompositionLike,
  workSession: WorkSessionLike,
): Record<string, unknown> {
  const now = new Date()
  return {
    _id: uuidv4(),
    title: composition.title ?? "Shared Session",
    description:
      composition.description ?? "Shared composition for anonymous access",
    tags: composition.tags ?? [],
    icon: composition.icon,
    pathname: { default: "workSession" },
    slotLayout: composition.slotLayout ?? "full",
    widgetsLayout: composition.widgetsLayout ?? [],
    status: composition.status ?? "live",
    author: workSession.userId,
    lastEditor: workSession.userId,
    isDeleted: false,
    createdAt: composition.createdAt ?? now.toISOString(),
    updatedAt: composition.updatedAt ?? now.toISOString(),
  }
}

/** Simulated interaction link so the frontend can resolve the composition like a normal project. */
function buildSimulatedInteractionLink(
  workSession: WorkSessionLike,
  virtualComposition: { _id: string; [key: string]: unknown },
): Record<string, unknown> {
  const created = workSession.createdAt
    ? new Date(workSession.createdAt)
    : new Date()
  const updated = workSession.updatedAt
    ? new Date(workSession.updatedAt)
    : new Date()
  return {
    _id: `session_${workSession._id}_interactionLink`,
    composition: virtualComposition._id,
    slotType: "page",
    triggers: [],
    queryParameters: [],
    description: "Shared session composition",
    tags: [],
    author: workSession.userId,
    lastEditor: workSession.userId,
    isLandingPage: true,
    isDeleted: false,
    createdAt: created,
    updatedAt: updated,
  }
}

export interface WorkSessionJoinPayloadData {
  compositions: unknown[]
  interactionLinks: unknown[]
  projectConfig: Record<string, unknown>
}

/**
 * Build the data part of joinConfirmation for a work session (no token).
 * Socket manager adds token and decoded after creating the JWT.
 */
export function buildWorkSessionJoinPayload(
  workSession: WorkSessionLike,
  composition: CompositionLike,
): WorkSessionJoinPayloadData {
  const virtualComposition = buildVirtualComposition(composition, workSession)
  const simulatedInteractionLink = buildSimulatedInteractionLink(
    workSession,
    virtualComposition as { _id: string; [key: string]: unknown },
  )
  return {
    compositions: [virtualComposition],
    interactionLinks: [simulatedInteractionLink],
    projectConfig: {},
  }
}
