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, text: String, at: String, udid: String, s: i32, e: i32) -> 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 post = json!({
        "did": did.to_string(),
        "repo": handle.to_string(),
        "collection": col.to_string(),
        "record": {
            "text": at.to_string() + &" ".to_string() + &text.to_string(),
            "$type": col.to_string(),
            "createdAt": d.to_string(),
            "facets": [
            {
                "$type": "app.bsky.richtext.facet",
                "index": {
                    "byteEnd": e,
                    "byteStart": s
                },"features": [
                {
                    "did": udid.to_string(),
                    "$type": "app.bsky.richtext.facet#mention"
                }
                ]
            }
            ]
        },
    });

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