import {
  Body,
  Controller,
  HttpCode,
  HttpStatus,
  Param,
  Post,
  Req,
  UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { Roles } from 'src/roles/roles.decorator';
import { RoleEnum } from 'src/roles/roles.enum';
import { RolesGuard } from 'src/roles/roles.guard';
import { CreateTransactionDto } from './dto/create-transaction.dto';
import { TransactionsService } from './transactions.service';

@ApiTags('Transactions')
@ApiBearerAuth()
@UseGuards(AuthGuard('jwt'), RolesGuard)
@Controller({
  path: 'transactions',
  version: '1',
})
export class TransactionsController {
  constructor(private transactionsService: TransactionsService) {}

  @Roles([RoleEnum.admin, RoleEnum.user])
  @Post()
  @HttpCode(HttpStatus.CREATED)
  create(@Req() req, @Body() createTransactionDtos: CreateTransactionDto[]) {
    const userId = req.user?.id;
    return this.transactionsService.createTransactions(
      createTransactionDtos,
      userId,
    );
  }

  @ApiOperation({
    summary: 'Buchung stornieren',
    description:
      'Erstellt eine Ausgleichsbuchung, die eine fehlerhafte Buchung rückgängig macht. Die Originalbuchung bleibt zur Nachvollziehbarkeit erhalten.',
  })
  @Roles([RoleEnum.admin, RoleEnum.user])
  @Post(':id/reverse')
  @HttpCode(HttpStatus.CREATED)
  reverse(@Req() req, @Param('id') id: string) {
    const userId = req.user?.id;
    return this.transactionsService.reverse(Number(id), userId);
  }
}
