import SwiftUI
import ComposeApp

struct ResidencesListView: View {
    @StateObject private var viewModel = ResidenceViewModel()
    @StateObject private var taskViewModel = TaskViewModel()
    @State private var showingAddResidence = false
    @State private var showingJoinResidence = false
    @State private var showingUpgradePrompt = false
    @State private var showingSettings = false
    @State private var pushTargetResidenceId: Int32?
    @StateObject private var authManager = AuthenticationManager.shared
    @StateObject private var subscriptionCache = SubscriptionCacheWrapper.shared

    var body: some View {
        ZStack {
            // Warm organic background
            WarmGradientBackground()

            if let response = viewModel.myResidences {
                ListAsyncContentView(
                    items: response.residences,
                    isLoading: viewModel.isLoading,
                    errorMessage: viewModel.errorMessage,
                    content: { residences in
                        ResidencesContent(residences: residences)
                    },
                    emptyContent: {
                        OrganicEmptyResidencesView()
                    },
                    onRefresh: {
                        viewModel.loadMyResidences(forceRefresh: true)
                        for await loading in viewModel.$isLoading.values {
                            if !loading { break }
                        }
                    },
                    onRetry: {
                        viewModel.loadMyResidences()
                    }
                )
            } else if viewModel.isLoading {
                DefaultLoadingView()
            } else if let error = viewModel.errorMessage {
                DefaultErrorView(message: error, onRetry: {
                    viewModel.loadMyResidences()
                })
            }
        }
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            ToolbarItem(placement: .navigationBarLeading) {
                Button(action: {
                    showingSettings = true
                }) {
                    OrganicToolbarButton(systemName: "gearshape.fill")
                }
                .accessibilityIdentifier(AccessibilityIdentifiers.Navigation.settingsButton)
                .accessibilityLabel("Settings")
            }

            ToolbarItemGroup(placement: .navigationBarTrailing) {
                Button(action: {
                    // Check if we should show upgrade prompt before joining
                    let currentCount = viewModel.myResidences?.residences.count ?? 0
                    if subscriptionCache.shouldShowUpgradePrompt(currentCount: currentCount, limitKey: "properties") {
                        showingUpgradePrompt = true
                    } else {
                        showingJoinResidence = true
                    }
                }) {
                    OrganicToolbarButton(systemName: "person.badge.plus")
                }
                .accessibilityLabel("Join a property")

                Button(action: {
                    // Check if we should show upgrade prompt before adding
                    let currentCount = viewModel.myResidences?.residences.count ?? 0
                    if subscriptionCache.shouldShowUpgradePrompt(currentCount: currentCount, limitKey: "properties") {
                        showingUpgradePrompt = true
                    } else {
                        showingAddResidence = true
                    }
                }) {
                    OrganicToolbarButton(systemName: "plus", isPrimary: true)
                }
                .accessibilityIdentifier(AccessibilityIdentifiers.Residence.addButton)
                .accessibilityLabel("Add new property")
            }
        }
        .sheet(isPresented: $showingAddResidence) {
            AddResidenceView(
                isPresented: $showingAddResidence,
                onResidenceCreated: {
                    refreshWithTimeout()
                }
            )
        }
        .sheet(isPresented: $showingJoinResidence) {
            JoinResidenceView(onJoined: {
                refreshWithTimeout()
            })
        }
        .sheet(isPresented: $showingUpgradePrompt) {
            UpgradePromptView(triggerKey: "add_second_property", isPresented: $showingUpgradePrompt)
        }
        .sheet(isPresented: $showingSettings) {
            NavigationStack {
                ProfileTabView()
            }
        }
        .onAppear {
            AnalyticsManager.shared.trackScreen(.residences)
            if let pendingResidenceId = PushNotificationManager.shared.pendingNavigationResidenceId {
                navigateToResidenceFromPush(residenceId: pendingResidenceId)
            }
            if authManager.isAuthenticated {
                viewModel.loadMyResidences()
                // Also load tasks to populate summary stats
                taskViewModel.loadTasks()
            }
        }
        // P-5: Removed redundant .onChange(of: scenePhase) handler.
        // iOSApp.swift already handles foreground refresh globally, so per-view
        // scenePhase handlers fire duplicate network requests.
        .onChange(of: authManager.isAuthenticated) { _, isAuth in
            if isAuth {
                // User just logged in or registered - load their residences and tasks
                viewModel.loadMyResidences()
                taskViewModel.loadTasks()
            } else {
                // User logged out - clear data
                viewModel.myResidences = nil
            }
        }
        .onReceive(NotificationCenter.default.publisher(for: .navigateToResidence)) { notification in
            if let residenceId = notification.userInfo?["residenceId"] as? Int {
                navigateToResidenceFromPush(residenceId: residenceId)
            }
        }
        .navigationDestination(item: $pushTargetResidenceId) { residenceId in
            ResidenceDetailView(residenceId: residenceId)
        }
    }

    /// Refresh residences with a 10-second timeout to prevent indefinite loading
    private func refreshWithTimeout() {
        viewModel.loadMyResidences(forceRefresh: true)
        // Safety timeout: if the API hangs, clear loading state after 10 seconds
        DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
            if viewModel.isLoading {
                viewModel.isLoading = false
            }
        }
    }

    private func navigateToResidenceFromPush(residenceId: Int) {
        pushTargetResidenceId = Int32(residenceId)
        PushNotificationManager.shared.pendingNavigationResidenceId = nil
    }
}

