// SPDX-FileCopyrightText: 2026 John Burwell and contributors
// SPDX-License-Identifier: AGPL-3.0-or-later

import Foundation

struct ContentItemFileFacts: Sendable {
    let storageFormatLabel: String
    let createdDateText: String
    let approximateAgeText: String?

    nonisolated init(item: MinecraftContentItem, now: Date = .now, fileManager: FileManager = .default) {
        let createdDate = try? item.folderURL.resourceValues(forKeys: [.creationDateKey]).creationDate
        self.createdDateText = createdDate?.formatted(date: .abbreviated, time: .omitted) ?? "Unknown"

        if let createdDate {
            let components = Calendar.current.dateComponents([.year, .month], from: createdDate, to: now)
            if let year = components.year, year > 0 {
                self.approximateAgeText = year == 1 ? "About 1 year old" : "About \(year) years old"
            } else if let month = components.month, month > 0 {
                self.approximateAgeText = month == 1 ? "About 1 month old" : "About \(month) months old"
            } else {
                self.approximateAgeText = "Recently created"
            }
        } else {
            self.approximateAgeText = nil
        }

        switch item.sourceEdition {
        case .bedrock:
            switch item.contentType {
            case .world:
                let levelDBURL = item.folderURL.appendingPathComponent("db", isDirectory: true)
                self.storageFormatLabel = fileManager.fileExists(atPath: levelDBURL.path)
                    ? "LevelDB world storage"
                    : "Flat-file world storage"
            case .behaviorPack, .resourcePack, .skinPack, .worldTemplate:
                self.storageFormatLabel = "Manifest-based package"
            }
        case .java:
            switch item.contentKind {
            case .world:
                self.storageFormatLabel = "Anvil world storage"
            case .mod:
                self.storageFormatLabel = "Java mod archive"
            case .shaderPack:
                self.storageFormatLabel = "Shader pack archive"
            case .resourcePack:
                self.storageFormatLabel = "Resource pack archive"
            case .dataPack:
                self.storageFormatLabel = "Data pack archive"
            case .behaviorPack, .skinPack, .worldTemplate:
                self.storageFormatLabel = "Java content"
            }
        }
    }
}
