//
//  WiredTransferPhotoGroupingTests.swift
//  suixinkanTests
//

import XCTest
@testable import suixinkan

/// 有线传图照片分组逻辑测试。
final class WiredTransferPhotoGroupingTests: XCTestCase {
    /// 测试 30 分钟时间段分组与侧栏构建。
    func testBuildPhotoSectionsAndSidebarGroups() {
        let photos = [
            makePhoto(id: "1", capturedAt: "2026-05-20 12:05:18", status: .uploaded),
            makePhoto(id: "2", capturedAt: "2026-05-20 12:18:06", status: .pending),
            makePhoto(id: "3", capturedAt: "2026-05-20 11:55:51", status: .failed),
        ]

        let sections = photos.buildPhotoSections()
        XCTAssertEqual(sections.count, 2)
        XCTAssertEqual(sections[0].photos.count, 2)
        XCTAssertEqual(sections[0].photos[0].id, "2")
        XCTAssertTrue(sections[0].headerTitle.contains("12:00 - 12:30"))

        let sidebar = photos.buildSidebarGroups()
        XCTAssertEqual(sidebar.count, 1)
        XCTAssertEqual(sidebar[0].slots.count, 2)
        XCTAssertEqual(sidebar[0].slots[0].photoCount, 2)
    }

    /// 测试今日拍摄筛选。
    func testIsCapturedTodayUsesLocalCalendar() {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        formatter.locale = Locale(identifier: "zh_CN")
        let today = formatter.string(from: Date())

        let photo = makePhoto(id: "today", capturedAt: today, status: .pending)
        XCTAssertTrue(photo.isCapturedToday())
        XCTAssertFalse(makePhoto(id: "old", capturedAt: "2020-01-01 10:00:00", status: .pending).isCapturedToday())
    }

    /// 测试按拍摄时间倒序排列，最新照片在最前。
    func testSortedByCaptureTimeDescending() {
        let photos = [
            makePhoto(id: "1", capturedAt: "2026-05-20 12:05:18", status: .uploaded),
            makePhoto(id: "2", capturedAt: "2026-05-20 12:18:06", status: .pending),
            makePhoto(id: "3", capturedAt: "2026-05-20 11:55:51", status: .failed),
        ]

        XCTAssertEqual(photos.sortedByCaptureTimeDescending().map(\.id), ["2", "1", "3"])
    }

    /// 测试 Tab 计数与 pending 可选集合。
    func testTransferTabCountsAndSelectablePendingIDs() {
        let photos = [
            makePhoto(id: "1", capturedAt: "2026-05-20 12:05:18", status: .uploaded),
            makePhoto(id: "2", capturedAt: "2026-05-20 12:18:06", status: .pending),
            makePhoto(id: "3", capturedAt: "2026-05-20 12:35:44", status: .failed),
        ]
        XCTAssertEqual(photos.transferTabCounts, [3, 1, 1])
        XCTAssertEqual(photos.selectablePendingPhotoIDs, ["2"])
    }

    private func makePhoto(
        id: String,
        capturedAt: String,
        status: WiredTransferUploadStatus
    ) -> WiredTransferPhotoItem {
        WiredTransferPhotoItem(
            id: id,
            fileName: "\(id).JPG",
            thumbnailURL: "",
            capturedAt: capturedAt,
            fileSizeText: "1.00 MB",
            resolutionText: "4032x3024",
            status: status
        )
    }
}
