// 🆕 Certificate Approvals controller
//   Separate URL base `certificate-approvals` so the global PermissionGuard
//   maps these routes to the `certificate-approvals` MODULE (not `certificates`).
//   Reuses CertificatesService — no business logic duplicated.
//   The guard is NOT modified; it auto-resolves the module from the URL segment.
import {
  Controller,
  Get,
  Post,
  Param,
  Body,
  Query,
  Req,
  ParseIntPipe,
} from '@nestjs/common';
import { CertificatesService } from './certificates.service';

@Controller('certificate-approvals')
export class CertificateApprovalsController {
  constructor(private readonly service: CertificatesService) {}

  // GET /api/certificate-approvals/queue?page=&search=&approved=
  @Get('queue')
  async queue(
    @Req() req: any,
    @Query('page', new ParseIntPipe({ optional: true })) page = 1,
    @Query('search') search?: string,
    @Query('approved') approved?: 'all' | 'approved' | 'pending',
  ) {
    return this.service.qaqcQueue(req.user?.id, page, search, approved ?? 'all');
  }

  // POST /api/certificate-approvals/:id/approve
  @Post(':id/approve')
  async approve(@Param('id', ParseIntPipe) id: number, @Req() req: any) {
    return this.service.qaqcApprove(id, req.user?.id);
  }

  // POST /api/certificate-approvals/:id/reject   body: { reason?: string }
  @Post(':id/reject')
  async reject(
    @Param('id', ParseIntPipe) id: number,
    @Body() body: { reason?: string },
    @Req() req: any,
  ) {
    return this.service.qaqcReject(id, req.user?.id, body?.reason);
  }
}