import { Controller, Get, Post, Body, UseGuards, Request } from '@nestjs/common';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { AiConfigService } from './ai-config.service';
import { CreateAiConfigDto } from './dto/create-ai-config.dto';
import { UnauthorizedException } from '@nestjs/common';

interface UserRequest extends Express.Request {
  user?: {
    email: string;
    workspaceId: number;
  };
}

@Controller('ai/config')
@UseGuards(JwtAuthGuard)
export class AiConfigController {
  constructor(private readonly aiConfigService: AiConfigService) {}

  @Get()
  getAiConfig(@Request() req: UserRequest) {
    if (!req.user?.workspaceId) {
      throw new UnauthorizedException('用户未关联工作区');
    }
    return this.aiConfigService.getAiConfigByWorkspace(req.user.workspaceId);
  }

  @Post()
  createAiConfig(
    @Request() req: UserRequest,
    @Body() createAiConfigDto: CreateAiConfigDto,
  ) {
    if (!req.user?.workspaceId) {
      throw new UnauthorizedException('用户未关联工作区');
    }
    return this.aiConfigService.createAiConfig(createAiConfigDto, req.user.workspaceId);
  }
}