import { Body, Controller, Delete, Get, Param, Post, Put, Query, UseGuards } from "@nestjs/common";
import { JwtAuthGuard } from "../auth/jwt-auth.guard";
import { UpdateFaqDto } from "./dto/update-faq.dto";
import { UpsertFaqDto } from "./dto/upsert-faq.dto";
import { FaqService } from "./faq.service";

@Controller("faqs")
export class FaqController {
  constructor(private readonly faqs: FaqService) {}

  // Public — build-time reads by the frontend (resolveFaqSelection in
  // content-fetch.ts) need this same as /content/pages, no different from
  // what's already visible in the published page source.
  @Get()
  list(@Query("q") q?: string) {
    return this.faqs.list(q);
  }

  @UseGuards(JwtAuthGuard)
  @Post()
  create(@Body() dto: UpsertFaqDto) {
    return this.faqs.create(dto);
  }

  @UseGuards(JwtAuthGuard)
  @Put(":id")
  update(@Param("id") id: string, @Body() dto: UpdateFaqDto) {
    return this.faqs.update(id, dto);
  }

  @UseGuards(JwtAuthGuard)
  @Delete(":id")
  async remove(@Param("id") id: string) {
    await this.faqs.remove(id);
    return { success: true };
  }
}
