<?php
/**
 * Postomatic Plugin - Constants file.
 *
 *  PHP Version 5.6
 *
 * @package  Postomatic
 * @category Plugin
 * @license  GPL-2.0-or-later https://www.gnu.org/licenses/gpl-2.0.html
 * @link     https://buildomatics.com/postomatics
 * @since    1.0.0
 */

namespace Postomatic;

// Define API URLs based on the current domain.
if ( ! defined( 'POSTOMATIC_API_BASE_URL' ) ) {
	$postomatic_site_url = function_exists( 'get_site_url' ) ? get_site_url() : '';

	if ( strpos( $postomatic_site_url, 'mywordpress.local' ) !== false ) {
		define( 'POSTOMATIC_API_BASE_URL', 'http://nginx_server' );
		define( 'POSTOMATIC_API_BASE_URL_FRONT', 'http://webserver' );
	} else {
		define( 'POSTOMATIC_API_BASE_URL', 'https://buildomatics.com' );
		define( 'POSTOMATIC_API_BASE_URL_FRONT', 'https://buildomatics.com' );
	}

	unset( $postomatic_site_url );
}

/**
 * Constants class.
 *
 * This class defines all the constants used in the plugin.
 *
 * @category Plugin
 * @license  GPL-2.0-or-later https://www.gnu.org/licenses/gpl-2.0.html
 * @link     https://buildomatics.com/postomatics
 * @since    1.0.0
 */
final class Constants {

	/**
	 * Plugin Name.
	 */
	public const PLUGIN_NAME = 'postomatic';

	/**
	 * Plugin Version.
	 */
	public const VERSION = POSTOMATIC_VERSION;

	/**
	 * Plugin Domain.
	 */
	public const DOMAIN = 'postomatic';

	/**
	 * Plugin Prefix.
	 */
	public const PREFIX = 'postomatic_';

	/**
	 * Plugin Page Slug.
	 */
	public const MENU_SLUG = 'postomatic';

	/**
	 * Plugin Admin Page Title.
	 */
	public const PAGE_TITLE = 'Postomatic';

	/**
	 * Plugin Admin Menu Title.
	 */
	public const MENU_TITLE = 'Postomatic';

	/**
	 * Capability Required to access the plugin admin settings.
	 */
	public const CAPABILITY = 'manage_options';

	/**
	 * Icon URL for the plugin admin menu.
	 */
	public const ICON_URL = 'dashicons-schedule';

	/**
	 * Position of the plugin admin menu.
	 */
	public const POSITION = 65;

	/**
	 * Custom post type name.
	 */
	public const POST_TYPE = 'postomatic';

	public const PLUGIN_DIR      = POSTOMATIC_PLUGIN_DIR;
	public const PLUGIN_URL      = POSTOMATIC_PLUGIN_URL;
	public const PLUGIN_BASENAME = POSTOMATIC_PLUGIN_BASENAME;


	// WordPress options.
	public const OPTION_CODE                       = self::PLUGIN_NAME . '_secure_code';
	/**
	 * Holds the credentials for an activation attempt that has not been
	 * confirmed yet.
	 *
	 * The service stages re-activations rather than applying them, and only
	 * promotes them once it has completed the inbound /cr handshake against this
	 * site. Until that happens the previous live credentials must keep working,
	 * so a new code and WP API secret are parked here instead of overwriting
	 * OPTION_CODE / OPTION_API_WP_SECURE_KEY.
	 */
	public const OPTION_PENDING_ACTIVATION         = self::PLUGIN_NAME . '_pending_activation';
	public const OPTION_API_SERVICE_KEY            = self::PLUGIN_NAME . '_api_service_key';
	public const OPTION_API_WP_SECURE_KEY          = self::PLUGIN_NAME . '_wp_api_secure_key';
	public const OPTION_API_IS_VALIDATING          = self::PLUGIN_NAME . '_is_validating_key';
	public const OPTION_API_PER_POST_ONE_TIME_CODE
		= self::PLUGIN_NAME . '_one_time_codes';

	// Database tables.
	public const TABLE_TITLES = self::PLUGIN_NAME . '_titles';

	// WordPress API endpoints.
	public const API_WP_ENDPOINT_BASE               = self::PLUGIN_NAME . '/v1';
	public const API_WP_ENDPOINT_CODE               = '/cr';
	public const API_WP_ENDPOINT_POST_CREATOR       = '/pm';
	public const API_WP_ENDPOINT_AUTOPILOT          = '/autopilots';
	public const API_WP_ENDPOINT_CONTENT_GOAL_TYPES = '/autopilot/content-goal-types';

	// External API configurations.
	public const API_BASE_URL       = POSTOMATIC_API_BASE_URL;  // For php connection to API.
	public const API_BASE_URL_FRONT = POSTOMATIC_API_BASE_URL_FRONT; // For api requests.
	public const API_ENDPOINT_URL   = self::API_BASE_URL_FRONT . '/api/services/postomatic'; // API endpoint URL.

