<?php
/**
 * Alamin Vendor Tasks for Dokan with ClickUp
 *
 * @package           VendorTasksDokanClickUp
 * @author            Al Amin Ahamed
 * @copyright         2025 Al Amin Ahamed
 * @license           GPL-2.0-or-later
 *
 * @wordpress-plugin
 * Plugin Name:       Vendor Tasks for Dokan & ClickUp – Marketplace Automation
 * Plugin URI:        https://thewpsquad.com/plugin/vendor-tasks-dokan-clickup
 * Description:       Bridge your Dokan marketplace and ClickUp. Auto-creates tasks on vendor registration, tracks onboarding milestones, and responds to lifecycle events — all without leaving ClickUp.
 * Version:           1.1.0
 * Requires at least: 6.6
 * Requires PHP:      7.4
 * Author:            Al Amin Ahamed
 * Author URI:        https://alaminahamed.com
 * Text Domain:       alamin-vendor-tasks-dokan-clickup
 * License:           GPL v2 or later
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Requires Plugins:  woocommerce, dokan-lite
 *
 * WC requires at least: 3.0
 * WC tested up to:     9.5
 * Dokan requires at least: 3.0
 * Dokan tested up to:     3.15
 */

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

define( 'VENDOR_TASKS_DOKAN_CLICKUP_VERSION', '1.1.0' );
define( 'VENDOR_TASKS_DOKAN_CLICKUP_FILE', __FILE__ );
define( 'VENDOR_TASKS_DOKAN_CLICKUP_URL', plugin_dir_url( __FILE__ ) );
define( 'VENDOR_TASKS_DOKAN_CLICKUP_PATH', plugin_dir_path( __FILE__ ) );
define( 'VENDOR_TASKS_DOKAN_CLICKUP_DIR', __DIR__ );

/* ── Freemius SDK ──────────────────────────────────────────────────────── */

if ( ! function_exists( 'vendor_tasks_dokan_clickup_freemius' ) ) {
	/**
	 * Return the Freemius SDK instance for this plugin.
	 *
	 * Initialises on first call. Must run at plugin load time (not inside a hook)
	 * so premium add-ons can hook into `vtdc_freemius_loaded` before plugins_loaded.
	 * Returns null when freemius/start.php is not present — callers must null-check.
	 *
	 * SDK setup:
	 *   1. Copy freemius/wordpress-sdk to <plugin-root>/freemius/
	 *   2. Define VTDC_FS_PRODUCT_ID + VTDC_FS_PUBLIC_KEY in wp-config.php (or edit
	 *      the defaults below with values from https://dashboard.freemius.com).
	 *
	 * @since 1.5.0
	 * @return mixed \Freemius|null
	 */
	function vendor_tasks_dokan_clickup_freemius() {
		global $vendor_tasks_dokan_clickup_fs;

		if ( ! isset( $vendor_tasks_dokan_clickup_fs ) ) {
			$sdk_path = VENDOR_TASKS_DOKAN_CLICKUP_PATH . 'freemius/start.php';

			if ( ! file_exists( $sdk_path ) ) {
				$vendor_tasks_dokan_clickup_fs = null;
				return null;
			}

			require_once $sdk_path; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable

			try {
				$vendor_tasks_dokan_clickup_fs = fs_dynamic_init( // phpstan-ignore-next-line
					array(
						'id'                   => '31959',
						'slug'                 => 'alamin-vendor-tasks-dokan-clickup',
						'premium_slug'         => 'alamin-vendor-tasks-dokan-clickup-pro',
						'type'                 => 'plugin',
						'public_key'           => 'pk_e49188f249c35021896b2b2a656fa',
						'is_premium'           => false,
						'has_addons'           => false,
						'has_paid_plans'       => true,
						'trial_can_be_skipped' => true,
						'parallel_activation'  => array(
							'enabled'                  => true,
							'premium_version_basename' => 'alamin-vendor-tasks-dokan-clickup-pro/alamin-vendor-tasks-dokan-clickup-pro.php',
						),
						'menu'                 => array(
							'slug'    => 'vendor-tasks-dokan-clickup',
							'support' => false,
						),
					)
				);
			} catch ( Exception $e ) {
				$vendor_tasks_dokan_clickup_fs = null;
			}
		}

		return $vendor_tasks_dokan_clickup_fs;
	}

	vendor_tasks_dokan_clickup_freemius();
	do_action( 'vendor_tasks_dokan_clickup_freemius_loaded' );

	/**
	 * Supply the plugin icon shown on the Freemius opt-in / connect page.
	 * Filter: fs_plugin_icon_{slug}  — expects an absolute filesystem path.
	 * Place assets/images/icon-128.png (128×128 or larger) to change the icon.
	 */
	add_filter(
		'fs_plugin_icon_alamin-vendor-tasks-dokan-clickup',
		static function () {
			$icon = VENDOR_TASKS_DOKAN_CLICKUP_PATH . 'assets/images/icon-128.png';
			return file_exists( $icon ) ? $icon : false;
		}
	);
}

/* ── Autoloader ────────────────────────────────────────────────────────── */

if ( ! file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
	return;
}

require_once __DIR__ . '/vendor/autoload.php';

/**
 * Get main plugin instance
 *
 * @since 1.0.0
 * @return Vendor_Tasks_Dokan_ClickUp Plugin instance.
 */
function vendor_tasks_dokan_clickup(): Vendor_Tasks_Dokan_ClickUp {
	return Vendor_Tasks_Dokan_ClickUp::get_instance();
}

vendor_tasks_dokan_clickup()->init();
