import SwiftUI

// MARK: - URL Input Component
struct URLInputView: View {
    @Binding var url: String
    @FocusState var isUrlFieldFocused: Bool
    
    var body: some View {
        VStack(spacing: 16) {
            // URL input field
            VStack(alignment: .leading, spacing: 8) {
                HStack {
                    Text("website_url".localized)
                        .font(.subheadline)
                        .foregroundColor(.primary)
                    Text("*")
                        .foregroundColor(.red)
                    Spacer()
                }
                
                TextField("url_placeholder".localized, text: $url)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .keyboardType(.URL)
                    .autocapitalization(.none)
                    .focused($isUrlFieldFocused)
                    .onChange(of: url) { newValue in
                        // Automatically add https:// prefix
                        if !newValue.isEmpty && !newValue.hasPrefix("http://") && !newValue.hasPrefix("https://") {
                            url = "https://" + newValue
                        }
                    }
            }
            
            // Format instructions
            VStack(alignment: .leading, spacing: 8) {
                HStack {
                    Image(systemName: "info.circle")
                        .font(.caption)
                        .foregroundColor(.blue)
                    
                    Text("format_instructions".localized)
                        .font(.caption)
                        .foregroundColor(.primary)
                    
                    Spacer()
                }
                
                Text("url_format_hint".localized)
                    .font(.caption)
                    .foregroundColor(.secondary)
                    .lineLimit(nil)
            }
            .padding(.horizontal, 12)
            .padding(.vertical, 8)
            .background(
                RoundedRectangle(cornerRadius: 8)
                    .fill(Color.blue.opacity(0.1))
            )
            
            // Preview URL
            if !url.isEmpty {
                VStack(alignment: .leading, spacing: 8) {
                    HStack {
                        Image(systemName: "link")
                            .font(.caption)
                            .foregroundColor(.green)
                        
                        Text("preview_url".localized)
                            .font(.caption)
                            .foregroundColor(.primary)
                        
                        Spacer()
                    }
                    
                    Text(url)
                        .font(.caption)
                        .foregroundColor(.green)
                        .padding(.horizontal, 12)
                        .padding(.vertical, 8)
                        .background(
                            RoundedRectangle(cornerRadius: 6)
                                .fill(Color.green.opacity(0.1))
                        )
                }
            }
        }
        .toolbar {
            ToolbarItemGroup(placement: .keyboard) {
                Spacer()
                Button("done".localized) {
                    isUrlFieldFocused = false
                }
                .foregroundColor(.blue)
                .font(.system(size: 16, weight: .medium))
            }
        }
    }
}

#Preview {
    URLInputView(url: .constant(""))
} 