// MARK: - Organic Toolbar Button

private struct OrganicToolbarButton: View {
    let systemName: String
    var isPrimary: Bool = false

    var body: some View {
        ZStack {
            if isPrimary {
                Circle()
                    .fill(Color.appPrimary)
                    .frame(width: 32, height: 32)

                Image(systemName: systemName)
                    .font(.system(size: 14, weight: .bold))
                    .foregroundColor(Color.appTextOnPrimary)
            } else {
                Circle()
                    .fill(Color.appPrimary.opacity(0.1))
                    .frame(width: 32, height: 32)

                Image(systemName: systemName)
                    .font(.system(size: 14, weight: .semibold))
                    .foregroundColor(Color.appPrimary)
            }
        }
    }
}

// MARK: - Residences Content View

private struct ResidencesContent: View {
    let residences: [ResidenceResponse]
    @ObservedObject private var dataManager = DataManagerObservable.shared

    /// Compute total summary from DataManagerObservable (single source of truth)
    private var computedSummary: TotalSummary {
        let metrics = dataManager.totalTaskMetrics
        return TotalSummary(
            totalResidences: Int32(residences.count),
            totalTasks: Int32(dataManager.activeTasks.count),
            totalPending: 0,
            totalOverdue: Int32(metrics.overdueCount),
            tasksDueNextWeek: Int32(metrics.upcoming7Days),
            tasksDueNextMonth: Int32(metrics.upcoming30Days)
        )
    }

    /// Get task metrics for a specific residence from DataManagerObservable
    private func taskMetrics(for residenceId: Int32) -> WidgetDataManager.TaskMetrics {
        dataManager.taskMetrics(for: residenceId)
    }

    var body: some View {
        ScrollView(showsIndicators: false) {
            VStack(spacing: OrganicSpacing.comfortable) {
                // Summary Card with enhanced styling
                SummaryCard(summary: computedSummary)
                    .padding(.horizontal, 16)
                    .padding(.top, 8)

                // Residences List with staggered animation
                LazyVStack(spacing: 16) {
                    ForEach(Array(residences.enumerated()), id: \.element.id) { index, residence in
                        NavigationLink(destination: ResidenceDetailView(residenceId: residence.id)) {
                            ResidenceCard(
                                residence: residence,
                                taskMetrics: taskMetrics(for: residence.id)
                            )
                            .padding(.horizontal, 16)
                        }
                        .buttonStyle(OrganicCardButtonStyle())
                        .transition(.asymmetric(
                            insertion: .opacity.combined(with: .move(edge: .bottom)),
                            removal: .opacity
                        ))
                    }
                }
            }
            .padding(.bottom, OrganicSpacing.airy)
        }
        .accessibilityIdentifier(AccessibilityIdentifiers.Residence.residencesList)
        .safeAreaInset(edge: .bottom) {
            Color.clear.frame(height: 0)
        }
    }
}

// MARK: - Organic Empty Residences View

private struct OrganicEmptyResidencesView: View {
    @State private var isAnimating = false
    @Environment(\.accessibilityReduceMotion) private var reduceMotion

    var body: some View {
        VStack(spacing: OrganicSpacing.comfortable) {
            Spacer()

            // Animated house illustration
            ZStack {
                // Background glow
                Circle()
                    .fill(
                        RadialGradient(
                            colors: [
                                Color.appPrimary.opacity(0.15),
                                Color.appPrimary.opacity(0.05),
                                Color.clear
                            ],
                            center: .center,
                            startRadius: 0,
                            endRadius: 80
                        )
                    )
                    .frame(width: 160, height: 160)
                    .scaleEffect(isAnimating ? 1.1 : 1.0)
                    .animation(
                        isAnimating && !reduceMotion
                            ? Animation.easeInOut(duration: 3).repeatForever(autoreverses: true)
                            : .default,
                        value: isAnimating
                    )

                // House icon
                ZStack {
                    Circle()
                        .fill(Color.appPrimary.opacity(0.1))
                        .frame(width: 100, height: 100)

                    Image("outline")
                        .renderingMode(.template)
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 48, height: 48)
                        .foregroundColor(Color.appPrimary)
                        .offset(y: isAnimating ? -2 : 2)
                        .animation(
                            isAnimating && !reduceMotion
                                ? Animation.easeInOut(duration: 2).repeatForever(autoreverses: true)
                                : .default,
                            value: isAnimating
                        )
                }
            }

            VStack(spacing: 12) {
                Text("Welcome to Your Space")
                    .font(.system(size: 24, weight: .bold, design: .rounded))
                    .foregroundColor(Color.appTextPrimary)

                Text("Tap the + icon in the top right\nto add your first property")
                    .font(.system(size: 15, weight: .medium))
                    .foregroundColor(Color.appTextSecondary)
                    .multilineTextAlignment(.center)
                    .lineSpacing(4)
            }
            .padding(.top, 8)

            Spacer()

            // Decorative footer elements
            HStack(spacing: 40) {
                FloatingLeaf(delay: 0, size: 18, color: Color.appPrimary)
                FloatingLeaf(delay: 0.5, size: 14, color: Color.appAccent)
                FloatingLeaf(delay: 1.0, size: 20, color: Color.appPrimary)
            }
            .opacity(0.6)
            .padding(.bottom, 40)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .onAppear {
            isAnimating = true
        }
        .onDisappear {
            isAnimating = false
        }
    }
}

#Preview {
    NavigationStack {
        ResidencesListView()
    }
}
