import Foundation

/// Recursive domain tree (documents_tree.json is a top-level ARRAY). `children` is optional so
/// SwiftUI OutlineGroup can treat leaves cleanly; `kids` is the non-optional accessor.
public struct DomainTreeNode: Codable, Sendable, Identifiable {
    public let name: String
    public let path: String
    public let count: Int
    public let children: [DomainTreeNode]?

    public var id: String { path }
    public var kids: [DomainTreeNode] { children ?? [] }
}

/// GET /documents/stats/category-counts — 서버가 Pydantic response model 없이 raw dict 를 반환하는
/// 엔드포인트. 초기 계약 추출이 이 shape 을 잘못 합성(total/by_domain/...)해 라이브 결선에서 decode
/// 실패 → 2026-06-07 라이브 재캡처로 정정 (fixture documents_stats.json = CAPTURED_LIVE).
public struct CategoryCounts: Codable, Sendable {
    /// category(enum)별 건수 — 예: document/library/news/law/memo/audio.
    public let counts: [String: Int]
    public let libraryPendingSuggestions: Int

    enum CodingKeys: String, CodingKey {
        case counts
        case libraryPendingSuggestions = "library_pending_suggestions"
    }

    /// 전체 건수 (counts 합) — 파생 접근자.
    public var total: Int { counts.values.reduce(0, +) }
}

public struct DuplicateGroup: Codable, Sendable, Identifiable {
    public let canonicalId: Int
    public let members: [Int]
    public let reason: String
    public let detail: String?

    public var id: Int { canonicalId }

    enum CodingKeys: String, CodingKey {
        case members, reason, detail
        case canonicalId = "canonical_id"
    }
}

public struct DuplicatesResponse: Codable, Sendable {
    public let groups: [DuplicateGroup]
    public let totalGroups: Int
    public let totalDuplicateDocs: Int

    enum CodingKeys: String, CodingKey {
        case groups
        case totalGroups = "total_groups"
        case totalDuplicateDocs = "total_duplicate_docs"
    }
}
