//
//  TaskModelsTests.swift
//  suixinkanTests
//

import XCTest
@testable import suixinkan

/// 任务模型编解码测试。
final class TaskModelsTests: XCTestCase {

    func testAddTaskRequestEncodesSnakeCaseFields() throws {
        let request = AddTaskRequest(
            scenicId: 10,
            name: "测试任务",
            orderNumber: "NO001",
            remark: "描述",
            urgentHour: 2,
            cloudFile: [TaskCloudFileItem(fileId: 1, remark: "云备注")],
            uploadFile: [TaskUploadFileItem(fileUrl: "https://cdn/a.jpg", fileName: "a.jpg", remark: "")]
        )
        let data = try JSONEncoder().encode(request)
        let json = try JSONSerialization.jsonObject(with: data) as? [String: Any]

        XCTAssertEqual(json?["scenic_id"] as? Int, 10)
        XCTAssertEqual(json?["name"] as? String, "测试任务")
        XCTAssertEqual(json?["order_number"] as? String, "NO001")
        XCTAssertEqual(json?["photog_remark"] as? String, "描述")
        XCTAssertEqual(json?["urgent_hour"] as? Int, 2)
    }

    func testAvailableOrderDecodesSnakeCaseFields() throws {
        let json = """
        {
          "project_name": "航拍项目",
          "order_number": "NO001",
          "order_status": 1,
          "order_status_label": "已付款",
          "pay_time": "2024-01-01 10:00:00",
          "user_id": 100,
          "user_phone": "13800138000"
        }
        """.data(using: .utf8)!

        let order = try JSONDecoder().decode(AvailableOrder.self, from: json)
        XCTAssertEqual(order.projectName, "航拍项目")
        XCTAssertEqual(order.orderNumber, "NO001")
        XCTAssertEqual(order.orderStatusLabel, "已付款")
    }

    func testCloudFileListResponseDecodesList() throws {
        let json = """
        {
          "total": 1,
          "list": [
            {
              "id": 5,
              "parent_folder_id": 0,
              "file_url": "https://cdn/a.jpg",
              "cover_url": "",
              "updated_at": "",
              "name": "图片1",
              "created_at": "",
              "child_num": 0,
              "type": 2,
              "file_size": 100
            }
          ]
        }
        """.data(using: .utf8)!

        let response = try JSONDecoder().decode(CloudFileListResponse.self, from: json)
        XCTAssertEqual(response.total, 1)
        XCTAssertEqual(response.list[0].id, 5)
        XCTAssertTrue(response.list[0].isImage)
    }
}
