package otel

import (
	"context"
	"strings"

	"github.com/adrianliechti/wingman/pkg/provider"

	"go.opentelemetry.io/otel"
)

type Transcriber interface {
	Observable
	provider.Transcriber
}

type observableTranscriber struct {
	name    string
	library string

	model    string
	provider string

	transcriber provider.Transcriber
}

func NewTranscriber(provider, model string, p provider.Transcriber) Transcriber {
	library := strings.ToLower(provider)

	return &observableTranscriber{
		transcriber: p,

		name:    strings.TrimSuffix(strings.ToLower(provider), "-transcriber") + "-transcriber",
		library: library,

		model:    model,
		provider: provider,
	}
}

func (p *observableTranscriber) otelSetup() {
}

func (p *observableTranscriber) Transcribe(ctx context.Context, input provider.File, options *provider.TranscribeOptions) (*provider.Transcription, error) {
	ctx, span := otel.Tracer(p.library).Start(ctx, p.name)
	defer span.End()

	result, err := p.transcriber.Transcribe(ctx, input, options)

	meterRequest(ctx, p.library, p.provider, "transcribe", p.model)

	return result, err
}
