// lua_fuzzer.h - Random Lua code generator for security testing
// Milestone 19: Security Testing Suite
#pragma once

#include <string>
#include <vector>
#include <random>
#include <cstdint>

namespace mosis {

struct FuzzResult {
    bool crashed = false;
    bool sandbox_intact = true;
    std::string error;
    size_t iterations = 0;
    size_t errors_caught = 0;
};

class LuaFuzzer {
public:
    explicit LuaFuzzer(uint32_t seed = 0);
    ~LuaFuzzer();

    // Run fuzzing for N iterations
    FuzzResult Run(size_t iterations);

    // Configuration
    void SetMaxCodeLength(size_t len) { m_max_code_length = len; }
    void SetMaxNesting(size_t depth) { m_max_nesting = depth; }

    // Statistics
    size_t GetTotalRuns() const { return m_total_runs; }
    size_t GetCrashes() const { return m_crashes; }
    size_t GetErrorsCaught() const { return m_errors_caught; }

private:
    std::mt19937 m_rng;
    size_t m_max_code_length = 1000;
    size_t m_max_nesting = 10;
    size_t m_total_runs = 0;
    size_t m_crashes = 0;
    size_t m_errors_caught = 0;

    // Code generators
    std::string GenerateRandomCode();
    std::string GenerateExpression(int depth);
    std::string GenerateStatement(int depth);
    std::string GenerateIdentifier();
    std::string GenerateLiteral();
    std::string GenerateOperator();
    std::string GenerateTableConstructor(int depth);
    std::string GenerateFunctionCall(int depth);

    // Sandbox integrity verification
    bool VerifySandboxIntegrity();

    // Helper
    int RandomInt(int min, int max);
    bool RandomBool();
    char RandomChar();
};

} // namespace mosis
