<?php
/**
 * Jalpi Page Builder — Uninstall handler.
 *
 * Fired when the plugin is deleted via the WordPress admin.
 * Removes all database tables, options, post meta, transients,
 * and custom post type entries created by the plugin.
 *
 * @package JalpiPageBuilder
 */

// Abort if not called by WordPress.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
    exit;
}

/**
 * Run cleanup for a single site.
 */
function jalpi_uninstall_site() {
    global $wpdb;

    // ── Drop custom tables ───────────────────────────────────────────────────
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery
    $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}jalpi_pages" );
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery
    $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}jalpi_revisions" );

    // ── Delete plugin options ────────────────────────────────────────────────
    $options = [
        'jalpi_version',
        'jalpi_enabled_post_types',
        'jalpi_revisions_db_version',
        'jalpi_settings_general',
        'jalpi_settings_layout',
        'jalpi_settings_design',
        'jalpi_settings_performance',
        'jalpi_settings_breakpoints',
        '_jalpi_orphan_cleanup_v1',
    ];
    foreach ( $options as $option ) {
        delete_option( $option );
    }

    // ── Delete post meta across all posts ────────────────────────────────────
    $meta_keys = [
        '_jalpi_has_canvas_data',
        '_jalpi_render_error',
        '_jalpi_history',
        '_jalpi_tpl_type',
        '_jalpi_tpl_category',
        '_jalpi_tpl_thumbnail',
        '_jalpi_tpl_location',
        '_jalpi_tpl_status',
        '_jalpi_tpl_priority',
        '_jalpi_tpl_conditions',
        '_jalpi_is_bundled',
        '_jalpi_bundled_slug',
    ];
    foreach ( $meta_keys as $key ) {
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery
        $wpdb->query( $wpdb->prepare(
            "DELETE FROM {$wpdb->postmeta} WHERE meta_key = %s",
            $key
        ) );
    }

    // ── Delete custom post type posts ────────────────────────────────────────
    $cpt_types = [ 'jalpi_template', 'jalpi_theme_tpl' ];
    foreach ( $cpt_types as $cpt ) {
        $posts = get_posts( [
            'post_type'      => $cpt,
            'post_status'    => 'any',
            'posts_per_page' => -1,
            'fields'         => 'ids',
            'no_found_rows'  => true,
        ] );
        foreach ( $posts as $post_id ) {
            wp_delete_post( $post_id, true );
        }
    }

    // ── Delete transients ────────────────────────────────────────────────────
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery
    $wpdb->query( $wpdb->prepare(
        "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s
         OR option_name LIKE %s
         OR option_name LIKE %s
         OR option_name LIKE %s
         OR option_name LIKE %s
         OR option_name LIKE %s",
        '_transient_jalpi_render_%',
        '_transient_timeout_jalpi_render_%',
        '_transient_jalpi_google_fonts_%',
        '_transient_timeout_jalpi_google_fonts_%',
        '_transient_jalpi_editor_tpl_list',
        '_transient_timeout_jalpi_editor_tpl_list'
    ) );
}

// ── Run cleanup ──────────────────────────────────────────────────────────────

if ( is_multisite() ) {
    $jalpi_sites = get_sites( [ 'fields' => 'ids', 'number' => 0 ] );
    foreach ( $jalpi_sites as $jalpi_site_id ) {
        switch_to_blog( $jalpi_site_id );
        jalpi_uninstall_site();
        restore_current_blog();
    }
} else {
    jalpi_uninstall_site();
}
