<?php

/**
 * Plugin Name: Preciousuite Metal Rate Charts
 * Description: Display custom metal rate charts with a modern UI. Not affiliated with any other service.
 * Version: 2.4
 * Author: Vimal Kava
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: preciousuite-metal-rate-charts
 */

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

// Strong unique prefix
define('PRECIOUSUITE_MRC_VERSION', '1.1');
define('PRECIOUSUITE_MRC_PATH', plugin_dir_path(__FILE__));
define('PRECIOUSUITE_MRC_FILE', __FILE__);
define('PMRCGOLD_PATH', PRECIOUSUITE_MRC_PATH);
define('PRECIOUSUITE_MRC_URL', plugin_dir_url(__FILE__));
define('PMRCGOLD_URL', PRECIOUSUITE_MRC_URL);
define('PMRCGOLD_ASSETS_URL', PRECIOUSUITE_MRC_URL . 'public/');

require PRECIOUSUITE_MRC_PATH . 'includes/plugin.php';
require PRECIOUSUITE_MRC_PATH . 'includes/deactivator.php';

if (is_admin()) {
    require_once PRECIOUSUITE_MRC_PATH . 'includes/precioussuite/class-precioussuite-registry.php';
    require_once PRECIOUSUITE_MRC_PATH . 'includes/precioussuite/class-precioussuite-admin.php';

    PreciousSuite_Admin::init(array(
        'version' => '1.1.11',
        'base_path' => PRECIOUSUITE_MRC_PATH . 'includes/precioussuite/',
        'base_url' => PRECIOUSUITE_MRC_URL . 'includes/precioussuite/',
    ));

    add_action('init', 'pmrcgold_register_precioussuite_module');
}

function pmrcgold_register_precioussuite_module()
{
    pt_gold_register_module(array(
        'slug' => 'gold-price-widgets',
        'name' => __('Metal Rate Charts', 'preciousuite-metal-rate-charts'),
        'label' => __('Widgets & API', 'preciousuite-metal-rate-charts'),
        'description' => __('Display custom metal rate charts with a modern UI.', 'preciousuite-metal-rate-charts'),
        'settings_url' => admin_url('admin.php?page=gold-price-widgets'),
        'plugin_file' => plugin_basename(__FILE__),
        'wporg_slug' => 'preciousuite-metal-rate-charts',
        'position' => 15,
        'stats' => '',
    ));
}

// Settings link filter with unique function name
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'pmrcgold_settings_link');

if (!function_exists('pmrcgold_settings_link')) {
    function pmrcgold_settings_link($links)
    {
        $settings_link = '<a href="admin.php?page=gold-price-widgets">' . __('Settings', 'preciousuite-metal-rate-charts') . '</a>';
        array_unshift($links, $settings_link);
        return $links;
    }
}

register_activation_hook(__FILE__, function () {
    // Load api-settings.php so constants and functions are available.
    require_once PRECIOUSUITE_MRC_PATH . 'includes/api-settings.php';

    // Setup daily cron.
    pmrcgold_setup_daily_cron();

    // Save API key and fetch metal prices on first activation.
    pmrcgold_activation_fetch();
});

register_deactivation_hook(__FILE__, function () {
    // Load api-settings.php so constants and functions are available.
    require_once PRECIOUSUITE_MRC_PATH . 'includes/api-settings.php';

    // Clear daily cron
    pmrcgold_clear_daily_cron();
});
