import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Body,
  Param,
  ParseUUIDPipe,
  HttpCode,
  HttpStatus,
  UseGuards,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiBearerAuth,
  ApiParam,
} from '@nestjs/swagger';
import { RelationshipTypesService } from './relationship-types.service';
import { CreateRelationshipTypeDto } from './dto/create-relationship-type.dto';
import { UpdateRelationshipTypeDto } from './dto/update-relationship-type.dto';
import { CurrentUser, JwtPayload } from '../common/decorators';
import { TenantGuard } from '../auth/guards/tenant.guard';
import { singleResponse } from '../common/dto/pagination.dto';

@ApiTags('Relationship Types')
@ApiBearerAuth('access-token')
@UseGuards(TenantGuard)
@Controller('relationship-types')
export class RelationshipTypesController {
  constructor(
    private readonly relationshipTypesService: RelationshipTypesService,
  ) {}

  @Post()
  @HttpCode(HttpStatus.CREATED)
  @ApiOperation({ summary: 'Beziehungstyp erstellen' })
  async create(
    @CurrentUser() user: JwtPayload,
    @Body() dto: CreateRelationshipTypeDto,
  ) {
    const relType = await this.relationshipTypesService.create(
      user.tenantId!,
      dto,
    );
    return singleResponse(relType);
  }

  @Get()
  @ApiOperation({ summary: 'Alle Beziehungstypen auflisten' })
  async findAll(@CurrentUser() user: JwtPayload) {
    const relTypes = await this.relationshipTypesService.findAll(
      user.tenantId!,
    );
    return { data: relTypes };
  }

  @Get(':id')
  @ApiOperation({ summary: 'Beziehungstyp abrufen' })
  @ApiParam({ name: 'id', type: 'string', format: 'uuid' })
  async findOne(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    const relType = await this.relationshipTypesService.findOne(
      user.tenantId!,
      id,
    );
    return singleResponse(relType);
  }

  @Patch(':id')
  @ApiOperation({ summary: 'Beziehungstyp aktualisieren' })
  @ApiParam({ name: 'id', type: 'string', format: 'uuid' })
  async update(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
    @Body() dto: UpdateRelationshipTypeDto,
  ) {
    const relType = await this.relationshipTypesService.update(
      user.tenantId!,
      id,
      dto,
    );
    return singleResponse(relType);
  }

  @Delete(':id')
  @ApiOperation({ summary: 'Beziehungstyp loeschen' })
  @ApiParam({ name: 'id', type: 'string', format: 'uuid' })
  async remove(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    const relType = await this.relationshipTypesService.remove(
      user.tenantId!,
      id,
    );
    return singleResponse(relType);
  }
}
