import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Query,
  Req,
  UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { ClinicOrgGuard } from '../../common/guards/clinic-org.guard';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { AppointmentsService } from './appointments.service';
import { ColumnProvidersQueryDto } from './dto/column-providers-query.dto';
import { CreateAppointmentDto } from './dto/create-appointment.dto';
import { ListAppointmentsDto } from './dto/list-appointments.dto';
import { UpdateAppointmentDto } from './dto/update-appointment.dto';

@ApiTags('appointments')
@ApiBearerAuth('JWT-auth')
@UseGuards(JwtAuthGuard, ClinicOrgGuard)
@Controller('appointments')
export class AppointmentsController {
  constructor(private readonly appointmentsService: AppointmentsService) {}

  @Get('column-providers')
  @ApiOperation({
    summary:
      'Staff columns: active non-owner members with TAB_TREATMENT_EDIT. Owners are excluded. Requires TAB_APPOINTMENTS_READ or TAB_APPOINTMENTS_EDIT, or owner.',
  })
  columnProviders(
    @Query() query: ColumnProvidersQueryDto,
    @Req() req: { user: { id: string; organizationId?: string } },
  ) {
    const organizationId = this.appointmentsService.getOrganizationIdFromUser(req.user);
    return this.appointmentsService.listColumnProviders(
      organizationId,
      req.user.id,
      query.date,
    );
  }

  @Get()
  @ApiOperation({ summary: 'List appointments intersecting a time range (requires TAB_APPOINTMENTS_READ or owner)' })
  list(@Query() query: ListAppointmentsDto, @Req() req: { user: { id: string; organizationId?: string } }) {
    const organizationId = this.appointmentsService.getOrganizationIdFromUser(req.user);
    return this.appointmentsService.list(query, organizationId, req.user.id);
  }

  @Post()
  @ApiOperation({ summary: 'Create appointment (requires TAB_APPOINTMENTS_EDIT or owner)' })
  create(
    @Body() dto: CreateAppointmentDto,
    @Req() req: { user: { id: string; organizationId?: string } },
  ) {
    const organizationId = this.appointmentsService.getOrganizationIdFromUser(req.user);
    return this.appointmentsService.create(dto, organizationId, req.user.id);
  }

  @Patch(':id')
  @ApiOperation({ summary: 'Update appointment (requires TAB_APPOINTMENTS_EDIT or owner)' })
  update(
    @Param('id') id: string,
    @Body() dto: UpdateAppointmentDto,
    @Req() req: { user: { id: string; organizationId?: string } },
  ) {
    const organizationId = this.appointmentsService.getOrganizationIdFromUser(req.user);
    return this.appointmentsService.update(id, dto, organizationId, req.user.id);
  }

  @Delete(':id')
  @ApiOperation({ summary: 'Delete appointment (requires TAB_APPOINTMENTS_EDIT or owner)' })
  remove(
    @Param('id') id: string,
    @Req() req: { user: { id: string; organizationId?: string } },
  ) {
    const organizationId = this.appointmentsService.getOrganizationIdFromUser(req.user);
    return this.appointmentsService.remove(id, organizationId, req.user.id);
  }
}
