import SwiftUI

struct TranscriptionDetailView: View {
    let transcription: Transcription
    var onInfoTap: (() -> Void)?

    private var hasAudioFile: Bool {
        if let urlString = transcription.audioFileURL,
           let url = URL(string: urlString),
           FileManager.default.fileExists(atPath: url.path) {
            return true
        }
        return false
    }

    var body: some View {
        VStack(spacing: ProatSpacing.s) {
            ScrollView {
                VStack(spacing: ProatSpacing.m) {
                    MessageBubble(
                        label: "Origineel",
                        text: transcription.text,
                        isEnhanced: false
                    )

                    if let enhancedText = transcription.enhancedText {
                        MessageBubble(
                            label: "Verfijnd",
                            text: enhancedText,
                            isEnhanced: true
                        )
                    }
                }
                .padding(ProatSpacing.m)
            }

            if hasAudioFile, let urlString = transcription.audioFileURL,
               let url = URL(string: urlString) {
                VStack(spacing: 0) {
                    Rectangle().fill(Color.proatBorder).frame(height: 1)

                    AudioPlayerView(url: url, transcription: transcription, onInfoTap: onInfoTap)
                        .padding(.horizontal, ProatSpacing.xs)
                        .padding(.vertical, 6)
                        .background(
                            RoundedRectangle(cornerRadius: ProatRadius.knop, style: .continuous)
                                .fill(Color.proatBgRaised)
                                .overlay(
                                    RoundedRectangle(cornerRadius: ProatRadius.knop, style: .continuous)
                                        .stroke(Color.proatBorder, lineWidth: 1)
                                )
                        )
                        .padding(.horizontal, ProatSpacing.s)
                        .padding(.top, 6)
                }
            }
        }
        .padding(.vertical, ProatSpacing.s)
        .background(Color.proatBgBase)
    }
}

private struct MessageBubble: View {
    let label: String
    let text: String
    let isEnhanced: Bool

    var body: some View {
        HStack(alignment: .bottom) {
            if isEnhanced { Spacer(minLength: 60) }

            VStack(alignment: isEnhanced ? .leading : .trailing, spacing: 4) {
                Text(label)
                    .font(ProatFont.klein)
                    .fontWeight(.medium)
                    .foregroundColor(isEnhanced ? .proatTeal : .proatText3)
                    .padding(.horizontal, ProatSpacing.s)

                ScrollView {
                    Text(text)
                        .font(ProatFont.body)
                        .foregroundColor(.proatText1)
                        .lineSpacing(2)
                        .textSelection(.enabled)
                        .padding(.horizontal, ProatSpacing.s)
                        .padding(.vertical, ProatSpacing.xs)
                }
                .frame(maxHeight: 350)
                .background {
                    if isEnhanced {
                        RoundedRectangle(cornerRadius: ProatRadius.sheet, style: .continuous)
                            .fill(Color.proatTealTint)
                            .overlay(
                                RoundedRectangle(cornerRadius: ProatRadius.sheet, style: .continuous)
                                    .strokeBorder(Color.proatTeal.opacity(0.45), lineWidth: 1)
                            )
                    } else {
                        RoundedRectangle(cornerRadius: ProatRadius.sheet, style: .continuous)
                            .fill(Color.proatBgRaised)
                            .overlay(
                                RoundedRectangle(cornerRadius: ProatRadius.sheet, style: .continuous)
                                    .strokeBorder(Color.proatBorder, lineWidth: 1)
                            )
                    }
                }
                .overlay(alignment: .bottomTrailing) {
                    CopyIconButton(textToCopy: text)
                        .padding(ProatSpacing.xs)
                }
            }

            if !isEnhanced { Spacer(minLength: 60) }
        }
    }


}
