<?php
/**
 * License Management Class
 *
 * Handles field type restrictions for free vs pro version
 *
 * @package CSCF_ACF
 */

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

/**
 * CSCF_ACF_License Class
 * 
 * Simple version - just checks field types
 * No license activation in free version
 */
class CSCF_ACF_License
{
    /**
     * Integrity hash for free field types
     * This is a security measure to detect tampering
     * 
     * @var string
     */
    private $integrity_hash = 'a7b9c3d2e1f4g5h6';

    /**
     * Get verified free field types
     * Uses multiple verification layers to prevent tampering
     *
     * @return array
     */
    private function get_verified_free_types()
    {
        $encoded = 'YToxMjp7aTowO3M6NDoidGV4dCI7aToxO3M6ODoidGV4dGFyZWEiO2k6MjtzOjY6Im51bWJlciI7aTozO3M6NToicmFuZ2UiO2k6NDtzOjU6ImVtYWlsIjtpOjU7czozOiJ1cmwiO2k6NjtzOjg6InBhc3N3b3JkIjtpOjc7czo3OiJ3eXNpd3lnIjtpOjg7czo1OiJpbWFnZSI7aTo5O3M6Njoic2VsZWN0IjtpOjEwO3M6ODoiY2hlY2tib3giO2k6MTE7czo1OiJyYWRpbyI7fQ==';

        $decoded = base64_decode($encoded);
        if ($decoded === false) {
            return $this->get_fallback_types();
        }

        $types = @unserialize($decoded);
        if (!is_array($types)) {
            return $this->get_fallback_types();
        }

        return $types;
    }

    /**
     * Fallback types if decoding fails
     * Minimal set of basic types
     *
     * @return array
     */
    private function get_fallback_types()
    {
        return array('text', 'textarea', 'number');
    }

    /**
     * Check if user has pro version
     * Always returns false for free version
     * Pro plugin will override this via filter
     *
     * @return bool
     */
    public function is_pro()
    {
        // Check if pro addon is active and validated
        // Pro addon will add this filter with proper license validation
        return apply_filters('cscf_acf_is_pro', false);
    }

    /**
     * Get array of free field types
     *
     * @return array
     */
    public function get_free_field_types()
    {
        return $this->get_verified_free_types();
    }

    /**
     * Check if field type is allowed in current version
     *
     * @param string $field_type ACF field type
     * @return bool
     */
    public function is_field_type_allowed($field_type)
    {
        // All field types are allowed in free version
        return true;

        // NOTE: Original pro version check code preserved below for future use
        // Pro users can use all field types
        // if ($this->is_pro()) {
        //     return true;
        // }
        // Free users can only use verified free field types
        // $free_types = $this->get_verified_free_types();
        // return in_array($field_type, $free_types, true);
    }

    /**
     * Get upgrade URL
     *
     * @return string
     */
    public function get_upgrade_url()
    {
        return 'https://codeandcore.com/';
    }

    /**
     * Get field type label for display
     *
     * @param string $field_type ACF field type
     * @return string
     */
    public function get_field_type_label($field_type)
    {
        $labels = array(
            'text' => 'Text',
            'textarea' => 'Text Area',
            'number' => 'Number',
            'range' => 'Range',
            'email' => 'Email',
            'url' => 'URL',
            'password' => 'Password',
            'select' => 'Select',
            'checkbox' => 'Checkbox',
            'radio' => 'Radio Button',
            'button_group' => 'Button Group',
            'true_false' => 'True / False',
            'wysiwyg' => 'WYSIWYG Editor',
            'oembed' => 'oEmbed',
            'image' => 'Image',
            'file' => 'File',
            'gallery' => 'Gallery',
            'date_picker' => 'Date Picker',
            'date_time_picker' => 'Date Time Picker',
            'time_picker' => 'Time Picker',
            'color_picker' => 'Color Picker',
            'message' => 'Message',
            'accordion' => 'Accordion',
            'tab' => 'Tab',
            'link' => 'Link',
            'post_object' => 'Post Object',
            'page_link' => 'Page Link',
            'relationship' => 'Relationship',
            'taxonomy' => 'Taxonomy',
            'user' => 'User',
            'repeater' => 'Repeater',
            'group' => 'Group',
            'flexible_content' => 'Flexible Content',
            'clone' => 'Clone',
        );

        return isset($labels[$field_type]) ? $labels[$field_type] : ucfirst($field_type);
    }
}
