//
//  TaskAddViewModelTests.swift
//  suixinkanTests
//

import XCTest
@testable import suixinkan

/// 添加任务 ViewModel 测试。
@MainActor
final class TaskAddViewModelTests: XCTestCase {

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

    override func setUp() {
        defaults = UserDefaults(suiteName: "TaskAddViewModelTests")!
        defaults.removePersistentDomain(forName: "TaskAddViewModelTests")
        appStore = AppStore(defaults: defaults)
        appStore.currentScenicId = 10
    }

    func testValidateRequiresTaskName() async {
        let viewModel = TaskAddViewModel(appStore: appStore)
        var message: String?
        viewModel.onShowMessage = { message = $0 }

        await viewModel.validateAndSubmit(api: TaskAPI(client: makeClient()))

        XCTAssertEqual(message, "请填写任务名称！")
    }

    func testBuildAddTaskRequestSplitsCloudAndUploadFiles() {
        let viewModel = TaskAddViewModel(appStore: appStore)
        viewModel.updateTaskName("任务A")
        viewModel.updateTaskDetails("描述")
        viewModel.setSelectedOrderNumber("NO001")
        viewModel.setUrgentHour(2)
        viewModel.addCloudFiles([CloudFile(id: 1, name: "云文件", type: 2)])
        viewModel.addUploadPlaceholder(fileName: "a.jpg", fileType: 2, uploadTaskId: "task1")
        viewModel.markUploadSucceeded(uploadTaskId: "task1", fileURL: "https://cdn/a.jpg", autoRemarkWhenSingle: false)

        let request = viewModel.buildAddTaskRequest()

        XCTAssertEqual(request.name, "任务A")
        XCTAssertEqual(request.orderNumber, "NO001")
        XCTAssertEqual(request.urgentHour, 2)
        XCTAssertEqual(request.cloudFile.count, 1)
        XCTAssertEqual(request.uploadFile.count, 1)
        XCTAssertEqual(request.uploadFile[0].fileUrl, "https://cdn/a.jpg")
    }

    func testAddCloudFilesDedupesAndAutoRemarkSingleFile() {
        let viewModel = TaskAddViewModel(appStore: appStore)
        viewModel.addCloudFiles([CloudFile(id: 1, name: "A", type: 2)])
        XCTAssertEqual(viewModel.selectedFiles.count, 1)
        XCTAssertEqual(viewModel.consumePendingRemarkFileID(), viewModel.selectedFiles[0].id)

        viewModel.addCloudFiles([
            CloudFile(id: 1, name: "A", type: 2),
            CloudFile(id: 2, name: "B", type: 2),
            CloudFile(id: 3, name: "C", type: 2),
        ])
        XCTAssertEqual(viewModel.selectedFiles.count, 3)
        XCTAssertNil(viewModel.consumePendingRemarkFileID())
    }

    func testResetAllStateClearsForm() {
        let viewModel = TaskAddViewModel(appStore: appStore)
        viewModel.updateTaskName("任务")
        viewModel.setSelectedOrderNumber("NO001")
        viewModel.addCloudFiles([CloudFile(id: 1, type: 2)])

        viewModel.resetAllState()

        XCTAssertEqual(viewModel.taskName, "")
        XCTAssertNil(viewModel.selectedOrderNumber)
        XCTAssertTrue(viewModel.selectedFiles.isEmpty)
    }

    private func makeClient() -> APIClient {
        APIClient(environment: .testing, session: MockURLSession(responses: []))
    }
}
