<?php
/**
 * Plugin Activator
 *
 * @package PackshotPhotoAI
 */

namespace PackshotPhotoAI\Core;

defined('ABSPATH') || exit;

/**
 * Runs on plugin activation
 */
class Activator {

    /**
     * Activate plugin
     */
    public static function activate(): void {
        self::migrate_options();

        $installed_version = get_option('packshotphoto_version');

        // Fresh install or migration
        if (!$installed_version) {
            self::set_defaults();
        } elseif (version_compare($installed_version, PACKSHOTPHOTO_AI_VERSION, '<')) {
            // Future migration logic can go here
            update_option('packshotphoto_version', PACKSHOTPHOTO_AI_VERSION);
        }

        update_option('packshotphoto_version', PACKSHOTPHOTO_AI_VERSION);
        flush_rewrite_rules();

        // Schedule weekly keep-alive to prevent Supabase project from pausing
        if (!wp_next_scheduled('packshotphoto_keep_alive_event')) {
            wp_schedule_event(time(), 'weekly', 'packshotphoto_keep_alive_event');
        }
    }

    /**
     * Migrate old PMM options to new packshotphoto_ options
     */
    private static function migrate_options(): void {
        $mapping = [
            'pmm_settings'                => 'packshotphoto_settings',
            'pmm_api_key'                 => 'packshotphoto_api_key',
            'pmm_site_context'            => 'packshotphoto_site_context',
            'pmm_max_image_size'          => 'packshotphoto_max_image_size',
            'pmm_delete_mode'             => 'packshotphoto_delete_mode',
            'pmm_license_key'             => 'packshotphoto_license_key',
            'pmm_connection_type'         => 'packshotphoto_connection_type',
            'pmm_dev_api_endpoint'        => 'packshotphoto_dev_api_endpoint',
            'pmm_dev_api_key'             => 'packshotphoto_dev_api_key',
            'pmm_dev_model'               => 'packshotphoto_dev_model',
            'pmm_stripe_webhook_secret'   => 'packshotphoto_stripe_webhook_secret',
            'pmm_custom_prompt'           => 'packshotphoto_custom_prompt',
            'pmm_auto_process'            => 'packshotphoto_auto_process',
            'pmm_auto_webp'               => 'packshotphoto_auto_webp',
            'pmm_auto_avif'               => 'packshotphoto_auto_avif',
            'pmm_auto_rename'             => 'packshotphoto_auto_rename',
            'pmm_enable_logging'          => 'packshotphoto_enable_logging',
            'pmm_debug_logging'           => 'packshotphoto_debug_logging',
            'pmm_credits_balance'         => 'packshotphoto_credits_balance',
            'pmm_free_credits_granted'    => 'packshotphoto_free_credits_granted',
        ];

        foreach ($mapping as $old_key => $new_key) {
            $old_value = get_option($old_key);
            if (false !== $old_value && false === get_option($new_key)) {
                update_option($new_key, $old_value);
            }
        }
    }

    /**
     * Set default options
     */
    public static function set_defaults(): void {
        if (false === get_option('packshotphoto_settings')) {
            update_option('packshotphoto_settings', [
                'model'     => 'gemini-2.0-flash',
                'language'  => 'auto',
                'sync_slug' => '1',
            ]);
        }

        if (false === get_option('packshotphoto_free_credits_granted')) {
            update_option('packshotphoto_free_credits_granted', true);
            $current_balance = (int) get_option('packshotphoto_credits_balance', 0);
            update_option('packshotphoto_credits_balance', $current_balance + 10);
        } elseif (false === get_option('packshotphoto_credits_balance')) {
            update_option('packshotphoto_credits_balance', 10);
        }

        if (false === get_option('packshotphoto_connection_type')) {
            update_option('packshotphoto_connection_type', 'our_service');
        }

        if (false === get_option('packshotphoto_auto_process')) {
            update_option('packshotphoto_auto_process', '1');
        }

        if (false === get_option('packshotphoto_auto_webp')) {
            update_option('packshotphoto_auto_webp', '1');
        }

        if (false === get_option('packshotphoto_auto_avif')) {
            update_option('packshotphoto_auto_avif', '0');
        }

        if (false === get_option('packshotphoto_auto_rename')) {
            update_option('packshotphoto_auto_rename', '1');
        }
    }

    /**
     * Reset all plugin settings to factory defaults
     */
    public static function reset_plugin(): void {
        $options = [
            'packshotphoto_settings',
            'packshotphoto_api_key',
            'packshotphoto_site_context',
            'packshotphoto_max_image_size',
            'packshotphoto_delete_mode',
            'packshotphoto_license_key',
            'packshotphoto_connection_type',
            'packshotphoto_dev_api_endpoint',
            'packshotphoto_dev_api_key',
            'packshotphoto_dev_model',
            'packshotphoto_stripe_webhook_secret',
            'packshotphoto_custom_prompt',
            'packshotphoto_auto_process',
            'packshotphoto_auto_webp',
            'packshotphoto_auto_rename',
            'packshotphoto_enable_logging',
            'packshotphoto_debug_logging',
            'packshotphoto_credits_balance',
            'packshotphoto_activated_url',
            'packshotphoto_version',
            'packshotphoto_recent_openers',
        ];

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

        self::activate();
    }
}
