//
//  LNHttpManager+Feed.swift
//  Gami
//
//  Created by OneeChan on 2026/2/28.
//

import Foundation


private let kNetPath_Feed_Like = "/trend/trend/like"
private let kNetPath_Feed_List = "/trend/list"
private let kNetPath_Feed_Detail = "/trend/detail"
private let kNetPath_Feed_Create = "/trend/create"
private let kNetPath_Feed_Comment = "/trend/comment"
private let kNetPath_Feed_Comment_List = "/trend/comment/list"
private let kNetPath_Feed_Comment_Like = "/trend/comment/like"
private let kNetPath_Feed_Del = "/trend/trend/del"


struct LNPostFeedItem {
    var text: String
    var medias: [LNFeedMediaVO]
}


extension LNHttpManager {
    func likeFeed(id: String, completion: @escaping (LNFeedLikeResponse?, LNHttpError?) -> Void) {
        post(path: kNetPath_Feed_Like, params: [
            "id": id
        ], completion: completion)
    }
    
    func getFeedList(uid: String, size: Int, next: String, completion: @escaping (LNFeedListResponse?, LNHttpError?) -> Void) {
        post(path: kNetPath_Feed_List, params: [
            "userNo": uid,
            "page": [
                "size": size,
                "next": next
            ]
        ], completion: completion)
    }
    
    func getFeedDetail(id: String, completion: @escaping (LNFeedDetailVO?, LNHttpError?) -> Void) {
        post(path: kNetPath_Feed_Detail, params: [
            "id": id
        ], completion: completion)
    }
    
    func postFeed(item: LNPostFeedItem, completion: @escaping (LNHttpError?) -> Void) {
        var param: [String: Any] = [
            "content": item.text,
        ]
        var medias: [[String: Any]] = []
        for media in item.medias {
            medias.append(
                [
                    "url": media.url,
                    "type": media.type.rawValue,
                    "videoCover": media.videoCover
                ]
            )
        }
//        if !medias.isEmpty {
            param["medias"] = medias
//        }
        post(path: kNetPath_Feed_Create, params: param, completion: completion)
    }
    
    func sendFeedComment(id: String, content: String, completion: @escaping (LNHttpError?) -> Void) {
        post(path: kNetPath_Feed_Comment, params: [
            "trendId": id,
            "content": content
        ], completion: completion)
    }
    
    func getFeedCommentList(id: String, size: Int, next: String, completion: @escaping (LNFeedCommentListResponse?, LNHttpError?) -> Void) {
        post(path: kNetPath_Feed_Comment_List, params: [
            "trendId": id,
            "page": [
                "size": size,
                "next": next
            ]
        ], completion: completion)
    }
    
    func listFeedComment(id: String, completion: @escaping (LNHttpError?) -> Void) {
        post(path: kNetPath_Feed_Comment_Like, params: [
            "id": id
        ], completion: completion)
    }
    
    func deleteFeed(id: String, completion: @escaping (LNHttpError?) -> Void) {
        post(path: kNetPath_Feed_Del, params: [
            "id": id
        ], completion: completion)
    }
}
