use crate::http_client::HttpClient;
use crate::data_toml;
use crate::url;
use iso8601_timestamp::Timestamp;
use serde_json::json;

pub async fn post_request(
    text: String,
    link: String,
    cid: String,
    uri: String,
    itype: String,
) -> String {
    let did = data_toml(&"did");
    let handle = data_toml(&"handle");
    let url = url(&"record_create");
    let col = "app.bsky.feed.post".to_string();

    let d = Timestamp::now_utc();
    let d = d.to_string();

    let post = json!({
        "repo": handle.to_string(),
        "did": did.to_string(),
        "collection": col.to_string(),
        "record": {
            "createdAt": d.to_string(),
            "text": text.to_string(),
            "embed": {
                "$type": "app.bsky.embed.images",
                "images": [
                    {
                        "alt": "",
                        "image": {
                            "$type":"blob",
                            "ref": {
                                "$link" : link.to_string()
                            },
                            "mimeType": itype.to_string(),
                                "size": 0
                        }
                    }
                ]
            },
            "reply": {
                "root": {
                    "cid": cid.to_string(),
                    "uri": uri.to_string()
                },
                "parent": {
                    "cid": cid.to_string(),
                    "uri": uri.to_string()
                }
            }
        }
    });

    let client = HttpClient::new();
    
    match client.post_json_with_auth(&url, &post).await {
        Ok(response) => response,
        Err(e) => format!("Error: {}", e),
    }
}
