#include "gtest/gtest.h"
#include "lumacs/register_manager.hpp"
#include <string>

using namespace lumacs;

// Fixture for RegisterManager tests
class RegisterManagerTest : public ::testing::Test {
protected:
    RegisterManager reg_manager;

    void SetUp() override {
        // Ensure registers are clear at the start of each test (though by default they should be)
    }

    void TearDown() override {
        // Nothing specific needed
    }
};

TEST_F(RegisterManagerTest, InitialState) {
    ASSERT_FALSE(reg_manager.get_from_register('a').has_value());
    ASSERT_FALSE(reg_manager.get_from_register('Z').has_value());
}

TEST_F(RegisterManagerTest, CopyToRegisterAndRetrieve) {
    char reg_name = 'x';
    std::string content = "This is a test string.";

    reg_manager.copy_to_register(reg_name, content);
    
    std::optional<std::string> retrieved_content = reg_manager.get_from_register(reg_name);
    ASSERT_TRUE(retrieved_content.has_value());
    ASSERT_EQ(retrieved_content.value(), content);
}

TEST_F(RegisterManagerTest, OverwriteRegister) {
    char reg_name = 'b';
    std::string content1 = "First content";
    std::string content2 = "Second content";

    reg_manager.copy_to_register(reg_name, content1);
    ASSERT_EQ(reg_manager.get_from_register(reg_name).value(), content1);

    reg_manager.copy_to_register(reg_name, content2);
    ASSERT_EQ(reg_manager.get_from_register(reg_name).value(), content2);
}

TEST_F(RegisterManagerTest, GetFromEmptyOrNonExistentRegister) {
    ASSERT_FALSE(reg_manager.get_from_register('c').has_value()); // Non-existent
    
    char reg_name = 'd';
    // Don't copy anything to 'd'
    ASSERT_FALSE(reg_manager.get_from_register(reg_name).has_value()); // Empty
}

TEST_F(RegisterManagerTest, DifferentRegisterNames) {
    reg_manager.copy_to_register('1', "Number One");
    reg_manager.copy_to_register('z', "Last Letter");

    ASSERT_EQ(reg_manager.get_from_register('1').value(), "Number One");
    ASSERT_EQ(reg_manager.get_from_register('z').value(), "Last Letter");
    ASSERT_FALSE(reg_manager.get_from_register('2').has_value());
}

TEST_F(RegisterManagerTest, InvalidRegisterName) {
    // RegisterManager::copy_to_register only takes alnum chars, no check needed here.
    // However, EditorCore's copy_to_register might have validation.
    // For unit testing, assume input is valid as per the method signature.
}
