import { Body, Controller, Delete, ForbiddenException, Get, Param, Post, Query } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { ResidentsService } from './residents.service';
import { CreateResidentDto } from './dto/create-resident.dto';
import { Roles } from '../../common/decorators/roles.decorator';
import { CurrentUser, AuthenticatedUser } from '../../common/decorators/current-user.decorator';

@ApiTags('Residents')
@ApiBearerAuth()
@Controller('residents')
export class ResidentsController {
  constructor(private readonly residentsService: ResidentsService) {}

  @Roles('SYNDIC')
  @Post()
  create(@CurrentUser() user: AuthenticatedUser, @Body() dto: CreateResidentDto) {
    if (!user.tenantId) throw new ForbiddenException();
    return this.residentsService.create(user.tenantId, dto);
  }

  @Get()
  list(@Query('lotId') lotId: string) {
    return this.residentsService.findByLot(lotId);
  }

  @Roles('SYNDIC')
  @Delete(':id')
  endResidency(@Param('id') id: string) {
    return this.residentsService.endResidency(id);
  }
}
