//
//  TaskOrderSelectViewModelTests.swift
//  suixinkanTests
//

import XCTest
@testable import suixinkan

@MainActor
/// 任务订单选择 ViewModel 测试。
final class TaskOrderSelectViewModelTests: XCTestCase {

    private var appStore: AppStore!
    private var defaults: UserDefaults!

    override func setUp() {
        defaults = UserDefaults(suiteName: "TaskOrderSelectViewModelTests")!
        defaults.removePersistentDomain(forName: "TaskOrderSelectViewModelTests")
        appStore = AppStore(defaults: defaults)
        appStore.session.configureTestAccount(scenicId: 10)
    }

    func testToggleSelectionSelectsAndDeselects() {
        let viewModel = TaskOrderSelectViewModel(appStore: appStore)
        viewModel.toggleSelection(orderNumber: "NO001")
        XCTAssertEqual(viewModel.selectedOrderNumber, "NO001")
        viewModel.toggleSelection(orderNumber: "NO001")
        XCTAssertNil(viewModel.selectedOrderNumber)
    }

    func testLoadOrdersPopulatesList() async throws {
        let json = """
        {
          "code": 100000,
          "msg": "success",
          "data": [
            {
              "project_name": "项目A",
              "order_number": "NO001",
              "order_status": 1,
              "order_status_label": "已付款",
              "pay_time": "2024-01-01",
              "user_id": 1,
              "user_phone": "13800138000"
            }
          ]
        }
        """.data(using: .utf8)!
        let session = MockURLSession(responses: [json])
        let api = TaskAPI(client: APIClient(environment: .testing, session: session))
        let viewModel = TaskOrderSelectViewModel(appStore: appStore)

        await viewModel.loadOrders(api: api)

        XCTAssertEqual(viewModel.orders.count, 1)
        XCTAssertEqual(viewModel.orders[0].orderNumber, "NO001")
    }
}
