<?php
/**
 * Render template for Author Pro Author Taxonomy Info block
 *
 * @package Author_Website_Templates
 */

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

// Attributes
$accentColor = isset($attributes['accentColor']) ? $attributes['accentColor'] : '#ea580c';

// Helper to hex to rgb
function hex2rgb($hex) {
    $hex = str_replace("#", "", $hex);
    if(strlen($hex) == 3) {
        $r = hexdec(substr($hex,0,1).substr($hex,0,1));
        $g = hexdec(substr($hex,1,1).substr($hex,1,1));
        $b = hexdec(substr($hex,2,1).substr($hex,2,1));
    } else {
        $r = hexdec(substr($hex,0,2));
        $g = hexdec(substr($hex,2,2));
        $b = hexdec(substr($hex,4,2));
    }
    return array($r, $g, $b);
}

// Get Accent Color RGBA for opacity
$rgb = hex2rgb($accentColor);
$accent_bg_light = "rgba({$rgb[0]}, {$rgb[1]}, {$rgb[2]}, 0.1)";

// Get Current Term
$term = get_queried_object();
$author_name = 'Author Name';
$author_description = 'Author biography description will appear here on the frontend.';
$author_image_url = 'https://images.unsplash.com/photo-1556157382-97eda2d62296?auto=format&fit=crop&q=80&w=800'; // Default placeholder
$social_profiles = [];
$total_books = 0;

// If in editor or not a term, use mock data? 
// Or try to get a sample term? 
// For now, if $term is valid, use it.
if ($term instanceof WP_Term && $term->taxonomy === 'book-author') {
    $author_name = $term->name;
    $author_description = term_description($term->term_id);
    if (empty($author_description)) {
        // Fallback to meta description if term description is empty
        $author_description = get_term_meta($term->term_id, 'rswpbs_book_author_description', true);
    }
    
    $author_image_url_meta = get_term_meta($term->term_id, 'rswpbs_book_author_picture', true);
    if (!empty($author_image_url_meta)) {
        $author_image_url = $author_image_url_meta; 
    }
    
    $social_profiles = get_term_meta($term->term_id, 'rswpbs_book_author_social_profiles', true);
    $total_books = $term->count;
} else {
    // Possibly in editor logic where get_queried_object might not return what we expect always.
    // If inside Render block in editor, $term might be null or page.
    // We can show placeholder data in editor.
}

if (!is_tax('book-author')) {
    return;
}

?>

<section class="py-24 bg-paper <?php echo isset($attributes['className']) ? esc_attr($attributes['className']) : ''; ?>">
    <div class="awt-container px-10 mx-auto">
        <div class="grid grid-cols-1 lg:grid-cols-12 gap-12 lg:gap-20 items-start">
            
            <div class="lg:col-span-5 relative">
                <div class="relative group">
                    <div class="absolute top-4 -left-4 w-full h-full border-2 rounded-2xl z-0 transition-transform duration-500 group-hover:translate-x-2 group-hover:translate-y-2" style="border-color: <?php echo esc_attr($accentColor); ?>;"></div>
                    <div class="absolute -bottom-4 -right-4 w-full h-full bg-slate-50 rounded-2xl -z-10"></div>
                    
                    <img src="<?php echo esc_url($author_image_url); ?>" 
                         alt="<?php echo esc_attr($author_name); ?>" 
                         class="relative z-10 w-full aspect-[3/4] object-cover rounded-2xl shadow-xl grayscale group-hover:grayscale-0 transition duration-700">
                    
                    <div class="absolute -bottom-6 -right-6 z-20 bg-white p-5 rounded-xl shadow-[0_8px_30px_rgb(0,0,0,0.12)] border-l-4" style="border-color: <?php echo esc_attr($accentColor); ?>;">
                        <div class="flex items-center gap-3">
                            <div class="p-2 rounded-full" style="color: <?php echo esc_attr($accentColor); ?>; background-color: <?php echo esc_attr($accent_bg_light); ?>;">
                                <i class="fas fa-pen-nib"></i>
                            </div>
                            <div>
                                <p class="text-sm font-bold text-gray-400 uppercase tracking-widest text-[10px]">Total Books</p>
                                <p class="text-xl font-serif font-bold text-primary"><?php echo esc_html($total_books); ?>+ Published</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="lg:col-span-7 pt-4">
                <span class="font-bold text-xs uppercase tracking-widest mb-3 block" style="color: <?php echo esc_attr($accentColor); ?>;">Biography</span>
                
                <h2 class="text-4xl lg:text-5xl font-serif font-bold text-primary mb-6"><?php echo esc_html($author_name); ?></h2>
                
                <div class="prose prose-lg prose-slate text-secondary mb-10 leading-relaxed text-justify">
                    <?php echo wp_kses_post($author_description); ?>
                </div>
                <?php if (!empty($social_profiles) && is_array($social_profiles)): ?>
                <div class="border-t border-gray-100 pt-8">
                    <h4 class="text-sm font-bold text-primary mb-4 uppercase tracking-widest">Connect with Author</h4>
                    <div class="flex flex-wrap gap-3">
                        <?php foreach ($social_profiles as $profile): ?>
                            <?php 
                                $link = isset($profile['profile_link']) ? $profile['profile_link'] : '#';
                                $icon = isset($profile['website_icon']) ? $profile['website_icon'] : 'fas fa-link';
                                $name = isset($profile['special_title']) ? $profile['special_title'] : 'Social'; // Assuming a title field or we can derive from icon/link
                                // If no title, we can guess or just show icon.
                                // Based on reference, we need a name. 
                                // If the array structure is unknown, I'll assume standard from the other blocks?
                                // User said: "Key rswpbs_book_author_social_profiles (Returns serialized array with website_icon class and profile_link)."
                                // I'll guess name or just use 'Social'.
                                if (empty($name) || $name === 'Social') {
                                        if (strpos($link, 'facebook') !== false) $name = 'Facebook';
                                        elseif (strpos($link, 'twitter') !== false) $name = 'Twitter';
                                        elseif (strpos($link, 'linkedin') !== false) $name = 'LinkedIn';
                                        elseif (strpos($link, 'instagram') !== false) $name = 'Instagram';
                                        else $name = 'Website';
                                }
                            ?>
                            <a href="<?php echo esc_url($link); ?>" target="_blank" class="group flex items-center gap-3 px-5 py-3 bg-slate-50 border border-gray-100 rounded-lg hover:bg-white hover:shadow-md transition duration-300 hover:border-[<?php echo esc_attr($accentColor); ?>]" 
                                onmouseover="this.style.borderColor='<?php echo esc_attr($accentColor); ?>'" 
                                onmouseout="this.style.borderColor=''"
                            >
                                <i class="<?php echo esc_attr($icon); ?> text-lg text-gray-400 transition" 
                                    style="transition: color 0.3s;"
                                    onmouseover="this.style.color='<?php echo esc_attr($accentColor); ?>'" 
                                    onmouseout="this.style.color=''"
                                ></i>
                                <span class="text-sm font-bold text-secondary group-hover:text-primary"><?php echo esc_html($name); ?></span> 
                            </a>
                        <?php endforeach; ?>
                    </div>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</section>
