// bobcord_java.java — SDK BobCord (REST + builders)
// User + Bot + EmbedBuilder + ActionRow / Button / WebAction
// API: https://aml-s9xx-box.taildd440a.ts.net/api/v1.1
// Java 11+ — HttpClient + org.json

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.*;
import org.json.JSONArray;
import org.json.JSONObject;

public class BobCord {
    public static final String DEFAULT_BASE = "https://aml-s9xx-box.taildd440a.ts.net/api/v1.1";

    public static JSONObject webActionConfirm(String title, String body, JSONObject then) {
        JSONObject o = new JSONObject().put("type", "confirm").put("title", title).put("body", body);
        if (then != null) o.put("then", then);
        return o;
    }
    public static JSONObject webActionSendMessage(String content, String channel, Long serverId, JSONObject confirm) {
        JSONObject o = new JSONObject().put("type", "send_message").put("content", content);
        if (channel != null) o.put("channel", channel);
        if (serverId != null) o.put("server_id", serverId);
        if (confirm != null) o.put("confirm", confirm);
        return o;
    }
    public static JSONObject webActionReact(String emoji, Object messageId, JSONObject confirm) {
        JSONObject o = new JSONObject().put("type", "react").put("emoji", emoji).put("message_id", messageId == null ? "self" : messageId);
        if (confirm != null) o.put("confirm", confirm);
        return o;
    }
    public static JSONObject button(String label, String style, String customId, String url, JSONObject webAction, boolean disabled) {
        String st = style.toUpperCase(Locale.ROOT);
        JSONObject o = new JSONObject().put("type", "BUTTON").put("style", st).put("label", label).put("disabled", disabled);
        if ("LINK".equals(st)) {
            if (url == null) throw new IllegalArgumentException("LINK requires url");
            o.put("url", url);
        } else {
            if (customId == null) throw new IllegalArgumentException("BUTTON requires custom_id");
            o.put("custom_id", customId);
        }
        if ("WEB_ACTION".equals(st)) {
            if (webAction == null) throw new IllegalArgumentException("WEB_ACTION requires web_action");
            o.put("web_action", webAction);
        }
        return o;
    }
    public static JSONObject actionRow(JSONObject... components) {
        JSONArray arr = new JSONArray();
        for (int i = 0; i < components.length && i < 5; i++) arr.put(components[i]);
        return new JSONObject().put("type", "ACTION_ROW").put("components", arr);
    }

    public static class EmbedBuilder {
        private final JSONObject data = new JSONObject().put("fields", new JSONArray());
        public EmbedBuilder title(String t) { data.put("title", t); return this; }
        public EmbedBuilder description(String d) { data.put("description", d); return this; }
        public EmbedBuilder color(int c) { data.put("color", c & 0xFFFFFF); return this; }
        public EmbedBuilder addField(String name, String value, boolean inline) {
            data.getJSONArray("fields").put(new JSONObject().put("name", name).put("value", value).put("inline", inline));
            return this;
        }
        public EmbedBuilder footer(String text) { data.put("footer", new JSONObject().put("text", text)); return this; }
        public JSONObject build() { return data; }
    }

    public static class User {
        public final String token;
        public String baseUrl = DEFAULT_BASE;
        public JSONObject me;
        private final HttpClient http = HttpClient.newHttpClient();
        public User(String token) { this.token = token.trim(); }

        public JSONObject request(String method, String path, JSONObject body) throws Exception {
            String url = baseUrl.replaceAll("/$", "") + "/" + path.replaceAll("^/", "");
            HttpRequest.Builder b = HttpRequest.newBuilder(URI.create(url))
                .header("Authorization", "Bearer " + token)
                .header("Content-Type", "application/json");
            String payload = body == null ? "" : body.toString();
            switch (method) {
                case "GET" -> b.GET();
                case "POST" -> b.POST(HttpRequest.BodyPublishers.ofString(payload, StandardCharsets.UTF_8));
                case "PUT" -> b.PUT(HttpRequest.BodyPublishers.ofString(payload, StandardCharsets.UTF_8));
                case "DELETE" -> b.method("DELETE", HttpRequest.BodyPublishers.ofString(payload, StandardCharsets.UTF_8));
                default -> b.GET();
            }
            HttpResponse<String> res = http.send(b.build(), HttpResponse.BodyHandlers.ofString());
            if (res.statusCode() == 204) return null;
            JSONObject data = res.body() == null || res.body().isBlank() ? new JSONObject() : new JSONObject(res.body());
            if (res.statusCode() >= 400) throw new RuntimeException("HTTP " + res.statusCode() + ": " + data);
            return data;
        }

        public JSONObject login() throws Exception {
            JSONObject data = request("GET", "/me", null);
            me = data.has("user") ? data.getJSONObject("user") : data;
            return me;
        }

        public JSONObject send(String content, String channel, long serverId, JSONArray embeds, JSONArray actionRows) throws Exception {
            JSONObject body = new JSONObject().put("content", content).put("channel", channel).put("server_id", serverId);
            if (embeds != null) body.put("embeds", embeds);
            if (actionRows != null) body.put("action_rows", actionRows);
            return request("POST", "/message", body);
        }
    }

    public static class Bot extends User {
        public Bot(String token) { super(token); }
    }
}
