<?php
/**
 * Yatoon Booking System - License (Pro Edition)
 * All features unlocked via Freemius license.
 */
if ( ! defined( 'ABSPATH' ) ) { exit;
}

class YATOON_License {

    const FREE_LIMITS = array(
        'staff' => 2,
    );

    public static function is_pro() {
        if ( function_exists( 'ybs_is_pro' ) ) {
            return (bool) ybs_is_pro();
        }

        return false;
    }

    public static function recheck() {
        if ( function_exists( 'ybs_fs' ) ) {
            try {
                if ( method_exists( ybs_fs(), '_sync_license' ) ) {
                    ybs_fs()->_sync_license();
                }
            } catch ( Exception $e ) {
                return false;
            }
        }

        return self::is_pro();
    }

    public static function cache_clear() {}
    public static function domain() {
 return isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : ''; }

    public static function free_limit( $type ) {
        $type = sanitize_key( $type );
        return isset( self::FREE_LIMITS[ $type ] ) ? (int) self::FREE_LIMITS[ $type ] : 0;
    }

    public static function check_limit( $type, $count ) {
        $limit = self::free_limit( $type );
        return self::is_pro() || ( $limit > 0 && absint( $count ) <= $limit );
    }
    public static function has_feature( $feature ) {
 return self::is_pro(); }

    public static function get_upgrade_url() {
        return defined( 'YBS_UPGRADE_URL' ) ? YBS_UPGRADE_URL : 'https://checkout.freemius.com/plugin/24668/plan/40958/';
    }

    public static function get_status_text() {
        $detail = self::get_status_detail();
        return '<span style="color:' . esc_attr( $detail['color'] ) . ';font-weight:600;">' . esc_html( $detail['label'] ) . '</span>';
    }

    public static function get_status_detail() {
        $registered = false;
        if ( function_exists( 'ybs_fs' ) ) {
            try {
                $registered = method_exists( ybs_fs(), 'is_registered' ) ? (bool) ybs_fs()->is_registered() : false;
            } catch ( Exception $e ) {
                $registered = false;
            }
        }

        if ( self::is_pro() ) {
            return array( 'state' => 'active', 'label' => 'Active Pro License', 'color' => '#10b981' );
        }

        if ( $registered ) {
            return array( 'state' => 'inactive', 'label' => 'Free Version - Upgrade Available', 'color' => '#f59e0b' );
        }

        return array( 'state' => 'unregistered', 'label' => 'Free Version', 'color' => '#6b7280' );
    }

    public static function get_pro_badge() {
        if ( self::is_pro() ) {
            return '<span style="background:#10B981;color:#fff;padding:2px 8px;border-radius:4px;font-size:11px;font-weight:700;margin-left:6px;">Pro Active</span>';
        }

        return '<span style="background:#f59e0b;color:#fff;padding:2px 8px;border-radius:4px;font-size:11px;font-weight:700;margin-left:6px;">License Needed</span>';
    }

    public static function get_upgrade_notice( $type ) {
        if ( self::is_pro() ) {
            return '';
        }

        return sprintf(
            __( 'The %s feature requires an active Pro license. Activate your license key in Freemius to unlock it.', 'yatoon-booking-system' ),
            $type
        );
    }

    public static function get_powered_by() {
        if ( self::is_pro() ) {
            return '';
        }
        return '<p style="font-size:11px;color:#9ca3af;text-align:center;margin-top:20px;">Powered by <a href="https://yatoon.com" target="_blank" style="color:#ec4899;">Yatoon</a></p>';
    }
}
