import type { ExecutionContext } from "@nestjs/common";
import { createParamDecorator } from "@nestjs/common";
import type { AuthenticatedRequest, WorkspaceContext as WsContext } from "../types/user.types";

/**
 * Decorator to extract workspace ID from the request.
 * Must be used with WorkspaceGuard which validates and attaches the workspace.
 *
 * @example
 * ```typescript
 * @Get()
 * @UseGuards(AuthGuard, WorkspaceGuard)
 * async getTasks(@Workspace() workspaceId: string) {
 *   // workspaceId is validated and ready to use
 * }
 * ```
 */
export const Workspace = createParamDecorator(
  (_data: unknown, ctx: ExecutionContext): string | undefined => {
    const request = ctx.switchToHttp().getRequest<AuthenticatedRequest>();
    return request.workspace?.id;
  }
);

/**
 * Decorator to extract full workspace context from the request.
 *
 * @example
 * ```typescript
 * @Get()
 * @UseGuards(AuthGuard, WorkspaceGuard)
 * async getTasks(@WorkspaceContext() workspace: { id: string }) {
 *   console.log(workspace.id);
 * }
 * ```
 */
export const WorkspaceContext = createParamDecorator(
  (_data: unknown, ctx: ExecutionContext): WsContext | undefined => {
    const request = ctx.switchToHttp().getRequest<AuthenticatedRequest>();
    return request.workspace;
  }
);
