//
//  NavigationRouterTests.swift
//  suixinkanTests
//
//  Created by Codex on 2026/6/22.
//

import XCTest
@testable import suixinkan

/// 主导航路由测试，覆盖 AppRoute 的跨页面导航策略。
final class NavigationRouterTests: XCTestCase {
    /// 测试通过 NavigationStack push 的子页面默认隐藏底部 TabBar。
    func testPushedRoutesHideTabBarByDefault() {
        XCTAssertTrue(AppRoute.placeholder(title: "详情").hidesTabBarWhenPushed)
        XCTAssertTrue(AppRoute.home(.moreFunctions).hidesTabBarWhenPushed)
        XCTAssertTrue(AppRoute.home(.paymentCollection).hidesTabBarWhenPushed)
        XCTAssertTrue(AppRoute.home(.wallet).hidesTabBarWhenPushed)
        XCTAssertTrue(AppRoute.orders(.multiTravelTaskUpload(orderNumber: "MT001")).hidesTabBarWhenPushed)
        XCTAssertTrue(AppRoute.orders(.orderTrailer(orderNumber: "MT001", title: "尾片上传")).hidesTabBarWhenPushed)
    }

    /// 测试切换订单 Tab 时会保存订单内部子入口。
    func testSelectOrdersUpdatesTabAndEntry() {
        let router = AppRouter()

        router.selectOrders(entry: .verificationOrders)

        XCTAssertEqual(router.selectedTab, .orders)
        XCTAssertEqual(router.selectedOrdersEntry, .verificationOrders)
    }

    /// 测试全局扫码核销路由会切到核销管理页并保存扫码结果。
    func testRouteToOrderVerificationStoresPendingScanCode() {
        let router = AppRouter()

        router.routeToOrderVerification(scannedCode: "order_number=VERIFY001")

        XCTAssertEqual(router.selectedTab, .orders)
        XCTAssertEqual(router.router(for: .orders).path.last, .orders(.writeOffList))
        XCTAssertEqual(router.pendingOrderScanCode, "order_number=VERIFY001")
    }

    /// 测试全局扫码结果只能被消费一次。
    func testConsumePendingOrderScanCodeClearsValueAfterFirstRead() {
        let router = AppRouter()
        router.routeToOrderVerification(scannedCode: "VERIFY001")

        XCTAssertEqual(router.consumePendingOrderScanCode(), "VERIFY001")
        XCTAssertNil(router.consumePendingOrderScanCode())
    }

    /// 测试重置主路由时会清理未消费的扫码结果。
    func testResetClearsPendingOrderScanCode() {
        let router = AppRouter()
        router.routeToOrderVerification(scannedCode: "VERIFY001")

        router.reset()

        XCTAssertEqual(router.selectedTab, .home)
        XCTAssertEqual(router.selectedOrdersEntry, .storeOrders)
        XCTAssertNil(router.pendingOrderScanCode)
    }

    /// 测试个人中心系统设置路由可压入“我的”Tab 导航栈。
    func testProfileSettingsRouteCanBePushedIntoProfileTabPath() {
        let appRouter = AppRouter()
        let profileRouter = appRouter.router(for: .profile)

        profileRouter.navigate(to: .profile(.settings))

        XCTAssertEqual(profileRouter.path.last, .profile(.settings))
    }

    /// 测试订单模块真实路由可被压入导航栈。
    func testOrderRoutesCanBePushedIntoRouterPath() {
        let router = RouterPath()

        router.navigate(to: .orders(.multiTravelTaskUpload(orderNumber: "MT001")))
        router.navigate(to: .orders(.orderTrailer(orderNumber: "MT001", title: "视频预告")))

        XCTAssertEqual(router.path.count, 2)
        XCTAssertEqual(router.path[0], .orders(.multiTravelTaskUpload(orderNumber: "MT001")))
        XCTAssertEqual(router.path[1], .orders(.orderTrailer(orderNumber: "MT001", title: "视频预告")))
    }
}
