/**
 * REST controller for per-tenant voice configuration (STT provider selection).
 *
 * Endpoints (JWT validated by Kong gateway):
 *   GET  /api/v1/agent/voice-config  → Get current tenant's voice config
 *   PUT  /api/v1/agent/voice-config  → Upsert voice config
 */
import { Controller, Get, Put, Body } from '@nestjs/common';
import { TenantId } from '@it0/common';
import { VoiceConfigService, UpdateVoiceConfigDto } from '../../../infrastructure/services/voice-config.service';

const DEFAULT_CONFIG = {
  stt_provider: 'openai',
};

@Controller('api/v1/agent/voice-config')
export class VoiceConfigController {
  constructor(private readonly voiceConfigService: VoiceConfigService) {}

  @Get()
  async getConfig(@TenantId() tenantId: string) {
    if (!tenantId) return DEFAULT_CONFIG;
    const config = await this.voiceConfigService.findByTenantId(tenantId);
    if (!config) return { ...DEFAULT_CONFIG, tenantId };
    return {
      tenantId: config.tenantId,
      stt_provider: config.sttProvider,
    };
  }

  @Put()
  async upsertConfig(
    @TenantId() tenantId: string,
    @Body() dto: UpdateVoiceConfigDto,
  ) {
    const config = await this.voiceConfigService.upsert(tenantId || 'default', dto);
    return {
      tenantId: config.tenantId,
      stt_provider: config.sttProvider,
    };
  }
}
