import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
} from "@nestjs/common";
import {
  QuestionsService,
  type CreateQuestionInput,
  type UpdateQuestionInput,
} from "./questions.service.js";
import type { Question } from "./questions.schema.js";
import {
  Permissions,
  RequirePermission,
} from "../middleware/permission.guard.js";

@Controller("questions")
export class QuestionsController {
  constructor(private readonly service: QuestionsService) {}

  @Post()
  @RequirePermission(Permissions.CONTENT_QUESTION_CREATE)
  async create(
    @Body() body: CreateQuestionInput,
  ): Promise<{ success: true; data: { id: string } }> {
    const result = await this.service.createQuestion(body);
    return { success: true, data: result };
  }

  @Get("knowledge-point/:knowledgePointId")
  @RequirePermission(Permissions.CONTENT_QUESTION_READ)
  async listByKnowledgePoint(
    @Param("knowledgePointId") knowledgePointId: string,
  ): Promise<{ success: true; data: Question[] }> {
    const data = await this.service.listByKnowledgePoint(knowledgePointId);
    return { success: true, data };
  }

  @Get(":id")
  @RequirePermission(Permissions.CONTENT_QUESTION_READ)
  async getById(
    @Param("id") id: string,
  ): Promise<{ success: true; data: Question }> {
    const data = await this.service.getQuestion(id);
    return { success: true, data };
  }

  @Put(":id")
  @RequirePermission(Permissions.CONTENT_QUESTION_UPDATE)
  async update(
    @Param("id") id: string,
    @Body() body: UpdateQuestionInput,
  ): Promise<{ success: true; data: { success: true } }> {
    await this.service.updateQuestion(id, body);
    return { success: true, data: { success: true } };
  }

  @Delete(":id")
  @RequirePermission(Permissions.CONTENT_QUESTION_DELETE)
  async remove(
    @Param("id") id: string,
  ): Promise<{ success: true; data: { success: true } }> {
    await this.service.deleteQuestion(id);
    return { success: true, data: { success: true } };
  }
}
