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

//use crate::data::Follow;

pub async fn post_request(u: String) -> String {
    let did = data_toml(&"did");
    let handle = data_toml(&"handle");

    let url = url(&"record_create");
    let col = "app.bsky.graph.follow".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": {
            "subject": u.to_string(),
            "createdAt": d.to_string(),
        },
    });

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

pub async fn delete_request(u: String, rkey: String) -> String {
    let did = data_toml(&"did");
    let handle = data_toml(&"handle");

    let url = url(&"record_delete");
    let col = "app.bsky.graph.follow".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(),
        "rkey": rkey.to_string(),
        "record": {
            "subject": u.to_string(),
            "createdAt": d.to_string(),
        },
    });

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