<?php

/**
 * Plugin Name: Gu&Co AI Blog Writer
 * Contributors: @krzysztofsarna
 * Description: AI agent for generating and managing WordPress blog post drafts.
 * Version: 1.1.2
 * Author: Krzysztof Sarna
 * Requires at least: 6.4
 * Requires PHP: 8.1
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: guco-ai-blog-writer
 *
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
if ( !function_exists( 'aisbw_fs' ) ) {
    function aisbw_fs() {
        global $aisbw_fs;
        if ( !isset( $aisbw_fs ) ) {
            $start_file = dirname( __FILE__ ) . '/vendor/freemius/start.php';
            if ( file_exists( $start_file ) ) {
                require_once $start_file;
                $aisbw_fs = fs_dynamic_init( array(
                    'id'               => '34396',
                    'slug'             => 'guco-ai-blog-writer',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_c82540976dca8b88897efaa314594',
                    'is_premium'       => false,
                    'has_addons'       => false,
                    'has_paid_plans'   => true,
                    'is_org_compliant' => true,
                    'menu'             => array(
                        'support' => false,
                    ),
                    'is_live'          => true,
                ) );
            } else {
                // Mock class for local compatibility when SDK is missing
                if ( !class_exists( 'AISBW_Freemius_Mock' ) ) {
                    class AISBW_Freemius_Mock {
                        public function is_premium() {
                            return (bool) apply_filters( 'aisbw_is_premium', false );
                        }

                        public function can_use_premium_code() {
                            return (bool) apply_filters( 'aisbw_is_premium', false );
                        }

                        public function is_paying() {
                            return (bool) apply_filters( 'aisbw_is_premium', false );
                        }

                        public function is_paying_or_trial() {
                            return (bool) apply_filters( 'aisbw_is_premium', false );
                        }

                        public function is_not_paying() {
                            return !$this->can_use_premium_code();
                        }

                        public function is_free_plan() {
                            return !$this->can_use_premium_code();
                        }

                        public function is_plan( $plan, $exact = false ) {
                            return (bool) apply_filters( 'aisbw_is_premium', false );
                        }

                        public function is_plan_or_trial( $plan, $exact = false ) {
                            return (bool) apply_filters( 'aisbw_is_premium', false );
                        }

                        public function get_upgrade_url() {
                            return '#upgrade-to-pro';
                        }

                        public function checkout_url() {
                            return '#checkout';
                        }

                        public function set_basename( $set, $file ) {
                        }

                        public function add_action(
                            $hook,
                            $callback,
                            $priority = 10,
                            $accepted_args = 1
                        ) {
                        }

                    }

                }
                $aisbw_fs = new AISBW_Freemius_Mock();
            }
        }
        return $aisbw_fs;
    }

    aisbw_fs();
    aisbw_fs()->set_basename( false, __FILE__ );
    if ( file_exists( dirname( __FILE__ ) . '/vendor/freemius/start.php' ) ) {
        do_action( 'aisbw_fs_loaded' );
        aisbw_fs()->add_action( 'after_uninstall', 'aisbw_fs_uninstall_cleanup' );
    }
}
define( 'AISBW_PLUGIN_FILE', __FILE__ );
define( 'AISBW_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'AISBW_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'AISBW_VERSION', '1.1.1' );
/**
 * Checks if the plugin has premium eligibility.
 */
function aisbw_is_premium() : bool {
    return aisbw_fs()->can_use_premium_code();
}

/**
 * Cleanup database and options on plugin uninstall.
 */
function aisbw_fs_uninstall_cleanup() {
    global $wpdb;
    // 1. Delete database tables
    $tables = [$wpdb->prefix . 'aisbw_topics', $wpdb->prefix . 'aisbw_logs', $wpdb->prefix . 'aisbw_runs'];
    foreach ( $tables as $table ) {
        $wpdb->query( "DROP TABLE IF EXISTS {$table}" );
    }
    // 2. Delete settings options
    $options = [
        'aisbw_default_article_length',
        'aisbw_default_tone',
        'aisbw_default_language',
        'aisbw_default_category_id',
        'aisbw_default_author_id',
        'aisbw_test_mode_enabled',
        'aisbw_autopublish_enabled',
        'aisbw_min_quality_score',
        'aisbw_cron_enabled',
        'aisbw_max_articles_per_day',
        'aisbw_max_ai_requests_per_day'
    ];
    foreach ( $options as $option ) {
        delete_option( $option );
    }
    // 3. Remove administrator capabilities
    $role = get_role( 'administrator' );
    if ( $role ) {
        $role->remove_cap( 'manage_ai_smartblog_writer' );
    }
}

require_once AISBW_PLUGIN_DIR . 'includes/class-plugin.php';
register_activation_hook( __FILE__, ['AISBW_Plugin', 'activate'] );
register_deactivation_hook( __FILE__, ['AISBW_Plugin', 'deactivate'] );
add_action( 'plugins_loaded', static function () : void {
    AISBW_Plugin::instance()->init();
} );