//
//  DebugHomeMenuPreviewView.swift
//  suixinkan
//
//  Created by Codex on 2026/6/24.
//

#if DEBUG
import SwiftUI

/// 首页菜单调试预览页，展示所有已知菜单入口并跳过权限过滤。
struct DebugHomeMenuPreviewView: View {
    @EnvironmentObject private var appRouter: AppRouter
    @EnvironmentObject private var router: RouterPath
    @EnvironmentObject private var toastCenter: ToastCenter

    private let items = HomeMenuRouter.debugAllMenuItems()

    var body: some View {
        List {
            Section {
                ForEach(items) { item in
                    Button {
                        open(item)
                    } label: {
                        DebugHomeMenuRow(item: item, route: HomeMenuRouter.resolve(uri: item.uri, title: item.title))
                    }
                    .buttonStyle(.plain)
                }
            } header: {
                Text("全部首页菜单")
            } footer: {
                Text("DEBUG 调试入口不读取角色权限，仅用于预览页面迁移状态。")
            }
        }
        .listStyle(.insetGrouped)
        .navigationTitle("首页菜单调试")
        .navigationBarTitleDisplayMode(.inline)
    }

    /// 打开指定调试菜单。
    private func open(_ item: HomeMenuItem) {
        let route = HomeMenuRouter.resolve(uri: item.uri, title: item.title)
        switch route {
        case .tab(let tab):
            appRouter.select(tab)
        case .orders(let entry):
            appRouter.selectOrders(entry: entry)
        case .orderPush(let route):
            appRouter.select(.orders)
            appRouter.router(for: .orders).navigate(to: .orders(route))
        case .destination(let homeRoute):
            router.navigate(to: .home(homeRoute))
        case .unsupported(_, let title, let reason):
            toastCenter.show(reason)
            router.navigate(to: .home(.modulePlaceholder(uri: item.uri, title: title)))
        case .placeholder(let uri, let title):
            HomeRouteDiagnostics.recordUnknown(uri: uri, title: title)
            router.navigate(to: .home(.modulePlaceholder(uri: uri, title: title)))
        }
    }
}

/// 首页菜单调试行，展示菜单标题、URI 和解析状态。
private struct DebugHomeMenuRow: View {
    let item: HomeMenuItem
    let route: HomeMenuResolvedRoute

    var body: some View {
        HStack(spacing: AppMetrics.Spacing.small) {
            Image(systemName: iconName)
                .font(.system(size: 18, weight: .semibold))
                .foregroundStyle(iconColor)
                .frame(width: 30, height: 30)
                .background(iconColor.opacity(0.12), in: Circle())

            VStack(alignment: .leading, spacing: 4) {
                Text(item.title)
                    .font(.system(size: AppMetrics.FontSize.body, weight: .semibold))
                    .foregroundStyle(AppDesign.textPrimary)
                Text(item.uri)
                    .font(.system(size: AppMetrics.FontSize.caption))
                    .foregroundStyle(AppDesign.textSecondary)
                    .lineLimit(1)
            }

            Spacer()

            Text(routeLabel)
                .font(.system(size: AppMetrics.FontSize.caption, weight: .semibold))
                .foregroundStyle(iconColor)

            Image(systemName: "chevron.right")
                .font(.system(size: AppMetrics.FontSize.caption, weight: .semibold))
                .foregroundStyle(AppDesign.placeholder)
        }
        .contentShape(Rectangle())
        .padding(.vertical, AppMetrics.Spacing.xSmall)
    }

    /// 返回当前路由类型展示文案。
    private var routeLabel: String {
        switch route {
        case .tab:
            return "Tab"
        case .orders:
            return "订单"
        case .orderPush:
            return "订单页"
        case .destination(let homeRoute):
            if case .modulePlaceholder = homeRoute {
                return "占位"
            }
            return "页面"
        case .unsupported:
            return "不支持"
        case .placeholder:
            return "未知"
        }
    }

    /// 返回当前路由类型图标。
    private var iconName: String {
        switch route {
        case .tab:
            return "rectangle.grid.1x2"
        case .orders, .orderPush:
            return "doc.text"
        case .destination(let homeRoute):
            if case .modulePlaceholder = homeRoute {
                return "square.dashed"
            }
            return "arrowshape.turn.up.right"
        case .unsupported:
            return "nosign"
        case .placeholder:
            return "questionmark.circle"
        }
    }

    /// 返回当前路由类型颜色。
    private var iconColor: Color {
        switch route {
        case .tab, .orders, .orderPush, .destination:
            AppDesign.primary
        case .unsupported:
            .red
        case .placeholder:
            AppDesign.warning
        }
    }
}
#endif
