// server/src/model-config/model-config.controller.ts
import {
  Body,
  Controller,
  Delete,
  Get,
  HttpCode,
  HttpStatus,
  Param,
  Patch,
  Post,
  Put,
  Req,
  UseGuards,
} from '@nestjs/common';
import { ModelConfigService } from './model-config.service';
import { CreateModelConfigDto } from './dto/create-model-config.dto';
import { UpdateModelConfigDto } from './dto/update-model-config.dto';
import { CombinedAuthGuard } from '../auth/combined-auth.guard';
import { RolesGuard } from '../auth/roles.guard';
import { Roles } from '../auth/roles.decorator';
import { UserRole } from '../user/user-role.enum';
import { ModelConfigResponseDto } from './dto/model-config-response.dto';
import { plainToClass } from 'class-transformer';
import { ApiTags, ApiBearerAuth, ApiOperation, ApiResponse, ApiForbiddenResponse } from '@nestjs/swagger';

@ApiTags('Models')
@UseGuards(CombinedAuthGuard)
@Controller('models') // Global prefix /api/models
export class ModelConfigController {
  constructor(private readonly modelConfigService: ModelConfigService) { }

  @Roles(UserRole.TENANT_ADMIN, UserRole.SUPER_ADMIN)
  @Post()
  @HttpCode(HttpStatus.CREATED)
  @ApiBearerAuth('JWT')
  @ApiOperation({ summary: '创建模型配置', description: '创建新的AI模型配置（LLM/Embedding/Rerank）' })
  @ApiResponse({ status: 201, description: '创建成功' })
  @ApiForbiddenResponse({ description: '权限不足' })
  async create(
    @Req() req,
    @Body() createModelConfigDto: CreateModelConfigDto,
  ): Promise<ModelConfigResponseDto> {
    const modelConfig = await this.modelConfigService.create(
      createModelConfigDto,
    );
    return plainToClass(ModelConfigResponseDto, modelConfig);
  }

  @Get()
  @ApiBearerAuth('JWT')
  @ApiOperation({ summary: '获取模型配置列表' })
  @ApiResponse({ status: 200, description: '返回模型列表' })
  async findAll(): Promise<ModelConfigResponseDto[]> {
    const modelConfigs = await this.modelConfigService.findAll();
    return modelConfigs.map((mc) => plainToClass(ModelConfigResponseDto, mc));
  }

  @Get(':id')
  @ApiBearerAuth('JWT')
  @ApiOperation({ summary: '获取单个模型配置' })
  @ApiResponse({ status: 200, description: '返回模型详情' })
  async findOne(
    @Param('id') id: string,
  ): Promise<ModelConfigResponseDto> {
    const modelConfig = await this.modelConfigService.findOne(id);
    return plainToClass(ModelConfigResponseDto, modelConfig);
  }

  @Roles(UserRole.TENANT_ADMIN, UserRole.SUPER_ADMIN)
  @Put(':id')
  @ApiBearerAuth('JWT')
  @ApiOperation({ summary: '更新模型配置' })
  @ApiResponse({ status: 200, description: '更新成功' })
  @ApiForbiddenResponse({ description: '权限不足' })
  async update(
    @Req() req,
    @Param('id') id: string,
    @Body() updateModelConfigDto: UpdateModelConfigDto,
  ): Promise<ModelConfigResponseDto> {
    const modelConfig = await this.modelConfigService.update(
      id,
      updateModelConfigDto,
    );
    return plainToClass(ModelConfigResponseDto, modelConfig);
  }

  @Roles(UserRole.TENANT_ADMIN, UserRole.SUPER_ADMIN)
  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiBearerAuth('JWT')
  @ApiOperation({ summary: '删除模型配置' })
  @ApiResponse({ status: 204, description: '删除成功' })
  @ApiForbiddenResponse({ description: '权限不足' })
  async remove(@Param('id') id: string): Promise<void> {
    await this.modelConfigService.remove(id);
  }

  @Roles(UserRole.TENANT_ADMIN, UserRole.SUPER_ADMIN)
  @Patch(':id/set-default')
  @ApiBearerAuth('JWT')
  @ApiOperation({ summary: '设置默认模型', description: '将指定模型设置为该类型的默认模型' })
  @ApiResponse({ status: 200, description: '设置成功' })
  @ApiForbiddenResponse({ description: '权限不足' })
  async setDefault(
    @Param('id') id: string,
  ): Promise<ModelConfigResponseDto> {
    const modelConfig = await this.modelConfigService.setDefault(id);
    return plainToClass(ModelConfigResponseDto, modelConfig);
  }
}
