#define _POSIX_C_SOURCE 200809L

#include "tools_internal.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "cjson/cJSON.h"
#include "../nostr_handler.h"

static char* json_error_local(const char* msg) {
    cJSON* root = cJSON_CreateObject();
    if (!root) return NULL;
    cJSON_AddBoolToObject(root, "success", 0);
    cJSON_AddStringToObject(root, "error", msg ? msg : "unknown error");
    char* out = cJSON_PrintUnformatted(root);
    cJSON_Delete(root);
    return out;
}

static cJSON* parse_args_local(const char* args_json) {
    const char* raw = args_json ? args_json : "{}";
    cJSON* args = cJSON_Parse(raw);
    if (!args || !cJSON_IsObject(args)) {
        cJSON_Delete(args);
        return NULL;
    }
    return args;
}

char* execute_admin_identity(tools_context_t* ctx, const char* args_json) {
    if (!ctx || !ctx->cfg) return json_error_local("tool context unavailable");

    cJSON* args = parse_args_local(args_json);
    if (!args) return json_error_local("invalid arguments JSON");
    cJSON_Delete(args);

    cJSON* out = cJSON_CreateObject();
    if (!out) return NULL;

    const char* tier_text = "Sender tier unknown.";
    if (ctx->template_sender_tier == 1) {
        tier_text = "This message has been cryptographically verified as coming from your administrator.";
    } else if (ctx->template_sender_tier == 2) {
        tier_text = "This message is from a web-of-trust contact (not the administrator).";
    }

    cJSON_AddBoolToObject(out, "success", 1);

    char content[768];
    snprintf(content,
             sizeof(content),
             "## Administrator Identity (source: config.admin.pubkey)\n\n"
             "This is your administrator! Admin pubkey (hex): %s\n\n"
             "%s",
             ctx->cfg->admin.pubkey[0] ? ctx->cfg->admin.pubkey : "unknown",
             tier_text);
    cJSON_AddStringToObject(out, "content", content);

    char* json = cJSON_PrintUnformatted(out);
    cJSON_Delete(out);
    return json;
}

char* execute_nostr_admin_profile(tools_context_t* ctx, const char* args_json) {
    if (!ctx || !ctx->cfg) return json_error_local("tool context unavailable");

    cJSON* args = parse_args_local(args_json);
    if (!args) return json_error_local("invalid arguments JSON");
    cJSON_Delete(args);

    char* kind0 = nostr_handler_get_admin_kind0_context();
    const char* profile_json = (kind0 && kind0[0]) ? kind0 : "{}";

    cJSON* out = cJSON_CreateObject();
    if (!out) {
        free(kind0);
        return NULL;
    }

    cJSON_AddBoolToObject(out, "success", 1);

    size_t content_len = strlen("## Administrator Kind 0 Profile (source: nostr kind 0)\n\nAdministrator kind 0 profile content (JSON): ") + strlen(profile_json) + 1U;
    char* content = (char*)malloc(content_len);
    if (!content) {
        cJSON_Delete(out);
        free(kind0);
        return NULL;
    }
    snprintf(content,
             content_len,
             "## Administrator Kind 0 Profile (source: nostr kind 0)\n\nAdministrator kind 0 profile content (JSON): %s",
             profile_json);
    cJSON_AddStringToObject(out, "content", content);
    free(content);

    char* json = cJSON_PrintUnformatted(out);
    cJSON_Delete(out);
    free(kind0);
    return json;
}

char* execute_nostr_admin_contacts(tools_context_t* ctx, const char* args_json) {
    if (!ctx || !ctx->cfg) return json_error_local("tool context unavailable");

    cJSON* args = parse_args_local(args_json);
    if (!args) return json_error_local("invalid arguments JSON");
    cJSON_Delete(args);

    char* kind3 = nostr_handler_get_admin_kind3_context();
    const char* contacts_json = (kind3 && kind3[0]) ? kind3 : "[]";

    cJSON* out = cJSON_CreateObject();
    if (!out) {
        free(kind3);
        return NULL;
    }

    cJSON_AddBoolToObject(out, "success", 1);

    size_t content_len = strlen("## Administrator Kind 3 Contacts (source: nostr kind 3)\n\nAdministrator contacts (JSON): ") + strlen(contacts_json) + 1U;
    char* content = (char*)malloc(content_len);
    if (!content) {
        cJSON_Delete(out);
        free(kind3);
        return NULL;
    }
    snprintf(content,
             content_len,
             "## Administrator Kind 3 Contacts (source: nostr kind 3)\n\nAdministrator contacts (JSON): %s",
             contacts_json);
    cJSON_AddStringToObject(out, "content", content);
    free(content);

    char* json = cJSON_PrintUnformatted(out);
    cJSON_Delete(out);
    free(kind3);
    return json;
}

char* execute_nostr_admin_relays(tools_context_t* ctx, const char* args_json) {
    if (!ctx || !ctx->cfg) return json_error_local("tool context unavailable");

    cJSON* args = parse_args_local(args_json);
    if (!args) return json_error_local("invalid arguments JSON");
    cJSON_Delete(args);

    char* kind10002 = nostr_handler_get_admin_kind10002_context();
    const char* relays_json = (kind10002 && kind10002[0]) ? kind10002 : "[]";

    cJSON* out = cJSON_CreateObject();
    if (!out) {
        free(kind10002);
        return NULL;
    }

    cJSON_AddBoolToObject(out, "success", 1);

    size_t content_len = strlen("## Administrator Kind 10002 Relays (source: nostr kind 10002)\n\nAdministrator relay list (JSON): ") + strlen(relays_json) + 1U;
    char* content = (char*)malloc(content_len);
    if (!content) {
        cJSON_Delete(out);
        free(kind10002);
        return NULL;
    }
    snprintf(content,
             content_len,
             "## Administrator Kind 10002 Relays (source: nostr kind 10002)\n\nAdministrator relay list (JSON): %s",
             relays_json);
    cJSON_AddStringToObject(out, "content", content);
    free(content);

    char* json = cJSON_PrintUnformatted(out);
    cJSON_Delete(out);
    free(kind10002);
    return json;
}

char* execute_nostr_admin_notes(tools_context_t* ctx, const char* args_json) {
    if (!ctx || !ctx->cfg) return json_error_local("tool context unavailable");

    cJSON* args = parse_args_local(args_json);
    if (!args) return json_error_local("invalid arguments JSON");
    cJSON_Delete(args);

    char* notes = nostr_handler_get_admin_kind1_notes_context();
    const char* notes_text = (notes && notes[0]) ? notes : "";

    cJSON* out = cJSON_CreateObject();
    if (!out) {
        free(notes);
        return NULL;
    }

    cJSON_AddBoolToObject(out, "success", 1);

    size_t content_len = strlen("## Administrator Recent Kind 1 Notes\n\n") + strlen(notes_text) + 1U;
    char* content = (char*)malloc(content_len);
    if (!content) {
        cJSON_Delete(out);
        free(notes);
        return NULL;
    }
    snprintf(content,
             content_len,
             "## Administrator Recent Kind 1 Notes\n\n%s",
             notes_text);
    cJSON_AddStringToObject(out, "content", content);
    free(content);

    char* json = cJSON_PrintUnformatted(out);
    cJSON_Delete(out);
    free(notes);
    return json;
}
