// Copyright (c) 2025 shibedrill
// SPDX-License-Identifier: MIT

use crate::format;
use crate::memory::alloc::string::String;
use crate::{LOGGER, LogLevel, log_trace};
use lazy_static::lazy_static;
use limine::{
    request::{ExecutableFileRequest, ModuleRequest},
    response::ModuleResponse,
};

#[used]
#[unsafe(link_section = ".requests")]
pub static FILE_REQUEST: ExecutableFileRequest = limine::request::ExecutableFileRequest::new();

#[used]
#[unsafe(link_section = ".requests")]
pub static MODULE_REQUEST: ModuleRequest = limine::request::ModuleRequest::new();

lazy_static! {
    pub static ref MODULE_RESPONSE: &'static ModuleResponse = MODULE_REQUEST
        .get_response()
        .expect("Bootloader did not return kernel modules");
}

pub fn log_modules() {
    if !MODULE_RESPONSE.modules().is_empty() {
        let mut log_msg: String = String::from("Kernel modules list:\n");
        for module in MODULE_RESPONSE.modules() {
            log_msg.push_str(&format!(
                "\t{}",
                String::from_utf8_lossy(module.path().to_bytes())
            ));
        }
        log_trace!("{log_msg}")
    }
}
