<?php
/**
 * Fired when the plugin is uninstalled.
 *
 * @package Integrate_Palace_Properties
 */

// If uninstall not called from WordPress, exit.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
    exit;
}

// Only run if the user has explicitly chosen to remove data.
$remove_data = get_option( 'ipp_remove_data_on_uninstall', 'no' );
if ( $remove_data !== 'yes' ) {
    return;
}

global $wpdb;

// 1. Delete all palace-properties posts and their meta
$posts = get_posts( array(
    'post_type'      => 'palace-properties',
    'posts_per_page' => -1,
    'post_status'    => 'any',
    'fields'         => 'ids',
) );

foreach ( $posts as $post_id ) {
    // Delete gallery attachments
    $gallery = get_post_meta( $post_id, 'integrate_palace_gallery', true );
    if ( is_array( $gallery ) ) {
        foreach ( $gallery as $attachment_id ) {
            wp_delete_attachment( intval( $attachment_id ), true );
        }
    }
    // Delete featured image
    $thumb_id = get_post_thumbnail_id( $post_id );
    if ( $thumb_id ) {
        wp_delete_attachment( $thumb_id, true );
    }
    wp_delete_post( $post_id, true );
}

// 2. Delete taxonomy terms
$taxonomies = array(
    'palace-property-type',
    'palace-property-status',
    'palace-property-region',
    'palace-property-management',
);
foreach ( $taxonomies as $taxonomy ) {
    $terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => false, 'fields' => 'ids' ) );
    if ( ! is_wp_error( $terms ) ) {
        foreach ( $terms as $term_id ) {
            wp_delete_term( $term_id, $taxonomy );
        }
    }
}

// 3. Delete all plugin options
$options_to_delete = array(
    'integrate_palace_properties_username',
    'integrate_palace_properties_password',
    'integrate_palace_properties_subscription_key',
    'integrate_palace_properties_slug',
    'integrate_palace_properties_webhook_status',
    'integrate_palace_properties_webhook_url',
    'integrate_palace-palace-unique-id',
    'integrate_palace_webhook_secret',
    'integrate_palace_select_template',
    'ipp_select_template',
    'ipp_display_fields',
    'ipp_sync_archived',
    'ipp_sync_statuses',
    'ipp_last_sync_time',
    'ipp_last_sync_count',
    'ipp_archive_columns',
    'ipp_remove_data_on_uninstall',
    'ipp_settings',
    IPPR_SETTINGS ?? 'ippr-template-settings',
);

foreach ( $options_to_delete as $option ) {
    delete_option( $option );
}

// 4. Delete orphaned post meta with ipp_ prefix
$wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE 'ipp\_%'" );
$wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE 'information\_%'" );
$wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE 'accommodation\_%'" );
$wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE 'property\_%'" );
$wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE 'agent\_%'" );
$wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE meta_key = 'integrate_palace_gallery'" );

// 5. Clear scheduled events
wp_clear_scheduled_hook( 'update_integrate_palace_gallery' );
wp_clear_scheduled_hook( 'integrate_palace_update_gallery' ); // legacy misnamed hook
wp_clear_scheduled_hook( 'ipp_process_gallery_image' ); // legacy Phase 2 hook

// 6. Clean up transients
delete_transient( 'ipp_gallery_api_last_call' );

// 6. Flush rewrite rules
flush_rewrite_rules();
