#include <iostream>
#include <exception>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_sinks.h>
#include <spdlog/sinks/basic_file_sink.h>
#include "./core/server.hpp"
#include "./utils/args_parser.hpp"

int main(int argc, char* argv[])
{
    lsp::utils::ArgsParser& args_parser = lsp::utils::ArgsParser::Instance();
    auto& config = args_parser.Parse(argc, argv);

    if (config.show_help)
    {
        lsp::utils::ArgsParser::PrintHelp(argv[0]);
        return 0;
    }

    lsp::utils::ArgsParser::SetupLogger(config);

    try
    {
        spdlog::info("TSL-LSP server starting...");
        lsp::core::LspServer server(config.thread_count);
        server.Run();
    }
    catch (const std::exception& e)
    {
        std::cerr << "[TSL-LSP] Server fatal error: " << e.what() << std::endl;
        spdlog::error("Server fatal error: ", e.what());
        return 1;
    }
    catch (...)
    {
        std::cerr << "[TSL-LSP] Server unknown fatal error" << std::endl;
        spdlog::error("Server unknown fatal error");
        return 1;
    }

    spdlog::info("TSL-LSP server stopped normally");
    spdlog::shutdown();
    return 0;
}
