import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  UseGuards,
  HttpCode,
  HttpStatus,
} from "@nestjs/common";
import { TagsService } from "./tags.service";
import { CreateTagDto, UpdateTagDto } from "./dto";
import { AuthGuard } from "../auth/guards/auth.guard";
import { WorkspaceGuard, PermissionGuard } from "../common/guards";
import { Workspace, Permission, RequirePermission } from "../common/decorators";

@Controller("knowledge/tags")
@UseGuards(AuthGuard, WorkspaceGuard, PermissionGuard)
export class TagsController {
  constructor(private readonly tagsService: TagsService) {}

  @Post()
  @RequirePermission(Permission.WORKSPACE_MEMBER)
  async create(@Body() createTagDto: CreateTagDto, @Workspace() workspaceId: string) {
    return this.tagsService.create(workspaceId, createTagDto);
  }

  @Get()
  @RequirePermission(Permission.WORKSPACE_ANY)
  async findAll(@Workspace() workspaceId: string) {
    return this.tagsService.findAll(workspaceId);
  }

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

  @Put(":slug")
  @RequirePermission(Permission.WORKSPACE_MEMBER)
  async update(
    @Param("slug") slug: string,
    @Body() updateTagDto: UpdateTagDto,
    @Workspace() workspaceId: string
  ) {
    return this.tagsService.update(slug, workspaceId, updateTagDto);
  }

  @Delete(":slug")
  @HttpCode(HttpStatus.NO_CONTENT)
  @RequirePermission(Permission.WORKSPACE_ADMIN)
  async remove(@Param("slug") slug: string, @Workspace() workspaceId: string) {
    await this.tagsService.remove(slug, workspaceId);
  }

  @Get(":slug/entries")
  @RequirePermission(Permission.WORKSPACE_ANY)
  async getEntries(@Param("slug") slug: string, @Workspace() workspaceId: string) {
    return this.tagsService.getEntriesWithTag(slug, workspaceId);
  }
}
