import Foundation

/// A service that transcribes recorded audio into text for the Recording feature.
///
/// ``RecordingView`` attaches the service to its model at initialization, so the transcription can be swapped without touching the feature's
/// state machine — for example, with a real transcription backend in the app, or with a fast mock in unit tests.
public protocol TranscribingService: Sendable {

    /// Transcribes the given recorded audio into text.
    ///
    /// - Parameter audio: The recorded audio to transcribe.
    /// - Returns: The transcription of the recorded audio.
    func transcribe(_ audio: Data) async throws -> String

}

// MARK: - Services

/// The transcribing service used by default, which simulates the transcription work.
public struct SimulatedTranscribingService: TranscribingService {

    // MARK: Initializers

    /// Creates a simulated transcribing service.
    public init() {}

    // MARK: Methods

    /// Simulates the transcription of the given recorded audio with a two-second delay.
    ///
    /// - Parameter audio: The recorded audio to transcribe.
    /// - Returns: A dummy transcription.
    public func transcribe(_ audio: Data) async throws -> String {
        try await Task.sleep(for: Constant.Delay.transcribing)

        return Constant.Text.transcription
    }

}

// MARK: - Constants

/// The constant values used across the transcribing services.
private enum Constant {
    /// The delay constants.
    enum Delay {
        /// The duration of the simulated transcription work.
        static let transcribing: Duration = .seconds(2)
    }

    /// The text constants.
    enum Text {
        /// The dummy transcription of a recorded audio.
        static let transcription = "This is a dummy transcription of the recorded audio."
    }
}
