<?php
declare(strict_types=1);

namespace ProRank\SEO\Core;

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

/**
 * Free-package license stub.
 *
 * The wp.org build does not ship premium license activation, cloud auth,
 * or entitlement validation. This class preserves interface compatibility
 * for shared services while hard-resolving the runtime to the free tier.
 */
class LicenseManager {
    public const TIER_FREE = 'free';
    public const TIER_CORE = 'core';
    public const TIER_PRO = 'pro';
    public const TIER_BUSINESS = 'business';
    public const TIER_AGENCY = 'agency';

    private const LEGACY_OPTIONS = [
        'prorank_seo_license_key',
        'prorank_seo_license_status',
        'prorank_seo_license_jwt',
        'prorank_license_key',
        'prorank_license_status',
        'prorank_license_last_check',
        'prorank_license_grace_period',
        'prorank_license_last_sync',
    ];

    private const LEGACY_TRANSIENTS = [
        'prorank_license_cache',
        'prorank_license_error',
        'prorank_pending_reactivation',
    ];

    private ?SiteCounter $site_counter = null;
    private ?EntitlementVerifier $entitlement_verifier = null;

    public function get_tier(): string {
        return self::TIER_FREE;
    }

    public function is_tier(string $tier): bool {
        return strtolower(trim($tier)) === self::TIER_FREE;
    }

    public function is_premium(): bool {
        return false;
    }

    public function is_feature_enabled(string $feature): bool {
        return false;
    }

    public function has_feature(string $feature): bool {
        return false;
    }

    public function get_current_tier(): string {
        return self::TIER_FREE;
    }

    public function is_tier_active(string $tier): bool {
        return $this->is_tier($tier);
    }

    public function has_tier(string $tier): bool {
        return $this->is_tier($tier);
    }

    public function site_counter(): SiteCounter {
        if ($this->site_counter === null) {
            $this->site_counter = new SiteCounter($this);
        }

        return $this->site_counter;
    }

    public function entitlement_verifier(): EntitlementVerifier {
        if ($this->entitlement_verifier === null) {
            $this->entitlement_verifier = new EntitlementVerifier();
        }

        return $this->entitlement_verifier;
    }

    public function get_tier_display_name(?string $tier = null): string {
        return match ($tier ?? self::TIER_FREE) {
            self::TIER_CORE => __('Core', 'prorank-seo'),
            self::TIER_PRO => __('Pro', 'prorank-seo'),
            self::TIER_BUSINESS => __('Business', 'prorank-seo'),
            self::TIER_AGENCY => __('Agency', 'prorank-seo'),
            default => __('Free', 'prorank-seo'),
        };
    }

    public function get_license_data(): array {
        return [
            'status' => 'inactive',
            'tier' => self::TIER_FREE,
            'display_name' => $this->get_tier_display_name(self::TIER_FREE),
            'expires' => null,
            'features' => [],
            'limits' => [],
            'activated_sites' => [],
            'add_ons' => [],
            'license_token' => null,
            'license_key' => '',
            'jwt' => null,
            'has_valid' => false,
            'is_premium' => false,
        ];
    }

    public function get_license_status_snapshot(): array {
        return $this->get_license_data();
    }

    public function get_days_until_expiration(): int {
        return 0;
    }

    public function get_available_sites(): int {
        return 1;
    }

    public function is_current_site_activated(): bool {
        return true;
    }

    public function is_developer_mode(): bool {
        return false;
    }

    public function is_developer_mode_active(): bool {
        return false;
    }

    public function get_license_key(): string {
        return '';
    }

    public function activate_license(string $license_key) {
        return new \WP_Error(
            'free_package_only',
            __('License activation is not available in the free plugin.', 'prorank-seo'),
            ['status' => 403]
        );
    }

    public function force_clear_license_data() {
        foreach (self::LEGACY_OPTIONS as $option) {
            delete_option($option);
            delete_option($option . '_encrypted');
        }

        foreach (self::LEGACY_TRANSIENTS as $transient) {
            delete_transient($transient);
        }

        wp_clear_scheduled_hook('prorank_license_check');
        if (function_exists('as_unschedule_all_actions')) {
            as_unschedule_all_actions('prorank_license_refresh', [], 'prorank-seo');
        }

        return true;
    }

    public function reset_and_reactivate() {
        return new \WP_Error(
            'free_package_only',
            __('License reconnection is not available in the free plugin.', 'prorank-seo'),
            ['status' => 403]
        );
    }

    public function deactivate_license() {
        $this->force_clear_license_data();
        return true;
    }

    public function validate_license(string $license_key) {
        return new \WP_Error(
            'free_package_only',
            __('License validation is not available in the free plugin.', 'prorank-seo'),
            ['status' => 403]
        );
    }

    public function check_license_status() {
        return false;
    }

    public function refresh_license_status(): void {
    }

    public function verify_token(?string $token): bool {
        return false;
    }

    public function get_jwt_token(): ?string {
        return null;
    }

    public function has_valid_license_status(): bool {
        return false;
    }

    public function is_license_active(): bool {
        return false;
    }

    public function store_entitlement(array $entitlement): bool {
        return false;
    }

    public function get_verified_entitlement(): ?array {
        return null;
    }

    public function can_use_cloud_feature(string $feature): bool {
        return false;
    }

    public function get_entitlement_status(): array {
        return [
            'verified' => false,
            'status' => 'inactive',
            'tier' => self::TIER_FREE,
            'display_name' => $this->get_tier_display_name(self::TIER_FREE),
            'features' => [],
            'limits' => [],
        ];
    }
}
