import { Controller, Get, Param, Query, UseGuards } from "@nestjs/common";
import { JobEventsService } from "./job-events.service";
import { QueryEventsDto } from "./dto";
import { AuthGuard } from "../auth/guards/auth.guard";
import { WorkspaceGuard, PermissionGuard } from "../common/guards";
import { Workspace, Permission, RequirePermission } from "../common/decorators";

/**
 * Controller for job events endpoints
 * Provides read-only access to job events for audit logging
 *
 * Guards are applied in order:
 * 1. AuthGuard - Verifies user authentication
 * 2. WorkspaceGuard - Validates workspace access and sets RLS context
 * 3. PermissionGuard - Checks role-based permissions
 */
@Controller("runner-jobs/:jobId/events")
@UseGuards(AuthGuard, WorkspaceGuard, PermissionGuard)
export class JobEventsController {
  constructor(private readonly jobEventsService: JobEventsService) {}

  /**
   * GET /api/runner-jobs/:jobId/events
   * Get paginated events for a specific job
   * Requires: Any workspace member (including GUEST)
   */
  @Get()
  @RequirePermission(Permission.WORKSPACE_ANY)
  async getEvents(
    @Param("jobId") jobId: string,
    @Query() query: QueryEventsDto,
    @Workspace() _workspaceId: string
  ) {
    return this.jobEventsService.getEventsByJobId(jobId, query);
  }
}
