import Foundation

public struct MemoResponse: Codable, Sendable, Identifiable {
    public let id: Int
    public let title: String?
    public let content: String?
    public let fileFormat: String?
    public let fileType: String?
    public let filePath: String?
    public let userTags: [String]?
    public let aiTags: [String]?
    public let aiDomain: String?
    public let aiSubGroup: String?
    public let aiSummary: String?
    public let pinned: Bool?
    public let archived: Bool?
    public let askIncludable: Bool?
    public let memoTaskState: JSONValue?
    public let aiEventKind: String?
    public let aiEventConfidence: Double?
    public let sourceChannel: String?
    public let sourceMetadata: JSONValue?
    public let createdAtRaw: String?
    public let updatedAtRaw: String?

    public var createdAt: Date? { DSDate.parse(createdAtRaw) }
    public var updatedAt: Date? { DSDate.parse(updatedAtRaw) }
    public var isPinned: Bool { pinned ?? false }
    public var isArchived: Bool { archived ?? false }
    public var isAudio: Bool { fileType == "audio" }

    /// Checked task indices derived from memo_task_state keys (index-keyed object).
    public var checkedTaskIndices: Set<Int> {
        guard let o = memoTaskState?.objectValue else { return [] }
        var s = Set<Int>()
        for k in o.keys { if let i = Int(k) { s.insert(i) } }
        return s
    }

    enum CodingKeys: String, CodingKey {
        case id, title, content, pinned, archived
        case fileFormat = "file_format"
        case fileType = "file_type"
        case filePath = "file_path"
        case userTags = "user_tags"
        case aiTags = "ai_tags"
        case aiDomain = "ai_domain"
        case aiSubGroup = "ai_sub_group"
        case aiSummary = "ai_summary"
        case askIncludable = "ask_includable"
        case memoTaskState = "memo_task_state"
        case aiEventKind = "ai_event_kind"
        case aiEventConfidence = "ai_event_confidence"
        case sourceChannel = "source_channel"
        case sourceMetadata = "source_metadata"
        case createdAtRaw = "created_at"
        case updatedAtRaw = "updated_at"
    }
}

public struct MemoListResponse: Codable, Sendable {
    public let items: [MemoResponse]
    public let total: Int
    public let page: Int
    public let pageSize: Int

    enum CodingKeys: String, CodingKey {
        case items, total, page
        case pageSize = "page_size"
    }
}
