// server/src/user-setting/user-setting.controller.ts
import {
  Body,
  Controller,
  Get,
  HttpCode,
  HttpStatus,
  Put,
  Req,
  UseGuards,
} from '@nestjs/common';
import { UserSettingService } from './user-setting.service';
import { UpdateUserSettingDto } from './dto/update-user-setting.dto';
import { CombinedAuthGuard } from '../auth/combined-auth.guard';
import { UserSettingResponseDto } from './dto/user-setting-response.dto';
import { ModelConfigService } from '../model-config/model-config.service';
import { plainToClass } from 'class-transformer';
import { RolesGuard } from '../auth/roles.guard';
import { Roles } from '../auth/roles.decorator';
import { UserRole } from '../user/user-role.enum';
import { TenantService } from '../tenant/tenant.service';

@UseGuards(CombinedAuthGuard, RolesGuard)
@Controller('settings') // Global prefix /api/settings
export class UserSettingController {
  constructor(
    private readonly userSettingService: UserSettingService,
    private readonly modelConfigService: ModelConfigService,
    private readonly tenantService: TenantService,
  ) { }

  @Get('global')
  async getGlobal(): Promise<UserSettingResponseDto> {
    const globalSetting = await this.userSettingService.getGlobalSettings();
    return plainToClass(UserSettingResponseDto, globalSetting);
  }

  @Get('tenant')
  async getTenantSettings(@Req() req) {
    if (!req.user.tenantId) {
      return this.userSettingService.getGlobalSettings();
    }
    return this.tenantService.getSettings(req.user.tenantId);
  }

  @Roles(UserRole.TENANT_ADMIN, UserRole.SUPER_ADMIN)
  @Put('global')
  @HttpCode(HttpStatus.OK)
  async updateGlobal(
    @Body() updateUserSettingDto: UpdateUserSettingDto,
  ): Promise<UserSettingResponseDto> {
    const globalSetting = await this.userSettingService.updateGlobalSettings(
      updateUserSettingDto,
    );
    return plainToClass(UserSettingResponseDto, globalSetting);
  }

  @Get()
  async findOne(@Req() req): Promise<UserSettingResponseDto> {
    const userSetting = await this.userSettingService.findOrCreate(req.user.id);
    return plainToClass(UserSettingResponseDto, userSetting);
  }

  @Put()
  @HttpCode(HttpStatus.OK)
  async update(
    @Req() req,
    @Body() updateUserSettingDto: UpdateUserSettingDto,
  ): Promise<UserSettingResponseDto> {
    const userSetting = await this.userSettingService.update(
      req.user.id,
      updateUserSettingDto,
    );
    return plainToClass(UserSettingResponseDto, userSetting);
  }

  @Get('vision-models')
  async getVisionModels(@Req() req: any) {
    const userId = req.user.id;
    const models = await this.modelConfigService.findByType(userId, req.user.tenantId, 'vision');
    return models;
  }

  @Get('vision-model')
  async getVisionModel(@Req() req: any) {
    const userId = req.user.id;
    const visionModelId =
      await this.userSettingService.getVisionModelId(userId);
    return { visionModelId };
  }

  @Put('vision-model')
  async updateVisionModel(
    @Req() req: any,
    @Body() body: { visionModelId: string },
  ) {
    const userId = req.user.id;
    await this.userSettingService.updateVisionModel(userId, body.visionModelId);
    return { success: true };
  }

  @Get('language')
  async getLanguage(@Req() req: any) {
    const userId = req.user.id;
    const language = await this.userSettingService.getLanguage(userId);
    return { language };
  }

  @Put('language')
  async updateLanguage(@Req() req: any, @Body() body: { language: string }) {
    const userId = req.user.id;
    await this.userSettingService.updateLanguage(userId, body.language);
    return { success: true };
  }
}
