package net.Chipperfluff.chipi.command;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;

import net.Chipperfluff.chipi.entity.ModEntities;
import net.Chipperfluff.chipi.entity.PlayerJrEntity;

public final class SpawnJrCommand {

    public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
        dispatcher.register(
            CommandManager.literal("spawnjr")
                .requires(src -> src.hasPermissionLevel(2))
                .then(
                    CommandManager.argument("dad", EntityArgumentType.player())
                        .executes(SpawnJrCommand::execute)
                )
        );
    }

    private static int execute(CommandContext<ServerCommandSource> ctx) {
        ServerCommandSource source = ctx.getSource();

        System.out.println("[spawnjr] ================================");
        System.out.println("[spawnjr] Command invoked");

        try {
            ServerWorld world = source.getWorld();
            ServerPlayerEntity dad = EntityArgumentType.getPlayer(ctx, "dad");
            ServerPlayerEntity spawner = source.getPlayerOrThrow();

            BlockPos pos = spawner.getBlockPos();

            System.out.println("[spawnjr] Dad = " + dad.getName().getString());
            System.out.println("[spawnjr] Dad UUID = " + dad.getUuidAsString());
            System.out.println("[spawnjr] World = " + world.getRegistryKey().getValue());
            System.out.println("[spawnjr] Spawn pos = " + pos);

            PlayerJrEntity jr = new PlayerJrEntity(ModEntities.PLAYER_JR, world);
            System.out.println("[spawnjr] Entity constructed");

            jr.refreshPositionAndAngles(
                pos.getX() + 0.5,
                pos.getY(),
                pos.getZ() + 0.5,
                world.random.nextFloat() * 360f,
                0f
            );

            // single source of truth
            jr.setDad(dad);
            System.out.println("[spawnjr] Dad relationship set");

            boolean spawned = world.spawnEntity(jr);
            System.out.println("[spawnjr] spawnEntity() returned = " + spawned);

            if (!spawned) {
                source.sendError(Text.literal("[spawnjr] Spawn failed (entity rejected by world)"));
                return 0;
            }

            System.out.println("[spawnjr] SUCCESS");
            System.out.println("[spawnjr] ================================");
            return 1;

        } catch (Exception e) {
            System.out.println("[spawnjr] ================================");
            System.out.println("[spawnjr] CRASH");
            System.out.println("[spawnjr] hey message Chipperfluff");
            System.out.println("[spawnjr] Exception: " + e.getClass().getName());
            System.out.println("[spawnjr] Message: " + e.getMessage());
            e.printStackTrace();

            source.sendError(Text.literal(
                "[spawnjr] Internal error. Check logs. (hey message Chipperfluff)"
            ));
            return 0;
        }
    }
}
