import { Controller, Get, Query, Param, UseGuards } from "@nestjs/common";
import { ActivityService } from "./activity.service";
import { EntityType } from "@prisma/client";
import { QueryActivityLogDto } from "./dto";
import { AuthGuard } from "../auth/guards/auth.guard";
import { WorkspaceGuard, PermissionGuard } from "../common/guards";
import { Workspace, Permission, RequirePermission } from "../common/decorators";

@Controller("activity")
@UseGuards(AuthGuard, WorkspaceGuard, PermissionGuard)
export class ActivityController {
  constructor(private readonly activityService: ActivityService) {}

  @Get()
  @RequirePermission(Permission.WORKSPACE_ANY)
  async findAll(@Query() query: QueryActivityLogDto, @Workspace() workspaceId: string) {
    return this.activityService.findAll(Object.assign({}, query, { workspaceId }));
  }

  @Get("audit/:entityType/:entityId")
  @RequirePermission(Permission.WORKSPACE_ANY)
  async getAuditTrail(
    @Param("entityType") entityType: EntityType,
    @Param("entityId") entityId: string,
    @Workspace() workspaceId: string
  ) {
    return this.activityService.getAuditTrail(workspaceId, entityType, entityId);
  }

  @Get(":id")
  @RequirePermission(Permission.WORKSPACE_ANY)
  async findOne(@Param("id") id: string, @Workspace() workspaceId: string) {
    return this.activityService.findOne(id, workspaceId);
  }
}
