#include "SdCard.hpp"
#include "esp_log.h"
#include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"
#include "driver/sdmmc_host.h"
#include "sd_pwr_ctrl_by_on_chip_ldo.h"
#include <sys/stat.h>

static const char* TAG = "SdCard";

// ESP32-P4 Waveshare Board SDMMC Pins (from official docs)
#define SDMMC_CLK_GPIO  43
#define SDMMC_CMD_GPIO  44
#define SDMMC_D0_GPIO   39
#define SDMMC_D1_GPIO   40
#define SDMMC_D2_GPIO   41
#define SDMMC_D3_GPIO   42

// SD card power control LDO channel (from Waveshare example)
#define SD_PWR_CTRL_LDO_CHANNEL  4

static sdmmc_card_t* s_card = nullptr;
static sd_pwr_ctrl_handle_t s_pwr_ctrl_handle = nullptr;

SdCard& SdCard::instance() {
    static SdCard inst;
    return inst;
}

bool SdCard::init() {
    if (mounted_) {
        ESP_LOGW(TAG, "SD card already mounted");
        return true;
    }

    ESP_LOGI(TAG, "Initializing SD card (SDMMC 4-wire mode)");

    // Configure SDMMC host
    sdmmc_host_t host = SDMMC_HOST_DEFAULT();
    host.max_freq_khz = SDMMC_FREQ_HIGHSPEED; // 40 MHz (faster image loading)

    // Initialize SD card power control via internal LDO
    sd_pwr_ctrl_ldo_config_t ldo_config = {
        .ldo_chan_id = SD_PWR_CTRL_LDO_CHANNEL,
    };

    esp_err_t ret = sd_pwr_ctrl_new_on_chip_ldo(&ldo_config, &s_pwr_ctrl_handle);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "Failed to create SD power control driver: %s", esp_err_to_name(ret));
        return false;
    }
    host.pwr_ctrl_handle = s_pwr_ctrl_handle;
    ESP_LOGI(TAG, "SD power control LDO initialized (channel %d)", SD_PWR_CTRL_LDO_CHANNEL);

    // Configure SDMMC slot with official Waveshare pinout
    sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
    slot_config.width = 4;
    slot_config.clk = static_cast<gpio_num_t>(SDMMC_CLK_GPIO);
    slot_config.cmd = static_cast<gpio_num_t>(SDMMC_CMD_GPIO);
    slot_config.d0 = static_cast<gpio_num_t>(SDMMC_D0_GPIO);
    slot_config.d1 = static_cast<gpio_num_t>(SDMMC_D1_GPIO);
    slot_config.d2 = static_cast<gpio_num_t>(SDMMC_D2_GPIO);
    slot_config.d3 = static_cast<gpio_num_t>(SDMMC_D3_GPIO);
    slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP;

    // Mount FAT filesystem
    esp_vfs_fat_sdmmc_mount_config_t mount_config = VFS_FAT_MOUNT_DEFAULT_CONFIG();
    mount_config.max_files = 5;
    mount_config.allocation_unit_size = 16 * 1024;

    ESP_LOGI(TAG, "Mounting SD card...");
    ret = esp_vfs_fat_sdmmc_mount(MOUNT_POINT, &host, &slot_config, &mount_config, &s_card);

    if (ret != ESP_OK) {
        if (ret == ESP_FAIL) {
            ESP_LOGE(TAG, "Failed to mount filesystem.");
        } else {
            ESP_LOGE(TAG, "Failed to initialize SD card (%s).", esp_err_to_name(ret));
        }
        // Clean up power control on failure
        sd_pwr_ctrl_del_on_chip_ldo(s_pwr_ctrl_handle);
        s_pwr_ctrl_handle = nullptr;
        return false;
    }

    mounted_ = true;

    // Print card info
    sdmmc_card_print_info(stdout, s_card);
    ESP_LOGI(TAG, "SD card mounted at %s", MOUNT_POINT);

    // Create directories if they don't exist
    struct stat st;
    const char* dirs[] = {
        "/sdcard/webseite",
        "/sdcard/images",
        "/sdcard/fonts"
    };

    for (const char* dir : dirs) {
        if (stat(dir, &st) != 0) {
            if (mkdir(dir, 0755) == 0) {
                ESP_LOGI(TAG, "Created directory: %s", dir);
            } else {
                ESP_LOGW(TAG, "Failed to create directory: %s", dir);
            }
        }
    }

    return true;
}
