#include "instagram.h"

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

namespace blogi {
namespace social {

    void Instagram::share(const int tid, SharePostType type, const std::string &url,
                          const std::string &title, const std::string &description) {
        // Read Instagram Graph API credentials from config
        std::string accessToken, igUserId;
        try { getConfig(*_args->database[tid], "SOCIAL_IG_ACCESS_TOKEN", accessToken); } catch(...) {}
        try { getConfig(*_args->database[tid], "SOCIAL_IG_USER_ID", igUserId); } catch(...) {}

        if(accessToken.empty() || igUserId.empty()){
            std::cerr << "[social/instagram] Not configured (SOCIAL_IG_ACCESS_TOKEN / SOCIAL_IG_USER_ID missing)" << std::endl;
            return;
        }

        // Instagram Content Publishing API:
        // Step 1: Create media container
        // POST /{ig-user-id}/media?image_url=...&caption=...&access_token=...
        // Step 2: Publish container
        // POST /{ig-user-id}/media_publish?creation_id=...&access_token=...

        std::string caption = title;
        if(!description.empty())
            caption += "\n\n" + description;

        const char *mediaType = (type == ShareVideo) ? "VIDEO" : "IMAGE";

        std::cerr << "[social/instagram] Sharing " << mediaType << ": " << caption << " -> " << url << std::endl;

        // The actual HTTP POST to the Instagram Graph API requires an HTTP client.
        // url here is the publicly accessible media URL.
        // For images: POST /{ig-user-id}/media?image_url={url}&caption={caption}&access_token={token}
        // For videos: POST /{ig-user-id}/media?video_url={url}&caption={caption}&media_type=REELS&access_token={token}
    }

} // namespace social
} // namespace blogi

