import SwiftUI
import ComposeApp

// MARK: - User List Item
struct UserListItem: View {
    let user: ResidenceUserResponse
    let isOwner: Bool
    let isPrimaryOwner: Bool
    let onRemove: () -> Void

    var body: some View {
        HStack(alignment: .top, spacing: 12) {
            VStack(alignment: .leading, spacing: 4) {
                HStack {
                    Text(user.username)
                        .font(.body)
                        .fontWeight(.medium)
                        .foregroundColor(Color.appTextPrimary)

                    if isOwner {
                        Text("Owner")
                            .font(.caption)
                            .fontWeight(.semibold)
                            .foregroundColor(Color.appPrimary)
                            .padding(.horizontal, 6)
                            .padding(.vertical, 2)
                            .background(Color.appPrimary.opacity(0.1))
                            .cornerRadius(4)
                            .accessibilityLabel("Owner")
                    }
                }

                if !user.email.isEmpty {
                    Text(user.email)
                        .font(.caption)
                        .foregroundColor(Color.appTextSecondary)
                }

                let fullName = [user.firstName, user.lastName]
                    .compactMap { $0 }
                    .filter { !$0.isEmpty }
                    .joined(separator: " ")

                if !fullName.isEmpty {
                    Text(fullName)
                        .font(.caption)
                        .foregroundColor(Color.appTextSecondary)
                }
            }

            Spacer()

            if isPrimaryOwner && !isOwner {
                Button(action: onRemove) {
                    Image(systemName: "trash")
                        .foregroundColor(Color.appError)
                }
                .accessibilityLabel("Remove \(user.username) from property")
            }
        }
        .padding()
        .background(Color.appBackgroundSecondary)
        .cornerRadius(12)
        .padding(.horizontal)
    }
}
