import APIClient
import ComposableArchitecture
import ComponentLibrary
import Localization
import SwiftUI

struct AudioLengthCaptionView: View {
    @Shared(.inMemory(.billingInfo)) private var billingInfo: SubscriptionInfoResponse?

    private var attributedAudioLengthUpsell: AttributedString {
        let baseString = L10n.FeatureCreateClip.audioLengthUpsellCaption
        var text = (try? AttributedString(markdown: baseString)) ?? AttributedString(baseString)
        let runsWithLinks = text.runs.filter { run in
            run.link != nil
        }

        for run in runsWithLinks {
            text[run.range].foregroundColor = ChatConstants.Colors.Foreground.inactive
            text[run.range].underlineStyle = .init(pattern: .solid, color: ChatConstants.Colors.Foreground.inactive)
        }
        return text
    }

    var body: some View {
        Group {
            if billingInfo?.plan == nil {
                Text(attributedAudioLengthUpsell)
                    .typographyV1(.caption6.inputSans())
                    .foregroundStyle(ChatConstants.Colors.Foreground.inactive)
                    .environment(\.openURL, OpenURLAction { _ in
                        // TODO: Handle upgrade tap
                        return .handled
                    })
            } else {
                Text(L10n.FeatureCreateClip.audioLengthProUserCaption)
                    .typographyV1(.caption6.inputSans())
                    .foregroundStyle(ChatConstants.Colors.Foreground.inactive)
            }
        }
    }
}
