<?php
/*
Plugin Name: Simple Amazon Affiliate
Plugin URI: https://slurpywp.slurpystudios.com/wordpress-plugins/simple-amazon-affiliate
Description: Adds affiliate product boxes to posts for Amazon products without ASINs; supports manual input and optional Amazon PA-API integration. Uses shortcodes for easy insertion.
Version: 1.0
Author: SlurpyWP
Author URI: https://slurpywp.slurpystudios.com
Text Domain: simple-amazon-affiliate
Domain Path: /languages
Requires at least: 6.5
Tested up to: 6.6
Requires PHP: 8.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Constants
 */
define('SAA_PLUGIN_VERSION', '1.0');
define('SAA_PLUGIN_FILE', __FILE__);
define('SAA_PLUGIN_BASENAME', plugin_basename(__FILE__));
define('SAA_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('SAA_PLUGIN_URL', plugin_dir_url(__FILE__));

define('SAA_FRONTEND_DIR', SAA_PLUGIN_DIR . 'frontend/');
define('SAA_ADMIN_DIR', SAA_PLUGIN_DIR . 'admin/');
define('SAA_METABOX_DIR', SAA_PLUGIN_DIR . 'metabox/');
define('SAA_API_DIR', SAA_PLUGIN_DIR . 'api/');
define('SAA_CORE_DIR', SAA_PLUGIN_DIR . 'core/');

/**
 * Autoloader (simple PSR-4–ish for our classes)
 * Looks for files like class-saa-*.php in subfolders.
 */
spl_autoload_register(function ($class) {
    if (strpos($class, 'SAA_') !== 0) {
        return;
    }

    $map = [
        'SAA_Plugin'   => SAA_CORE_DIR . 'class-saa-plugin.php',
        'SAA_Frontend' => SAA_FRONTEND_DIR . 'class-saa-frontend.php',
        'SAA_Admin'    => SAA_ADMIN_DIR . 'class-saa-admin.php',
        'SAA_Metabox'  => SAA_METABOX_DIR . 'class-saa-metabox.php',
        'SAA_API'      => SAA_API_DIR . 'class-saa-api.php',
    ];

    if (isset($map[$class]) && file_exists($map[$class])) {
        require_once $map[$class];
    }
});

/**
 * Load translations
 */
function saa_load_textdomain() {
    load_plugin_textdomain('slurpywp-saa-lang', false, dirname(SAA_PLUGIN_BASENAME) . '/languages');
}
add_action('plugins_loaded', 'saa_load_textdomain');

/**
 * Activation: set safe defaults
 */
function saa_activate() {
    $defaults = [
        'associate_tag'     => '',
        'access_key'        => '',
        'secret_key'        => '',
        'region'            => '', // '', UK, US, CA, DE, FR, IT, ES, JP, IN
        'delete_on_uninstall' => 0,
    ];

    $existing = get_option('saa_options', []);
    if (!is_array($existing)) {
        $existing = [];
    }

    update_option('saa_options', array_merge($defaults, $existing));
}
register_activation_hook(__FILE__, 'saa_activate');

/**
 * Deactivation: no data removal (respect uninstall option)
 */
function saa_deactivate() {
    // Intentionally empty. Cleanup handled by uninstall.php if user opted in.
}
register_deactivation_hook(__FILE__, 'saa_deactivate');

/**
 * Bootstrap the plugin
 */
function saa_init_plugin() {
    // Ensure core file exists (dev safety)
    if (!class_exists('SAA_Plugin')) {
        require_once SAA_CORE_DIR . 'class-saa-plugin.php';
    }

    $plugin = new SAA_Plugin();
    $plugin->run();
}
add_action('plugins_loaded', 'saa_init_plugin', 20);

/**
 * Admin action links (Settings link)
 */
function saa_action_links($links) {
    $settings_url = admin_url('options-general.php?page=saa-settings');
    $links[] = '<a href="' . esc_url($settings_url) . '">' . esc_html__('Settings', 'simple-amazon-affiliate') . '</a>';
    return $links;
}
add_filter('plugin_action_links_' . SAA_PLUGIN_BASENAME, 'saa_action_links');
