//
//  PaymentCollectionRecordViewModelTests.swift
//  suixinkanTests
//

import XCTest
@testable import suixinkan

/// 收款记录 ViewModel 测试。
@MainActor
final class PaymentCollectionRecordViewModelTests: XCTestCase {

    func testPaymentRecordGrouperGroupsByCreateDate() {
        let response = RepaymentCollectionRecordResponse(
            analyse: [
                RepaymentCollectionRecordAnalyseItem(date: "2024-01-15", orderCount: 2, orderAmountSum: "150.00"),
                RepaymentCollectionRecordAnalyseItem(date: "2024-01-14", orderCount: 1, orderAmountSum: "80.00"),
            ],
            list: [
                RepaymentCollectionRecordItem(
                    orderNumber: "NO001",
                    userPhone: "13800138000",
                    orderAmount: "100.00",
                    createDate: "2024-01-15",
                    createTime: "10:00:00"
                ),
                RepaymentCollectionRecordItem(
                    orderNumber: "NO002",
                    userPhone: "13800138001",
                    orderAmount: "50.00",
                    createDate: "2024-01-15",
                    createTime: "11:00:00"
                ),
                RepaymentCollectionRecordItem(
                    orderNumber: "NO003",
                    userPhone: "13800138002",
                    orderAmount: "80.00",
                    createDate: "2024-01-14",
                    createTime: "09:00:00"
                ),
            ]
        )

        let groups = PaymentRecordGrouper.group(response: response)

        XCTAssertEqual(groups.count, 2)
        XCTAssertEqual(groups[0].analyse.date, "2024-01-15")
        XCTAssertEqual(groups[0].items.count, 2)
        XCTAssertEqual(groups[1].analyse.date, "2024-01-14")
        XCTAssertEqual(groups[1].items.count, 1)
    }

    func testLoadRecordUsesAPIAndGroupsResult() async {
        let defaults = UserDefaults(suiteName: "PaymentCollectionRecordViewModelTests")!
        defaults.removePersistentDomain(forName: "PaymentCollectionRecordViewModelTests")
        let appStore = AppStore(defaults: defaults)
        appStore.session.configureTestAccount(scenicId: 10)

        let recordJSON = """
        {"code":100000,"msg":"success","data":{"analyse":[{"date":"2024-01-15","order_count":1,"order_amount_sum":"50.00"}],"list":[{"order_number":"NO001","user_phone":"13800138000","order_amount":"50.00","create_date":"2024-01-15","create_time":"10:00:00"}]}}
        """.data(using: .utf8)!
        let session = MockURLSession(responses: [recordJSON])
        let api = PaymentAPI(client: APIClient(environment: .testing, session: session))
        let viewModel = PaymentCollectionRecordViewModel(appStore: appStore)

        await viewModel.loadRecord(api: api)

        XCTAssertEqual(viewModel.groups.count, 1)
        XCTAssertEqual(viewModel.groups[0].items.first?.orderNumber, "NO001")
        XCTAssertTrue(session.requests.first?.url?.absoluteString.contains("photo-scan-order-list") == true)
    }
}
