#include "facebook.h"

#include <iostream>
#include <httppp/http.h>

namespace blogi {
namespace social {

    void Facebook::share(const int tid, SharePostType type, const std::string &url,
                         const std::string &title, const std::string &description) {
        // Read Facebook Page Access Token and Page ID from config
        std::string accessToken, pageId;
        try { getConfig(*_args->database[tid], "SOCIAL_FB_ACCESS_TOKEN", accessToken); } catch(...) {}
        try { getConfig(*_args->database[tid], "SOCIAL_FB_PAGE_ID", pageId); } catch(...) {}

        if(accessToken.empty() || pageId.empty()){
            std::cerr << "[social/facebook] Not configured (SOCIAL_FB_ACCESS_TOKEN / SOCIAL_FB_PAGE_ID missing)" << std::endl;
            return;
        }

        // Build Graph API request: POST /{page-id}/feed?message=...&link=...&access_token=...
        std::string apiUrl = "https://graph.facebook.com/v19.0/" + pageId + "/feed";

        std::string body = "message=" + title + "\n" + description
                         + "&link=" + url
                         + "&access_token=" + accessToken;

        try {
            libhttppp::HttpUrl graphUrl(apiUrl);
            libhttppp::HttpRequest req;
            req.setRequestURL(apiUrl.c_str());
            // Actual HTTP POST to Graph API would be done via the HTTP client.
            // For now, log intent — the HTTP client integration depends on the
            // available networking library in the project.
            std::cerr << "[social/facebook] Sharing: " << title << " -> " << url << std::endl;
        } catch(std::exception &e) {
            std::cerr << "[social/facebook] Error: " << e.what() << std::endl;
        }
    }

} // namespace social
} // namespace blogi

