//
//  TravelAlbumOTGStorageTests.swift
//  suixinkanTests
//

import XCTest
@testable import suixinkan

/// 旅拍相册 OTG 本地存储测试。
final class TravelAlbumOTGStorageTests: XCTestCase {
    func testStoragePathContainsAccountScenicStoreAndAlbum() throws {
        let root = try makeTempDirectory()
        defer { try? FileManager.default.removeItem(at: root) }
        let store = makeStore(root: root)

        let originals = try store.originalsDirectory(albumId: 33)
        let previews = try store.previewsDirectory(albumId: 33)

        XCTAssertTrue(originals.path.contains("u1_photog/scenic_11/store_22/album_33/originals"))
        XCTAssertTrue(previews.path.contains("u1_photog/scenic_11/store_22/album_33/previews"))
    }

    func testUniqueOriginalFileURLAvoidsOverwrite() throws {
        let root = try makeTempDirectory()
        defer { try? FileManager.default.removeItem(at: root) }
        let store = makeStore(root: root)

        let first = try store.uniqueOriginalFileURL(filename: "IMG/1.JPG", albumId: 33)
        try Data([1]).write(to: first)
        let second = try store.uniqueOriginalFileURL(filename: "IMG/1.JPG", albumId: 33)

        XCTAssertEqual(first.lastPathComponent, "IMG_1.JPG")
        XCTAssertEqual(second.lastPathComponent, "IMG_1_1.JPG")
    }

    func testLoadNormalizesInterruptedTransferToPending() throws {
        let root = try makeTempDirectory()
        defer { try? FileManager.default.removeItem(at: root) }
        let store = makeStore(root: root)
        let file = try store.uniqueOriginalFileURL(filename: "A.JPG", albumId: 33)
        try Data([1, 2, 3]).write(to: file)
        let record = TravelAlbumOTGPhotoRecord(
            id: "1",
            fileName: "A.JPG",
            localPath: file.path,
            capturedAt: "2026-07-07 10:00:00",
            fileSizeBytes: 3,
            status: .uploading,
            progress: 50,
            albumId: 33,
            userId: "u1"
        )

        store.save([record], albumId: 33)
        let loaded = store.load(albumId: 33)

        XCTAssertEqual(loaded.first?.status, .pending)
        XCTAssertEqual(loaded.first?.progress, 0)
        XCTAssertEqual(loaded.first?.localPath, "originals/A.JPG")
        XCTAssertEqual(store.absoluteURL(for: loaded.first?.localPath ?? "", albumId: 33), file)
    }

    func testRemoveAndClearAlbumDeleteFiles() throws {
        let root = try makeTempDirectory()
        defer { try? FileManager.default.removeItem(at: root) }
        let store = makeStore(root: root)
        let file = try store.uniqueOriginalFileURL(filename: "A.JPG", albumId: 33)
        try Data([1]).write(to: file)
        let record = TravelAlbumOTGPhotoRecord(
            id: "1",
            fileName: "A.JPG",
            localPath: file.path,
            capturedAt: "2026-07-07 10:00:00",
            fileSizeBytes: 1,
            status: .pending,
            albumId: 33,
            userId: "u1"
        )
        store.save([record], albumId: 33)

        store.remove(albumId: 33, photoId: "1")

        XCTAssertFalse(FileManager.default.fileExists(atPath: file.path))
        XCTAssertTrue(store.load(albumId: 33).isEmpty)

        let another = try store.uniqueOriginalFileURL(filename: "B.JPG", albumId: 33)
        try Data([2]).write(to: another)
        store.clearAlbum(albumId: 33)

        XCTAssertFalse(FileManager.default.fileExists(atPath: another.deletingLastPathComponent().deletingLastPathComponent().path))
    }

    private func makeStore(root: URL) -> TravelAlbumOTGPhotoStore {
        TravelAlbumOTGPhotoStore(
            context: TravelAlbumOTGStorageContext(
                accountCachePrefix: "u1_photog",
                userId: "u1",
                scenicId: 11,
                storeId: 22
            ),
            applicationSupportDirectory: root.appendingPathComponent("ApplicationSupport", isDirectory: true),
            cachesDirectory: root.appendingPathComponent("Caches", isDirectory: true)
        )
    }

    private func makeTempDirectory() throws -> URL {
        let url = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString, isDirectory: true)
        try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
        return url
    }
}
