// 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';

@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)
  async create(
    @Req() req,
    @Body() createModelConfigDto: CreateModelConfigDto,
  ): Promise<ModelConfigResponseDto> {
    const modelConfig = await this.modelConfigService.create(
      req.user.id,
      req.user.tenantId,
      createModelConfigDto,
    );
    return plainToClass(ModelConfigResponseDto, modelConfig);
  }

  @Get()
  async findAll(@Req() req): Promise<ModelConfigResponseDto[]> {
    const modelConfigs = await this.modelConfigService.findAll(req.user.id, req.user.tenantId);
    return modelConfigs.map((mc) => plainToClass(ModelConfigResponseDto, mc));
  }

  @Get(':id')
  async findOne(
    @Req() req,
    @Param('id') id: string,
  ): Promise<ModelConfigResponseDto> {
    const modelConfig = await this.modelConfigService.findOne(id, req.user.id, req.user.tenantId);
    return plainToClass(ModelConfigResponseDto, modelConfig);
  }

  @Roles(UserRole.TENANT_ADMIN, UserRole.SUPER_ADMIN)
  @Put(':id')
  async update(
    @Req() req,
    @Param('id') id: string,
    @Body() updateModelConfigDto: UpdateModelConfigDto,
  ): Promise<ModelConfigResponseDto> {
    const modelConfig = await this.modelConfigService.update(
      req.user.id,
      req.user.tenantId,
      id,
      updateModelConfigDto,
    );
    return plainToClass(ModelConfigResponseDto, modelConfig);
  }

  @Roles(UserRole.TENANT_ADMIN, UserRole.SUPER_ADMIN)
  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  async remove(@Req() req, @Param('id') id: string): Promise<void> {
    await this.modelConfigService.remove(req.user.id, req.user.tenantId, id);
  }

  @Roles(UserRole.TENANT_ADMIN, UserRole.SUPER_ADMIN)
  @Patch(':id/set-default')
  async setDefault(
    @Req() req,
    @Param('id') id: string,
  ): Promise<ModelConfigResponseDto> {
    const userId = req.user.id;
    const tenantId = req.user.tenantId;
    const modelConfig = await this.modelConfigService.setDefault(userId, tenantId, id);
    return plainToClass(ModelConfigResponseDto, modelConfig);
  }
}
