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

@ApiTags('DealTypes')
@ApiBearerAuth('access-token')
@UseGuards(TenantGuard)
@Controller('deal-types')
export class DealTypesController {
  constructor(private readonly dealTypesService: DealTypesService) {}

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

  @Get()
  @ApiOperation({ summary: 'Alle Vorgangsarten auflisten' })
  async findAll(@CurrentUser() user: JwtPayload) {
    const dealTypes = await this.dealTypesService.findAll(user.tenantId!);
    return { data: dealTypes };
  }

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

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

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