import SwiftUI

struct ProfileView: View {
    var appState: AppState
    var isOwnProfile: Bool
    var userId: String?
    @State private var viewModel = ProfileViewModel()
    @Environment(\.dismiss) private var dismiss

    var body: some View {
        NavigationStack {
            Form {
                // Avatar
                Section {
                    HStack {
                        Spacer()
                        VStack(spacing: 8) {
                            if let avatarData = viewModel.avatarData,
                               let uiImage = UIImage(data: avatarData) {
                                Image(uiImage: uiImage)
                                    .resizable()
                                    .aspectRatio(contentMode: .fill)
                                    .frame(width: 80, height: 80)
                                    .clipShape(Circle())
                            } else {
                                CircularAvatarView(
                                    name: viewModel.profile?.username ?? "?",
                                    size: 80,
                                    isGroup: false
                                )
                            }

                            if isOwnProfile {
                                Button("Change Photo") {
                                    // Photo picker would go here
                                }
                                .font(.caption)
                            }
                        }
                        Spacer()
                    }
                    .listRowBackground(Color.clear)
                }

                // Info
                Section("Info") {
                    if let username = viewModel.profile?.username {
                        LabeledContent("Username", value: username)
                    }
                    if let email = viewModel.profile?.email {
                        LabeledContent("Email", value: email)
                    }
                }

                if isOwnProfile {
                    // Editable fields
                    Section("Contact") {
                        TextField("Phone", text: $viewModel.phone)
                            .keyboardType(.phonePad)
                        Toggle("Phone visible to contacts", isOn: $viewModel.phoneVisible)

                        TextField("Location", text: $viewModel.location)
                        Toggle("Location visible to contacts", isOn: $viewModel.locationVisible)
                    }
                } else {
                    // Read-only view
                    if let phone = viewModel.profile?.phone, viewModel.profile?.phoneVisible == true {
                        Section("Contact") {
                            LabeledContent("Phone", value: phone)
                        }
                    }
                    if let location = viewModel.profile?.location, viewModel.profile?.locationVisible == true {
                        Section("Location") {
                            LabeledContent("Location", value: location)
                        }
                    }
                }

                if let error = viewModel.errorMessage {
                    Section {
                        Text(error)
                            .foregroundStyle(.red)
                    }
                }
            }
            .navigationTitle(isOwnProfile ? "My Profile" : "Profile")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .topBarTrailing) {
                    if isOwnProfile {
                        Button("Save") {
                            Task {
                                await viewModel.saveProfile(chatClient: appState.chatClient)
                                dismiss()
                            }
                        }
                        .disabled(viewModel.isSaving)
                    } else {
                        Button("Done") { dismiss() }
                    }
                }
                ToolbarItem(placement: .cancellationAction) {
                    Button("Cancel") { dismiss() }
                }
            }
            .task {
                await viewModel.loadProfile(userId: userId, chatClient: appState.chatClient)
            }
        }
    }
}
