import {
  Body,
  Controller,
  Get,
  Param,
  Patch,
  Post,
  Req,
  UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { LabOrgGuard } from '../../common/guards/lab-org.guard';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import {
  CreateLabCaseCommentDto,
  SetCommentVisibilityDto,
} from './dto/lab-case-comment.dto';
import { LabCaseCommentsService } from './lab-case-comments.service';

@ApiTags('case-comments')
@ApiBearerAuth('JWT-auth')
@UseGuards(JwtAuthGuard, LabOrgGuard)
@Controller('case-comments')
export class LabCaseCommentsController {
  constructor(private readonly service: LabCaseCommentsService) {}

  private orgId(req: { user: { organizationId?: string } }) {
    return req.user.organizationId as string;
  }

  @Get(':caseId')
  @ApiOperation({ summary: 'List comments for a lab case (lab side)' })
  list(@Param('caseId') caseId: string, @Req() req) {
    return this.service.listForLabViewer(caseId, this.orgId(req), req.user.id);
  }

  @Post(':caseId')
  @ApiOperation({ summary: 'Add a comment to a lab case (lab side)' })
  add(
    @Param('caseId') caseId: string,
    @Body() dto: CreateLabCaseCommentDto,
    @Req() req,
  ) {
    return this.service.addForLab(caseId, this.orgId(req), req.user.id, dto);
  }

  @Patch('item/:commentId/visibility')
  @ApiOperation({ summary: 'Toggle whether a comment is visible to the clinic' })
  setVisibility(
    @Param('commentId') commentId: string,
    @Body() dto: SetCommentVisibilityDto,
    @Req() req,
  ) {
    return this.service.setVisibility(
      commentId,
      this.orgId(req),
      req.user.id,
      dto.visibleToClinic,
    );
  }
}
