<?php

/**
 * Plugin Name: PloverCRM
 * Description: A powerful CRM plugin for WordPress with mobile app integration
 * Version: 2.1.0
 * Update URI: https://plovercrm.com/plover-crm/
 * Author: WebPlover
 * Author URI: https://webplover.com
 */

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

// Require Composer autoloader
require_once __DIR__ . '/vendor/autoload.php';

use PloverCRM\Core\Plugin;

/**
 * Initialize the plugin
 * 
 * @return void
 */
function plover_crm_init()
{
    // Define constants before bootstrapping plugin services.
    if (!defined('PLOVER_CRM_PLUGIN_PATH')) {
        define('PLOVER_CRM_PLUGIN_PATH', plugin_dir_path(__FILE__));
    }

    if (!defined('PLOVER_CRM_PLUGIN_URL')) {
        define('PLOVER_CRM_PLUGIN_URL', plugin_dir_url(__FILE__));
    }

    if (!defined('PLOVER_CRM_PLUGIN_VERSION')) {
        define('PLOVER_CRM_PLUGIN_VERSION', '2.1.0');
    }

    // Initialize Common classes
    \PloverCRM\Common\Hooks::init();
    \PloverCRM\Common\Compatibility::init();
    \PloverCRM\Common\ActivityEventManager::init();
    \PloverCRM\Common\CrmUserManager::init();
    \PloverCRM\Common\RoleManager::init();
    \PloverCRM\Common\StandalonePasswordManager::init();

    // License system
    \PloverCRM\Common\LicensePage::init();
    \PloverCRM\Common\LicenseCron::init();
    \PloverCRM\Common\UpdateChecker::init();

    // Get plugin instance and initialize
    $plugin = Plugin::getInstance();
    $plugin->init(__FILE__, '2.1.0');
}

// Initialize plugin
plover_crm_init();

/**
 * Plugin activation hook
 * 
 * @return void
 */
function plover_crm_activate()
{
    try {
        $container = \PloverCRM\Core\Plugin::getInstance()->getContainer();
        $provisioner = new \PloverCRM\Core\Services\SystemFieldProvisioner($container);
        $provisioner->syncCoreFields();
    } catch (\Exception $e) {
        // CRM not initialized yet, skip custom field setup
    }

    // Initialize default stage colors
    $default_colors = [
        'New' => '#2196f3',
        'Contacted' => '#4caf50',
        'On Trial' => '#03a9f4',
        'On Hold' => '#ff9800',
        'At Risk' => '#f44336',
        'Unpaid' => '#ff5722',
        'Paid' => '#4caf50',
        'Lost' => '#9e9e9e'
    ];

    if (!get_option('plover_crm_stage_colors')) {
        update_option('plover_crm_stage_colors', $default_colors);
    }

    // Modify IP column type (only if FluentCRM table exists)
    plover_crm_modify_ip_column_type();
}

register_activation_hook(__FILE__, 'plover_crm_activate');

/**
 * Ensure FluentCRM subscriber IP column supports IPv6 addresses.
 * 
 * @return void
 */
function plover_crm_modify_ip_column_type()
{
    global $wpdb;
    $table_name = $wpdb->prefix . 'fc_subscribers';

    if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name) {
        $sql = "ALTER TABLE `$table_name` MODIFY COLUMN `ip` VARCHAR(45)";
        $wpdb->query($sql);
    }
}

add_action('init', function () {
    \PloverCRM\Migrations\WebPushMigration::run();
});
