<?php
/**
 * Plugin Name: ChainPay SaaS License Manager
 * Plugin URI: https://chainpay.store
 * Description: Complete SaaS license and subscription management system
 * Version: 1.0.0
 * Author: ChainPay
 * Author URI: https://chainpay.store
 * Text Domain: chainpay-license
 */

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

define('CHAINPAY_LICENSE_VERSION', '1.0.0');
define('CHAINPAY_LICENSE_PATH', plugin_dir_path(__FILE__));
define('CHAINPAY_LICENSE_URL', plugin_dir_url(__FILE__));

class ChainPay_License_Manager {
    
    private static $instance = null;
    
    public static function get_instance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    private function __construct() {
        $this->load_dependencies();
        $this->init_hooks();
    }
    
    private function load_dependencies() {
        require_once CHAINPAY_LICENSE_PATH . 'includes/class-database.php';
        require_once CHAINPAY_LICENSE_PATH . 'includes/class-license.php';
        require_once CHAINPAY_LICENSE_PATH . 'includes/class-subscription.php';
        require_once CHAINPAY_LICENSE_PATH . 'includes/class-usage-tracker.php';
        require_once CHAINPAY_LICENSE_PATH . 'includes/class-api.php';
        require_once CHAINPAY_LICENSE_PATH . 'admin/class-admin.php';
    }
    
    private function init_hooks() {
        register_activation_hook(__FILE__, array($this, 'activate'));
        register_deactivation_hook(__FILE__, array($this, 'deactivate'));
        
        add_action('plugins_loaded', array($this, 'load_textdomain'));
        add_action('rest_api_init', array($this, 'register_api_endpoints'));
    }
    
    public function activate() {
        ChainPay_Database::get_instance()->create_tables();
        flush_rewrite_rules();
    }
    
    public function deactivate() {
        flush_rewrite_rules();
    }
    
    public function load_textdomain() {
        load_plugin_textdomain(
            'chainpay-license',
            false,
            dirname(plugin_basename(__FILE__)) . '/languages'
        );
    }
    
    public function register_api_endpoints() {
        $api = new ChainPay_API();
        $api->register_routes();
    }
}

function chainpay_license_manager() {
    return ChainPay_License_Manager::get_instance();
}

chainpay_license_manager();
