import { Controller, Post, Body, Get, Param, HttpCode } from '@nestjs/common';
import { ApiTags, ApiOperation } from '@nestjs/swagger';
import { IsString, IsOptional } from 'class-validator';
import { ReferralService } from '../../../application/services/referral.service';

class CreateProfileDto {
  @IsString()
  userId: string;

  @IsOptional()
  @IsString()
  usedCode?: string;
}

/**
 * 内网专用接口 (不经过 Kong 对外暴露)
 * 供 auth-service 在无 Kafka 时直接调用
 */
@ApiTags('Internal - Referral')
@Controller('internal/referral')
export class InternalReferralController {
  constructor(private readonly referralService: ReferralService) {}

  @Post('profiles')
  @HttpCode(201)
  @ApiOperation({ summary: '[内网] 创建推荐档案 (注册成功后调用)' })
  async createProfile(@Body() dto: CreateProfileDto) {
    const result = await this.referralService.createProfile({
      userId: dto.userId,
      usedCode: dto.usedCode ?? null,
    });
    return { code: 0, data: result };
  }

  @Get('profiles/:userId')
  @ApiOperation({ summary: '[内网] 获取用户推荐档案' })
  async getProfile(@Param('userId') userId: string) {
    const info = await this.referralService.getMyInfo(userId);
    return { code: 0, data: info };
  }
}
