import Foundation
import Localization

public struct CommentsPage: Codable {
    public enum CommentsSortOrder: String {
        case newest // (default): Sort by creation date, newest first
        case oldest // Sort by creation date, oldest first
        case mostLiked = "most_liked" // most_liked`: Sort by number of likes, highest first

        var propertyValue: String {
            return rawValue
        }
    }

    enum CodingKeys: String, CodingKey {
        case nextCursor = "next_cursor"
        case results
        case allowComment = "allow_comment"
        case disableReason = "disable_reason"
    }

    public let nextCursor: String?
    public let results: [ClipComment]
    public let allowComment: Bool
    public let disableReason: String?

    public init(
        nextCursor: String?,
        results: [ClipComment],
        allowComment: Bool,
        disableReason: String?
    ) {
        self.nextCursor = nextCursor
        self.results = results
        self.allowComment = allowComment
        self.disableReason = disableReason
    }

    init(_ remote: Components.Schemas.CommentsPage) throws {
        nextCursor = remote.next_cursor
        results = remote.results?.compactMap { try? ClipComment($0) } ?? []
        allowComment = remote.allow_comment ?? false
        disableReason = remote.disable_reason
    }
}

public struct CommentsSheetPage: Codable, Equatable {
    public let allowComment: Bool
    public let disableReason: String?
    public let nextCursor: String?
    public let results: [CommentEntity]
    public let totalCount: Int?

    public init(
        allowComment: Bool,
        disableReason: String?,
        nextCursor: String?,
        results: [CommentEntity],
        totalCount: Int?
    ) {
        self.allowComment = allowComment
        self.disableReason = disableReason
        self.nextCursor = nextCursor
        self.results = results
        self.totalCount = totalCount
    }

    init(_ remote: GenAPI.GetCommentsResponse) throws {
        self.allowComment = remote.allowComment
        self.disableReason = remote.disableReason
        self.nextCursor = remote.nextCursor
        self.results = try remote.results.map { try CommentEntity($0) }
        self.totalCount = remote.totalCount
    }
}

public extension CommentsSheetPage {
    enum CommentsSortOrder: String {
        case newest // (default): Sort by creation date, newest first
        case oldest // Sort by creation date, oldest first
        case mostLiked = "most_liked" // Sort by number of likes, highest first

        var propertyValue: String {
            return rawValue
        }
    }
}
