import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
} from "@nestjs/common";
import {
  TextbooksService,
  type CreateTextbookInput,
  type UpdateTextbookInput,
} from "./textbooks.service.js";
import type { Textbook } from "./textbooks.schema.js";
import {
  Permissions,
  RequirePermission,
} from "../middleware/permission.guard.js";

@Controller("textbooks")
export class TextbooksController {
  constructor(private readonly service: TextbooksService) {}

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

  @Get()
  @RequirePermission(Permissions.CONTENT_TEXTBOOK_READ)
  async list(): Promise<{ success: true; data: Textbook[] }> {
    const data = await this.service.list();
    return { success: true, data };
  }

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

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

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