package com.firefrostgaming.bitchbot;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

/**
 * Plain-old-data representation of provision.json. See spec in
 * REQ-2026-04-15-bitch-bot.md. Loaded once at ServerStartedEvent.
 *
 * Field names mirror the JSON exactly so Gson can deserialize without
 * additional annotations except where Java reserved words / casing collide.
 */
public class ProvisionConfig {

    public String version;
    public String schematic_name;
    public String schematic_url;
    public String schematic_hash;          // SHA-256 hex (lowercase)
    public String spawn_type;              // "standard" | future variants
    public Vec3i  spawn_coords;
    public String paste_origin;            // "center" | "corner"
    public Vec3i  worldspawn_offset;
    public List<CommandBlockEntry> command_blocks;
    public List<RulesSign> rules_signs;
    public YawpRegion yawp_region;
    public Boolean self_destruct_on_success;

    public static class Vec3i {
        public int x;
        public int y;
        public int z;
    }

    public static class CommandBlockEntry {
        public Vec3i position;
        public String facing;              // up | down | north | south | east | west
    }

    public static class RulesSign {
        public Vec3i position;
        public List<String> lines;         // up to 4
    }

    public static class YawpRegion {
        public String name;                // region name (e.g. "spawn")
        public Vec3i  min;
        public Vec3i  max;
        public List<String> flags;         // e.g. ["no-pvp", "no-mob-spawning"]
    }

    /** Load + parse provision.json from the given path. Returns null if missing. */
    public static ProvisionConfig loadOrNull(Path path) {
        if (!Files.exists(path)) return null;
        try {
            String json = Files.readString(path);
            Gson gson = new GsonBuilder().setLenient().create();
            return gson.fromJson(json, ProvisionConfig.class);
        } catch (Exception e) {
            BitchBot.LOGGER.error("[BitchBot] Failed to parse provision.json: {}", e.getMessage());
            return null;
        }
    }

    public boolean shouldSelfDestruct() {
        return self_destruct_on_success != null && self_destruct_on_success;
    }
}
