/**
 * Service for managing per-tenant voice configuration (STT provider selection).
 */
import { Injectable } from '@nestjs/common';
import { VoiceConfigRepository } from '../repositories/voice-config.repository';
import { VoiceConfig } from '../../domain/entities/voice-config.entity';

export interface UpdateVoiceConfigDto {
  stt_provider?: string;
}

@Injectable()
export class VoiceConfigService {
  constructor(private readonly repo: VoiceConfigRepository) {}

  async findByTenantId(tenantId: string): Promise<VoiceConfig | null> {
    return this.repo.findByTenantId(tenantId);
  }

  async upsert(tenantId: string, dto: UpdateVoiceConfigDto): Promise<VoiceConfig> {
    let config = await this.repo.findByTenantId(tenantId);
    if (!config) {
      config = new VoiceConfig();
      config.tenantId = tenantId;
    }
    if (dto.stt_provider !== undefined) {
      config.sttProvider = dto.stt_provider;
    }
    return this.repo.save(config);
  }
}
