import SwiftUI
import AVKit

struct ChatUrlVideoView: View {
    @State private var player: AVPlayer?
    @State private var isPlaying = false
    let imageUrl: String
    var onLoad: (() -> Void)? = nil

    var body: some View {
        ZStack {
            if let url = URL(string: imageUrl) {
                VideoPlayer(player: player)
                    .frame(height: 250)
                    .cornerRadius(16)
                    .onAppear {
                        player = AVPlayer(url: url)
                        onLoad?()
                    }
                    .onTapGesture {
                        if isPlaying {
                            player?.pause()
                        } else {
                            player?.play()
                        }
                        isPlaying.toggle()
                    }
            } else {
                Color.gray.opacity(0.2)
                    .frame(height: 250)
                    .overlay(
                        VStack {
                            Image(systemName: "video.slash")
                            Text("Video URL is invalid")
                                .font(.caption)
                        }
                    )
            }
        }
    }
}
