import { Controller, Get, Post, Body, Param, UseGuards, Request } from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
import { TimeWalletService } from './time-wallet.service';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { IsInt, IsString, Min, Max } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';

class ManualAdjustmentDto {
  @ApiProperty({ example: 15 })
  @IsInt()
  @Min(-120)
  @Max(120)
  minutes: number;

  @ApiProperty({ example: 'Bom comportamento hoje' })
  @IsString()
  reason: string;
}

@ApiTags('time-wallet')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller({ path: 'time-wallet', version: '1' })
export class TimeWalletController {
  constructor(private readonly service: TimeWalletService) {}

  @Get('child/:childId')
  @ApiOperation({ summary: 'Snapshot da carteira da criança' })
  async getWallet(@Param('childId') childId: string) {
    return this.service.getWallet(childId);
  }

  @Get('child/:childId/transactions')
  @ApiOperation({ summary: 'Histórico de transações' })
  async getTransactions(@Param('childId') childId: string) {
    return this.service.getTransactions(childId);
  }

  @Post('child/:childId/adjust')
  @ApiOperation({ summary: 'Ajuste manual de tempo (responsável)' })
  async adjust(
    @Param('childId') childId: string,
    @Body() dto: ManualAdjustmentDto,
    @Request() req: any,
  ) {
    return this.service.manualAdjustment(childId, dto.minutes, dto.reason, req.user.sub);
  }
}
