<?php

namespace Macymed\MacymedLinkTracker\Utils;

if (! defined('ABSPATH')) {
    exit; // Sécurité
}

define('MACYMED_LINK_TRACKER_DB_VERSION', 21);
class Database
{
    public static function init()
    {
        global $wpdb;

        // Récupérer la version actuelle de la base de données
        $installed_version = intval(get_option('macymed_link_tracker_db_version', 0));

        // Vérifier si la version de la base de données doit être mise à jour
        if ($installed_version < MACYMED_LINK_TRACKER_DB_VERSION) {
            global $wp_filesystem;

            // Initialiser WP_Filesystem
            if (!function_exists('WP_Filesystem')) {
                require_once ABSPATH . 'wp-admin/includes/file.php';
                $creds = request_filesystem_credentials(site_url());
                wp_filesystem($creds);
            }

            // if (!WP_Filesystem()) {
            //     wp_die(__('Impossible d\'initialiser WP_Filesystem.', 'textdomain'));
            // }

            // Définir un emplacement temporaire pour le fichier CSV

            $table_name_main   = MACYMED_LINK_TRACKER_TABLE_NAME;
            $table_name_clicks = MACYMED_LINK_TRACKER_CLICKS_TABLE_NAME;
            $table_name_page_views = MACYMED_LINK_TRACKER_PAGE_VIEWS_TABLE_NAME;
            $table_name_ai_cache = MACYMED_LINK_TRACKER_AI_CACHE_TABLE_NAME;
            $charset_collate   = $wpdb->get_charset_collate();

            for ($i = $installed_version; $i < intval(MACYMED_LINK_TRACKER_DB_VERSION) + 1; $i++) {
                $fileMigration = Utils::get_plugin_dir_path() . 'database/migrations/migration_' . $i . '.sql';
                if (file_exists($fileMigration)) {
                    // $sql = file_get_contents($fileMigration);
                    $sql = $wp_filesystem->get_contents($fileMigration);
                    $sql = str_replace(
                        [
                            '[[table_name_main]]',
                            '[[table_name_clicks]]',
                            '[[table_name_page_views]]',
                            '[[table_name_ai_cache]]',
                            '[[wp_prefix]]',
                            '[[charset_collate]]',
                        ],
                        [
                            $table_name_main,
                            $table_name_clicks,
                            $table_name_page_views,
                            $table_name_ai_cache,
                            $wpdb->prefix,

                            $charset_collate,
                        ],
                        $sql
                    );
                    require_once ABSPATH . 'wp-admin/includes/upgrade.php';
                    // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
                    $wpdb->query($sql);
                    // dbDelta($sql);
                }
                update_option('macymed_link_tracker_db_version', $i);

            }
        }
    }
}
