import {
  Body,
  Controller,
  Delete,
  Get,
  HttpCode,
  Param,
  ParseUUIDPipe,
  Patch,
  Post,
} from '@nestjs/common';
import type { AuthenticatedUser } from '../../common/auth/authenticated-user';
import { CurrentUser } from '../../common/auth/current-user.decorator';
import { CreateTradeDto, UpdateTradeDto } from './dto/trade.dto';
import { CreateTradingAccountDto, UpdateTradingAccountDto } from './dto/trading-account.dto';
import { TradingService } from './trading.service';

@Controller('trading')
export class TradingController {
  constructor(private readonly trading: TradingService) {}

  @Get('accounts')
  listAccounts(@CurrentUser() user: AuthenticatedUser) {
    return this.trading.listAccounts(user.id);
  }

  @Post('accounts')
  createAccount(@CurrentUser() user: AuthenticatedUser, @Body() input: CreateTradingAccountDto) {
    return this.trading.createAccount(user.id, input);
  }

  @Patch('accounts/:accountId')
  updateAccount(
    @CurrentUser() user: AuthenticatedUser,
    @Param('accountId', ParseUUIDPipe) accountId: string,
    @Body() input: UpdateTradingAccountDto,
  ) {
    return this.trading.updateAccount(user.id, accountId, input);
  }

  @Delete('accounts/:accountId')
  @HttpCode(204)
  deleteAccount(
    @CurrentUser() user: AuthenticatedUser,
    @Param('accountId', ParseUUIDPipe) accountId: string,
  ) {
    return this.trading.deleteAccount(user.id, accountId);
  }

  @Get('accounts/:accountId/trades')
  listTrades(
    @CurrentUser() user: AuthenticatedUser,
    @Param('accountId', ParseUUIDPipe) accountId: string,
  ) {
    return this.trading.listTrades(user.id, accountId);
  }

  @Post('accounts/:accountId/trades')
  createTrade(
    @CurrentUser() user: AuthenticatedUser,
    @Param('accountId', ParseUUIDPipe) accountId: string,
    @Body() input: CreateTradeDto,
  ) {
    return this.trading.createTrade(user.id, accountId, input);
  }

  @Patch('trades/:tradeId')
  updateTrade(
    @CurrentUser() user: AuthenticatedUser,
    @Param('tradeId', ParseUUIDPipe) tradeId: string,
    @Body() input: UpdateTradeDto,
  ) {
    return this.trading.updateTrade(user.id, tradeId, input);
  }

  @Delete('trades/:tradeId')
  @HttpCode(204)
  deleteTrade(
    @CurrentUser() user: AuthenticatedUser,
    @Param('tradeId', ParseUUIDPipe) tradeId: string,
  ) {
    return this.trading.deleteTrade(user.id, tradeId);
  }
}
