<?php
 
 /**
 * =============================================================================
 * @package         Blue Raven (Free Version)
 * @Plugin URI      https://enterrahost.com/plugins/blue-raven
 * 
 * @Author          EnterraHost
 * @Author URI      https://enterrahost.com
 * @License         GPL-2.0-or-later
 *  
 * @Copyright       (c) 2025 EnterraHost
 * @Contents        Load Brandability Section
 * 
 * =============================================================================
 * 
 * || INDEX:
 * ||   - BLUE RAVEN ADMIN BRANDABILITY
 * || 
 * =============================================================================
 */

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

/*
============================================================
    BLUE RAVEN ADMIN BRANDABILITY
============================================================
*/

class Blue_Raven_Admin_Brandability {

    /**
     * Renders the Brandability interface.
     *
     * Public method called to output the Brandability UI.
     * It typically handles layout and delegates template logic
     * to internal methods like `load_brandability_template()`.
     *
     * @access public
     * @return void
     */
    public function render() {
        self::load_brandability_template();
        exit();
    }
    
    /**
     * Loads the Brandability HTML template.
     *
     * Outputs the HTML content specific to the Brandability section,
     * including UI structure and any necessary wrapper markup.
     *
     * @access private
     * @static
     * @return void
     */
    private static function load_brandability_template() {
        
        global $wpdb;
        
    ## =========== First retrieve the data from database =========== #
        
        $brandabilityConfig = $wpdb->get_row(  
            $wpdb->prepare(
                "SELECT value FROM {$wpdb->prefix}BlueRavenValues 
                 WHERE setting LIKE %s
                 LIMIT 1",  
                '%brandability%'
            )
        );
        
    ## =========== Initialize all possible fields with default empty values =========== #
        
        $formData = [
            'brand-title' => '',
            'businessDescription' => '',
            'usp' => '',
            'offerings' => [],
            'audience' => '',
            'category' => '',
            'brand-title-style' => '',
            'brand-description-style' => '',
            'bullet-points' => ''
        ];
        
    ## =========== If data exists, decode and merge with defaults =========== #        

        if ($brandabilityConfig && !empty($brandabilityConfig->value)) {
            $savedData = json_decode($brandabilityConfig->value, true);
            if ($savedData !== null) $formData = array_merge($formData, $savedData);
        }
       
    ## =========== access each field safely =========== #      
       
        $brandTitle = htmlspecialchars($formData['brand-title'], ENT_QUOTES, 'UTF-8');
        $businessDescription = htmlspecialchars($formData['businessDescription'], ENT_QUOTES, 'UTF-8');
        $usp = htmlspecialchars($formData['usp'], ENT_QUOTES, 'UTF-8');
        $audience = htmlspecialchars($formData['audience'], ENT_QUOTES, 'UTF-8');
        $category = htmlspecialchars($formData['category'], ENT_QUOTES, 'UTF-8');
        $brandTitleStyle = htmlspecialchars($formData['brand-title-style'], ENT_QUOTES, 'UTF-8');
        $brandDescStyle = htmlspecialchars($formData['brand-description-style'], ENT_QUOTES, 'UTF-8');
        $bulletPoints = htmlspecialchars($formData['bullet-points'], ENT_QUOTES, 'UTF-8');
        
        $offerings = is_array($formData['offerings']) ? $formData['offerings'] : [];
        $offeringsP = in_array('P', $offerings) ? 'checked' : '';
        $offeringsS = in_array('S', $offerings) ? 'checked' : '';

    ## =========== HTML =========== # 
    
        ?>  
            <form id='brandability-form'>
                <div style='width: 100%;'>
                    <h2><?php esc_html_e('Brandability', 'blue-raven'); ?></h2>
                
                    <fieldset class='border-bottom-white'>
                        <h4><?php esc_html_e('About your Brand', 'blue-raven'); ?></h4>
                        
                        <div style="margin: 0; width: 100%;" class='spacing-bottom'>
                            <label for="brand-title" class='text-center'>
                                <p class='no-margin-padding spacing-right-small text-center'><?php esc_html_e('Brand Name', 'blue-raven'); ?></p>
                            </label>
                            <input type='text' name='brand-title' id='brand-title' placeholder='<?php esc_attr_e('My Business', 'blue-raven'); ?>' 
                                   class='br-input' style='width:100%;' value="<?php echo esc_attr($brandTitle); ?>">
                        </div>
                        
                        <div style="margin: 0; width: 100%;" class='spacing-bottom'>
                            <label for="businessDescription" class='text-center'>
                                <p class='no-margin-padding spacing-right-small text-center'><?php esc_html_e('Business Description', 'blue-raven'); ?></p>
                            </label>
                            <textarea class="br-text-area" id="businessDescription" name="businessDescription" rows="10" cols="50" required style="min-width: 100%; padding: 0.5rem;"><?php echo esc_textarea( $businessDescription ); ?></textarea>
                        </div> 
                        
                        <div style="margin: 0; width: 100%;" class='spacing-bottom'>
                            <label for="usp" class='text-center'>
                                <p class='no-margin-padding spacing-right-small text-center'><?php esc_html_e('Unique Selling Point', 'blue-raven'); ?></p>
                            </label>
                            <textarea class='br-text-area' id="usp" name="usp" required rows="5" cols="50" style='min-width: 100%; padding: 0.5rem;'><?php echo esc_textarea($usp); ?></textarea>
                        </div>
                    </fieldset>
                           
                    <fieldset class='border-bottom-white'>
                        <h4><?php esc_html_e('Extras About Your Brand', 'blue-raven'); ?></h4>
                        <div class='flex-sb'>
                            <div style='width: 49%'>
                                <p class='no-margin-padding spacing-right-small text-center'><?php esc_html_e('Products or Services', 'blue-raven'); ?></p>
                                <div class='flex-left spacing-bottom'>
                                    <label class="br-checkbox spacing-right" style="width: 20%;">
                                        <input type="checkbox" name="offerings[]" value="P" style="display: none;" id="products" <?php echo esc_attr($offeringsP); ?>>
                                        <span class="br-check-mark"></span>
                                        <label for="products"><?php esc_html_e('Products', 'blue-raven'); ?></label><br>
                                    </label>
                                    <label class="br-checkbox spacing-right" style="width: 20%;">
                                        <input type="checkbox" name="offerings[]" value="S" style="display: none;" id="services" <?php echo esc_attr($offeringsS); ?>>
                                        <span class="br-check-mark"></span>
                                        <label for="services"><?php esc_html_e('Services', 'blue-raven'); ?></label><br><br>
                                    </label>
                                </div>
                            </div>
                            <div style='width: 49%'>
                                <p class='no-margin-padding spacing-right-small text-center'><?php esc_html_e('Target Audience', 'blue-raven'); ?></p>
                                <div class='flex-left spacing-bottom'>
                                    <label class="br-checkbox spacing-right" style="width: 20%;">
                                        <input type="radio" name="audience" value="B2B" style="display: none;" id="B2B" <?php checked($audience, 'B2B'); ?> required>
                                        <span class="br-check-mark"></span>
                                        <label for="B2B"><?php esc_html_e('B2B', 'blue-raven'); ?></label><br>
                                    </label>
                                    <label class="br-checkbox spacing-right" style="width: 20%;">
                                        <input type="radio" name="audience" value="B2C" style="display: none;" id="B2C" <?php checked($audience, 'B2C'); ?>>
                                        <span class="br-check-mark"></span>
                                        <label for="B2C"><?php esc_html_e('B2C', 'blue-raven'); ?></label><br><br>
                                    </label>
                                    <label class="br-checkbox spacing-right" style="width: 20%;">
                                        <input type="radio" name="audience" value="Both" style="display: none;" id="Both" <?php checked($audience, 'Both'); ?>>
                                        <span class="br-check-mark"></span>
                                        <label for="Both"><?php esc_html_e('Both', 'blue-raven'); ?></label><br><br>
                                    </label>
                                </div>
                            </div>
                        </div>
                        <label class='text-center'>
                            <p class='no-margin-padding spacing-right-small text-center'><?php esc_html_e('Business Category', 'blue-raven'); ?></p>
                        </label>
                        <select id="category" name='category' class="br-dropdown" style="width: 100%; padding: 0.5rem; margin-bottom: 2rem;">
                            <option value=""><?php esc_html_e('Select a category', 'blue-raven'); ?></option>
                            <option value="agriculture" <?php selected($category, 'agriculture'); ?>><?php esc_html_e('Agriculture', 'blue-raven'); ?></option>
                            <option value="automotive" <?php selected($category, 'automotive'); ?>><?php esc_html_e('Automotive', 'blue-raven'); ?></option>
                            <option value="banking_finance" <?php selected($category, 'banking_finance'); ?>><?php esc_html_e('Banking & Finance', 'blue-raven'); ?></option>
                            <option value="construction" <?php selected($category, 'construction'); ?>><?php esc_html_e('Construction', 'blue-raven'); ?></option>
                            <option value="education" <?php selected($category, 'education'); ?>><?php esc_html_e('Education', 'blue-raven'); ?></option>
                            <option value="energy_utilities" <?php selected($category, 'energy_utilities'); ?>><?php esc_html_e('Energy & Utilities', 'blue-raven'); ?></option>
                            <option value="entertainment" <?php selected($category, 'entertainment'); ?>><?php esc_html_e('Entertainment', 'blue-raven'); ?></option>
                            <option value="food_beverage" <?php selected($category, 'food_beverage'); ?>><?php esc_html_e('Food & Beverage', 'blue-raven'); ?></option>
                            <option value="healthcare" <?php selected($category, 'healthcare'); ?>><?php esc_html_e('Healthcare', 'blue-raven'); ?></option>
                            <option value="hospitality" <?php selected($category, 'hospitality'); ?>><?php esc_html_e('Hospitality', 'blue-raven'); ?></option>
                            <option value="information_technology" <?php selected($category, 'information_technology'); ?>><?php esc_html_e('Information Technology', 'blue-raven'); ?></option>
                            <option value="manufacturing" <?php selected($category, 'manufacturing'); ?>><?php esc_html_e('Manufacturing', 'blue-raven'); ?></option>
                            <option value="media" <?php selected($category, 'media'); ?>><?php esc_html_e('Media', 'blue-raven'); ?></option>
                            <option value="pharmaceuticals" <?php selected($category, 'pharmaceuticals'); ?>><?php esc_html_e('Pharmaceuticals', 'blue-raven'); ?></option>
                            <option value="real_estate" <?php selected($category, 'real_estate'); ?>><?php esc_html_e('Real Estate', 'blue-raven'); ?></option>
                            <option value="retail" <?php selected($category, 'retail'); ?>><?php esc_html_e('Retail', 'blue-raven'); ?></option>
                            <option value="telecommunications" <?php selected($category, 'telecommunications'); ?>><?php esc_html_e('Telecommunications', 'blue-raven'); ?></option>
                            <option value="transportation_logistics" <?php selected($category, 'transportation_logistics'); ?>><?php esc_html_e('Transportation & Logistics', 'blue-raven'); ?></option>
                            <option value="travel_tourism" <?php selected($category, 'travel_tourism'); ?>><?php esc_html_e('Travel & Tourism', 'blue-raven'); ?></option>
                        </select>
                    </fieldset>
                    
                    <fieldset>
                        <h4><?php esc_html_e('Template Information', 'blue-raven'); ?></h4>
                        
                        <div style="margin: 0; width: 100%;" class='spacing-bottom'>
                            <label for="brand-title-style" class='text-center'> <p class='no-margin-padding spacing-right-small text-center'><?php esc_html_e('Template Title Structure', 'blue-raven'); ?></p></label>
                            <p class='no-margin-padding spacing-right-small text-center' style='opacity: 0.5; font-style: italic;'><?php esc_html_e('Examples :', 'blue-raven'); ?></p>
                            
                            <?php $title_blocks = ["Brand Name", "Product Type", "Key Features/Specifications", "Model Number or Name", "Intended Use or Target Audience", "Unique Selling Points", "My Businesses Name"];?>
                            
                            <div id="title-blocks" style="margin-bottom: 0.5rem;">
                                <?php foreach ($title_blocks as $block) :  $isActive = stripos($brandTitleStyle, $block) !== false; ?>
                                    <div class="br-block-item br-tag-item flex-sb blue-tag title-tag <?php echo $isActive ? 'active' : ''; ?>" style="display:inline-block">
                                        <p class="template-title-block"><?php echo esc_html($block); ?></p>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                            <input type='text' name='brand-title-style' id='brand-title-style' placeholder='<?php esc_attr_e('Brand Name, Product Type, Key Features/Specifications', 'blue-raven'); ?>' class='br-input' style='width:100%;' value="<?php echo esc_attr($brandTitleStyle); ?>">
                        </div>
                        
                        <div style="margin: 0; width: 100%;" class='spacing-bottom'>
                            <label for="brand-description-style" class="text-center"><p class="no-margin-padding spacing-right-small text-center"><?php esc_html_e('Template Description Structure', 'blue-raven'); ?></p></label>
                            <p class="no-margin-padding spacing-right-small text-center" style="opacity: 0.5; font-style: italic;"> <?php esc_html_e('Examples:', 'blue-raven'); ?></p>
                            <?php $description_blocks = ["Product Overview", "Features", "Specifications", "Usage Instructions", "Benefits", "Target Audience", "Use case", "Compatibility", "Care Instruction", "Warranty Information", "Customer Testimonial", "Pricing Details", "Purchase Options", "Shipping Information", "Return Policy", "Call to Action"];?>

                            <div id="description-blocks" style="margin-bottom: 0.5rem;">
                                <?php foreach ($description_blocks as $block) :  $isActive = stripos($brandDescStyle, $block) !== false; ?>
                                    <div class="br-block-item br-tag-item flex-sb blue-tag description-tag <?php echo $isActive ? 'active' : ''; ?>" style="display: inline-block">
                                        <p class="template-description-block"><?php echo esc_html($block); ?> </p>
                                    </div>
                                <?php endforeach; ?>
                            </div>

                            <input type="text" name="brand-description-style" id="brand-description-style" placeholder="<?php esc_attr_e('Product Overview, Features, Specifications', 'blue-raven'); ?>" class="br-input"  style="width: 100%" value="<?php echo esc_attr($brandDescStyle); ?>">
                        </div>
                        
                        <div style="margin: 0; width: 100%;" class='spacing-bottom'>
                            <label for="bullet-points" class='text-center'>
                                <p class='no-margin-padding spacing-right-small text-center'><?php esc_html_e('Bullet Point', 'blue-raven'); ?></p>
                            </label>
                            <input type='text' id="bullet-points" name="bullet-points" style='min-width: 30rem; padding: 0.5rem;' required placeholder='some bulletpoint like "⭐️"' class='br-input' style='width:100%;' value="<?php echo esc_attr($bulletPoints); ?>">
                        </div>
                    </fieldset>
                    <p id='brandability-form-error-message'></p>
                    <div style='text-align: right; margin: 4rem auto'>
                        <button class='enterra-button' id='save-brand-settings'><?php esc_html_e('Save Brand Settings', 'blue-raven'); ?></button>
                    </div>
                </div>
            </form>    
        <?php
    }
}