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(col: String, img: String, id: i32, cp: i32, rank: i32, rare: String, user_handle: String, user_did: String) -> String {
    let did = data_toml(&"did");
    let handle = data_toml(&"handle");
    let url = url(&"record_create");
    let d = Timestamp::now_utc();
    let d = d.to_string();
    let link = "https://bsky.app/profile/yui.syui.ai".to_string();
    let post = json!({
        "repo": handle.to_string(),
        "did": did.to_string(),
        "collection": col.to_string(),
        "record": {
            "id": id,
            "cp": cp,
            "rank": rank,
            "rare": rare.to_string(),
            "handle": user_handle.to_string(),
            "did": user_did.to_string(),
            "embed": {
                "$type": "app.bsky.embed.external",
                "external": {
                    "uri": link,
                    "thumb": {
                      "$type": "blob",
                      "ref": {
                        "$link": img.to_string()
                      },
                      "mimeType": "image/jpeg",
                      "size": 0
                    }
                }
            },
            "createdAt": d.to_string(),
        },
    });

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