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

@ApiTags('Account Types')
@ApiBearerAuth('access-token')
@UseGuards(TenantGuard)
@Controller('account-types')
export class AccountTypesController {
  constructor(private readonly accountTypesService: AccountTypesService) {}

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

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

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

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

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