<?php

namespace MovieEngine\Payments;

if (! defined("ABSPATH")) {
    exit; // Exit if accessed directly
}

/**
 * Membership Interface (Lite)
 * 
 * Provides basic membership utility methods and hooks for Pro version to implement
 * full subscription and paywall logic.
 */
class Membership {

    public function __construct() {
        // Base version doesn't register AJAX handlers for plans
    }

    /**
     * Check if a user is premium
     * Admins are always premium in Lite. Extensions can override via filter.
     */
    public static function is_premium($user_id = null, $skip_admin_check = false) {
        if (!$user_id) {
            $user_id = get_current_user_id();
        }

        if (!$user_id) {
            return false;
        }

        $user = get_userdata($user_id);
        if (!$user) {
            return false;
        }

        if (!$skip_admin_check && in_array('administrator', $user->roles)) {
            return true;
        }

        return apply_filters('movie_engine_is_premium', false, $user_id, $skip_admin_check);
    }

    /**
     * Helper to check for active membership
     */
    public static function has_active_membership($user_id = null) {
        return self::is_premium($user_id);
    }

    /**
     * Get Redirect URLs
     */
    public static function get_login_url($redirect = '') {
        $design = get_option('movie_engine_design_settings', []);
        $login_page_id = $design['login_page'] ?? 0;

        if ($login_page_id) {
            $url = get_permalink($login_page_id);
            if ($redirect) {
                $url = add_query_arg('redirect_to', $redirect, $url);
            }
            return $url;
        }

        return wp_login_url($redirect);
    }

    public static function get_pricing_url() {
        $design = get_option('movie_engine_design_settings', []);
        $pricing_page_id = $design['pricing_page'] ?? 0;

        return $pricing_page_id ? get_permalink($pricing_page_id) : '#';
    }

    public static function get_dashboard_url($tab = '') {
        $design = get_option('movie_engine_design_settings', []);
        $dashboard_page_id = $design['dashboard_page'] ?? 0;

        if (!$dashboard_page_id) {
            return '#';
        }

        $url = get_permalink($dashboard_page_id);

        if ($tab) {
            $url = add_query_arg('tab', $tab, $url);
        }

        return $url;
    }

    /**
     * Get basic membership status
     * Pro version overrides this via filter.
     */
    public static function get_membership_status($user_id = null) {
        $status = [
            'is_premium' => self::is_premium($user_id),
            'type' => 'free',
            'plan_name' => esc_html__('Free', 'cine-movie-engine'),
            'expiry' => null,
            'in_trial' => false,
            'trial_days_remaining' => 0
        ];

        return apply_filters('movie_engine_membership_status', $status, $user_id);
    }

    public static function is_in_trial_period($user_id = null) {
        return apply_filters('movie_engine_is_in_trial', false, $user_id);
    }

    public static function get_remaining_trial_days($user_id = null) {
        return apply_filters('movie_engine_trial_remaining_days', 0, $user_id);
    }

    public static function get_movie_engine_plan_data($plan_id) {
        $default = [
            'id'       => $plan_id,
            'name'     => get_the_title($plan_id),
            'price'    => 0,
            'currency' => 'USD',
            'interval' => 'month',
            'features' => [],
            'is_hot'   => false,
            'badge'    => ''
        ];

        return apply_filters('movie_engine_plan_data', $default, $plan_id);
    }

    public static function get_currency_symbol() {
        $payments = get_option('movie_engine_payments_settings', []);
        $currency = $payments['currency'] ?? 'USD';

        $symbols = [
            'USD' => '$',
            'EUR' => '€',
            'GBP' => '£',
            'BDT' => '৳',
            'INR' => '₹',
        ];
        return $symbols[$currency] ?? $currency;
    }

    /**
     * Paywall & Access Control
     */
    public static function is_restricted($post_id) {
        return apply_filters('movie_engine_is_restricted', false, $post_id);
    }

    public static function should_show_partial_paywall($post_id) {
        return apply_filters('movie_engine_partial_paywall_duration', false, $post_id);
    }

    public static function get_preview_duration() {
        $settings = get_option('movie_engine_monetization_settings', []);
        $duration = intval($settings['movie_engine_preview_duration'] ?? ($settings['preview_duration'] ?? 0));
        return apply_filters('movie_engine_preview_duration', $duration);
    }
}
