<?php

defined("ABSPATH") || exit();

class Chit_Uninstaller
{
    public static function uninstall(): void
    {
        global $wpdb;

        $wpdb->query(
            "DROP TABLE IF EXISTS {$wpdb->prefix}chit_codes"
        );
        $wpdb->query(
            "DROP TABLE IF EXISTS {$wpdb->prefix}chit_batches"
        );

        delete_option("chit_db_version");
        delete_option("chit_code_format");
        delete_option("chit_redemption_catalog_price");
        delete_option("chit_allow_guest_redemption");
        delete_option("chit_logo_id");
        delete_option("chit_redeem_page_id");
        delete_option("chit_import_state");
        delete_option("chit_import_history");

        $imports = trailingslashit(wp_upload_dir()["basedir"]) . "chit-imports";
        if (is_dir($imports)) {
            foreach (glob("$imports/{,.}*", GLOB_BRACE) ?: [] as $file) {
                if (is_file($file)) {
                    @unlink($file);
                }
            }
            @rmdir($imports);
        }
    }
}