	// Add this for clarity (used in the template above).
	public const API_AUTOPILOT_EXTERNAL            = self::API_ENDPOINT_URL . '/autopilots';
	public const API_BLOG_ANALYSIS_EXTERNAL        = self::API_BASE_URL . '/api/services/postomatic/blog/analyze-titles'; // API endpoint for blog analysis.
	public const API_BLOG_AUTOPILOT_GOALS_EXTERNAL = self::API_BASE_URL . '/api/services/postomatic/autopilot/content-goal-types'; // API endpoint for autopilot goals.

	// API Endpoint Groups - Authentication & Initialization.
	public const API_RE_ACTIVE = self::API_ENDPOINT_URL . '/initial-check';
	public const API_AGREEMENT = self::API_ENDPOINT_URL . '/agreement';

	// API Endpoint Group - Post Management.
	public const API_POST_MAKER        = self::API_ENDPOINT_URL . '/posts/maker';
	public const API_POST_FETCHER      = self::API_ENDPOINT_URL . '/posts/fetcher';
	public const API_POSTS_RECENT_LIST = self::API_ENDPOINT_URL . '/posts/list/recent';
	public const API_POSTS_DELETE      = self::API_ENDPOINT_URL . '/posts/%postid%';

	/**
	 * Re-queues a single cancelled post for generation.
	 *
	 * Cancelling a post refunds its credits, so putting it back into the queue
	 * has to debit them again. The amount is decided by the service, not here.
	 */
	public const API_POSTS_REGENERATE = self::API_ENDPOINT_URL . '/posts/%postid%/regenerate';

	/**
	 * Re-queues every cancelled post for generation in one call.
	 */
	public const API_POSTS_REGENERATE_ALL = self::API_ENDPOINT_URL . '/posts/regenerate-cancelled';

	// API Endpoint Group - Support.
	public const API_SUPPORTS_GET    = self::API_ENDPOINT_URL . '/support';
	public const API_SUPPORTS_POST   = self::API_ENDPOINT_URL . '/support';
	public const API_SUPPORTS_SINGLE = self::API_ENDPOINT_URL . '/support/%supportid%';

	// API Endpoint Group - Pricing and Subscription.
	public const API_PRICING_PLANS                       = self::API_ENDPOINT_URL . '/pricing/plans/list';
	public const API_PRICING_CREDITS                     = self::API_ENDPOINT_URL . '/pricing/credits/list';
	public const API_CREDIT_ACCESS_CREDIT_AMOUNT_DETAILS = self::API_ENDPOINT_URL . '/plan/accesses';
	public const API_CREDITS_SCORES_LIST                 = self::API_ENDPOINT_URL . '/pricing/credits/scores/list';
	public const API_PLAN_ACTIVE                         = self::API_ENDPOINT_URL . '/plan/active';

	// API Endpoint Group - Reference Data.
	public const API_LANGUAGES_LIST = self::API_ENDPOINT_URL . '/meta-data/languages/list';
	public const API_COUNTRIES_LIST = self::API_ENDPOINT_URL . '/meta-data/countries/list';
	public const API_SPEAKERS_LIST  = self::API_ENDPOINT_URL . '/meta-data/speakers/list';

	// Admin Pages.
	public const PAGE_MAIN       = 'postomatic';
	public const PAGE_MAIN_TITLE = 'Add Post';
	public const PAGE_SETTINGS   = 'settings';

	public const PAGE_SETTINGS_TITLE   = 'Settings';
	public const PAGE_MEMBERSHIP       = 'postomatic-membership';
	public const PAGE_MEMBERSHIP_TITLE = 'Subscription';
	public const PAGE_ADD_CREDIT       = 'postomatic-add-credit';
	public const PAGE_ADD_CREDIT_TITLE = 'Buy Credit';

	public const PAGE_SUPPORT       = 'postomatic-support';
	public const PAGE_SUPPORT_TITLE = 'Support';

	public const PAGE_AUTOPILOT       = 'postomatic-autopilot';
	public const PAGE_AUTOPILOT_TITLE = 'AutoPilot';

	/**
	 * Subscription plan slugs, ordered from lowest to highest tier.
	 *
	 * This is the single source of truth for plan ranking. It used to be
	 * duplicated as literal `=== 'pro-ultimate'` comparisons across four JS
	 * files and two templates, which meant that adding a tier above Pro
	 * Ultimate would have kept showing "Upgrade" to users already on the top
	 * plan. Anything that needs to know whether a plan can still be upgraded
	 * should go through Utils::is_highest_plan() / Utils::get_plan_rank().
	 *
	 * 'free' is treated as the implicit bottom tier and is intentionally not
	 * listed, since the API reports it for users without any subscription.
	 */
	public const PLAN_TIERS = array( 'starter', 'basic', 'pro-ultimate' );

	/**
	 * Lowercase phrases that mark an API notice as an upgrade pitch.
	 *
	 * The "important notice" shown on the Buy Credit and Subscription screens is
	 * authored remotely and delivered by the API, so the plugin cannot know its
	 * intent from a type flag. Notices that match any of these phrases are
	 * treated as a call to buy a higher plan and are hidden for users who are
	 * already on the top tier. Notices that do not match (renewal reminders,
	 * service announcements, and similar) are always shown.
	 */
	public const UPGRADE_NOTICE_KEYWORDS = array(
		'upgrad', // Deliberately truncated so it also matches "upgrade" / "upgrading" / "upgrades".
		'unlock more',
		'higher plan',
		'better plan',
	);
}
