<?php
/**
 * Author Pro Footer Block Template.
 *
 * @param   array $attributes - Block attributes.
 * @param   string $content - Block content.
 * @param   WP_Block $block - Block object.
 */

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

// Attributes
$block_id       = isset( $attributes['blockId'] ) ? $attributes['blockId'] : 'footer-' . uniqid();
$anchor         = isset( $attributes['anchor'] ) ? $attributes['anchor'] : '';
$element_id     = ! empty( $anchor ) ? $anchor : $block_id;

$logo_type      = isset( $attributes['logoType'] ) ? $attributes['logoType'] : 'custom';
$logo_text      = isset( $attributes['logoText'] ) ? $attributes['logoText'] : 'AuthorPro.';
$logo_image_url = isset( $attributes['logoImageUrl'] ) ? $attributes['logoImageUrl'] : '';
$description    = isset( $attributes['description'] ) ? $attributes['description'] : '';
// Safe array handling
$social_links   = ( isset( $attributes['socialLinks'] ) && is_array( $attributes['socialLinks'] ) ) ? $attributes['socialLinks'] : array();

$col2_title     = isset( $attributes['col2Title'] ) ? $attributes['col2Title'] : 'Quick Links';
$menu2_slug     = isset( $attributes['menu2Slug'] ) ? $attributes['menu2Slug'] : '';

$col3_title     = isset( $attributes['col3Title'] ) ? $attributes['col3Title'] : 'Resources';
$menu3_slug     = isset( $attributes['menu3Slug'] ) ? $attributes['menu3Slug'] : '';

$col4_title     = isset( $attributes['col4Title'] ) ? $attributes['col4Title'] : 'Contact Info';
// Safe array handling
$contact_details = ( isset( $attributes['contactDetails'] ) && is_array( $attributes['contactDetails'] ) ) ? $attributes['contactDetails'] : array();

$copyright_text  = isset( $attributes['copyrightText'] ) ? $attributes['copyrightText'] : '';
$bottom_menu_slug = isset( $attributes['bottomMenuSlug'] ) ? $attributes['bottomMenuSlug'] : '';

// Colors
$bg_color      = isset( $attributes['footerBgColor'] ) ? $attributes['footerBgColor'] : '';
$text_color    = isset( $attributes['textColor'] ) ? $attributes['textColor'] : '';
$heading_color = isset( $attributes['headingColor'] ) ? $attributes['headingColor'] : '';
$accent_color  = isset( $attributes['accentColor'] ) ? $attributes['accentColor'] : '';
$border_color  = isset( $attributes['borderColor'] ) ? $attributes['borderColor'] : '';

// Helper to get menu links safely
$get_menu_links = function( $slug ) {
    if ( ! $slug ) {
        return array();
    }
    $items = wp_get_nav_menu_items( $slug );
    return ( $items && is_array( $items ) ) ? $items : array();
};

$menu2_items  = $get_menu_links( $menu2_slug );
$menu3_items  = $get_menu_links( $menu3_slug );
$bottom_items = $get_menu_links( $bottom_menu_slug );

// CSS Variables
$style_vars = '';
if ( ! empty( $bg_color ) ) {
    $style_vars .= "--awt-footer-bg: {$bg_color};";
}
if ( ! empty( $text_color ) ) {
    $style_vars .= "--awt-text-color: {$text_color};";
}
if ( ! empty( $heading_color ) ) {
    $style_vars .= "--awt-heading-color: {$heading_color};";
}
if ( ! empty( $accent_color ) ) {
    $style_vars .= "--awt-accent-color: {$accent_color};";
}
if ( ! empty( $border_color ) ) {
    $style_vars .= "--awt-border-color: {$border_color};";
}

// Wrapper parameters
$wrapper_attr = get_block_wrapper_attributes( array(
    'class' => "bg-primary text-slate-400 pt-20 pb-10 border-t border-slate-800 awt-footer block-{$block_id}",
    'id'    => $element_id,
    'style' => $style_vars,
) );
?>

<style>
    .block-<?php echo esc_attr( $block_id ); ?>.awt-footer {
        <?php if ( ! empty( $bg_color ) ) : ?>
        background-color: var(--awt-footer-bg);
        <?php endif; ?>
        <?php if ( ! empty( $text_color ) ) : ?>
        color: var(--awt-text-color);
        <?php endif; ?>
        <?php if ( ! empty( $border_color ) ) : ?>
        border-color: var(--awt-border-color);
        <?php endif; ?>
    }
    .block-<?php echo esc_attr( $block_id ); ?>.awt-footer h4, 
    .block-<?php echo esc_attr( $block_id ); ?>.awt-footer .logo-text {
        <?php if ( ! empty( $heading_color ) ) : ?>
        color: var(--awt-heading-color);
        <?php endif; ?>
    }
    .block-<?php echo esc_attr( $block_id ); ?>.awt-footer a:hover,
    .block-<?php echo esc_attr( $block_id ); ?>.awt-footer .social-icon:hover {
        <?php if ( ! empty( $accent_color ) ) : ?>
        color: var(--awt-accent-color);
        <?php endif; ?>
    }
    .block-<?php echo esc_attr( $block_id ); ?>.awt-footer .social-icon:hover {
        <?php if ( ! empty( $accent_color ) ) : ?>
        background-color: var(--awt-accent-color);
        color: #fff !important;
        <?php endif; ?>
    }
    .block-<?php echo esc_attr( $block_id ); ?>.awt-footer .contact-icon {
        <?php if ( ! empty( $accent_color ) ) : ?>
        color: var(--awt-accent-color);
        <?php endif; ?>
    }
    /* Logo Box default */
    .block-<?php echo esc_attr( $block_id ); ?>.awt-footer .logo-box {
        background-color: #fff;
        <?php if ( ! empty( $bg_color ) ) : ?>
        color: var(--awt-footer-bg); /* Primary */
        <?php endif; ?>
    }
    .block-<?php echo esc_attr( $block_id ); ?>.awt-footer .awt-custom-icon {
        object-fit: contain;
    }
    /* SVG Styling in Links/Icons */
    .block-<?php echo esc_attr( $block_id ); ?>.awt-footer svg {
        width: 1rem; /* w-4 */
        height: 1rem; /* h-4 */
        fill: currentColor;
    }
</style>

<footer <?php echo $wrapper_attr; ?>>
    <div class="awt-container">
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-12 mb-16">
            <!-- Col 1: Logo & Bio -->
            <div>
                <a href="<?php echo esc_url( home_url( '/' ) ); ?>" class="flex items-center gap-2 mb-6">
                    <?php if ( 'image' === $logo_type && $logo_image_url ) : ?>
                         <img src="<?php echo esc_url( $logo_image_url ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>" class="max-h-12 w-auto" />
                    <?php elseif ( 'site_title' === $logo_type ) : ?>
                        <span class="text-xl font-serif font-bold text-white logo-text">
                            <?php echo esc_html( get_bloginfo( 'name' ) ); ?>
                        </span>
                    <?php else : ?>
                        <span class="w-8 h-8 bg-white text-primary rounded flex items-center justify-center font-bold font-serif logo-box">
                            <?php echo esc_html( substr( $logo_text, 0, 1 ) ); ?>
                        </span>
                        <span class="text-xl font-serif font-bold text-white logo-text">
                            <?php echo esc_html( $logo_text ); ?>
                        </span>
                    <?php endif; ?>
                </a>
                <p class="text-sm leading-relaxed mb-6">
                    <?php echo wp_kses_post( $description ); ?>
                </p>
                <div class="flex gap-4">
                    <?php if ( ! empty( $social_links ) && is_array( $social_links ) ) : ?>
                        <?php foreach ( $social_links as $link ) : ?>
                            <?php 
                                $icon_url = isset( $link['iconUrl'] ) ? $link['iconUrl'] : '';
                                $svg_content = isset( $link['svgContent'] ) ? $link['svgContent'] : '';
                                $url = isset( $link['url'] ) ? $link['url'] : '#';
                            ?>
                            <a href="<?php echo esc_url( $url ); ?>" class="w-8 h-8 bg-slate-800 rounded flex items-center justify-center text-slate-400 hover:bg-accent hover:text-white transition">
                                 <?php if ( $icon_url ) : ?>
                                    <img src="<?php echo esc_url( $icon_url ); ?>" alt="Social" class="awt-custom-icon w-4 h-4" />
                                <?php elseif ( ! empty( $svg_content ) ) : ?>
                                    <span class="w-4 h-4 flex items-center justify-center"><?php echo $svg_content; ?></span>
                                <?php endif; ?>
                            </a>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Col 2: Quick Links -->
            <div>
                <h4 class="text-white font-bold text-sm uppercase tracking-widest mb-6"><?php echo esc_html( $col2_title ); ?></h4>
                <ul class="space-y-3 text-sm list-none pl-0">
                    <?php if ( ! empty( $menu2_items ) && is_array( $menu2_items ) ) : ?>
                        <?php foreach ( $menu2_items as $item ) : ?>
                             <li><a href="<?php echo esc_url( $item->url ); ?>" class="hover:text-accent text-slate-400 transition"><?php echo esc_html( $item->title ); ?></a></li>
                        <?php endforeach; ?>
                    <?php else : ?>
                        <li class="italic opacity-50">Select a menu in block settings.</li>
                    <?php endif; ?>
                </ul>
            </div>

            <!-- Col 3: Resources -->
            <div>
                <h4 class="text-white font-bold text-sm uppercase tracking-widest mb-6"><?php echo esc_html( $col3_title ); ?></h4>
                <ul class="space-y-3 text-sm list-none pl-0">
                    <?php if ( ! empty( $menu3_items ) && is_array( $menu3_items ) ) : ?>
                        <?php foreach ( $menu3_items as $item ) : ?>
                             <li><a href="<?php echo esc_url( $item->url ); ?>" class="hover:text-accent text-slate-400 transition"><?php echo esc_html( $item->title ); ?></a></li>
                        <?php endforeach; ?>
                    <?php else : ?>
                        <li class="italic opacity-50">Select a menu in block settings.</li>
                    <?php endif; ?>
                </ul>
            </div>

            <!-- Col 4: Contact Info -->
            <div>
                <h4 class="text-white font-bold text-sm uppercase tracking-widest mb-6"><?php echo esc_html( $col4_title ); ?></h4>
                <ul class="space-y-4 text-sm list-none pl-0">
                    <?php if ( ! empty( $contact_details ) && is_array( $contact_details ) ) : ?>
                        <?php foreach ( $contact_details as $detail ) : ?>
                            <?php 
                                $icon_url = isset( $detail['iconUrl'] ) ? $detail['iconUrl'] : '';
                                $svg_content = isset( $detail['svgContent'] ) ? $detail['svgContent'] : '';
                                $text = isset( $detail['text'] ) ? $detail['text'] : '';
                            ?>
                            <li class="flex items-start gap-3">
                                 <?php if ( $icon_url ) : ?>
                                    <img src="<?php echo esc_url( $icon_url ); ?>" alt="Icon" class="awt-custom-icon w-4 h-4 mt-1" />
                                <?php elseif ( ! empty( $svg_content ) ) : ?>
                                    <span class="contact-icon mt-1 text-accent flex items-center justify-center w-4 h-4"><?php echo $svg_content; ?></span>
                                <?php endif; ?>
                                <span><?php echo wp_kses_post( $text ); ?></span>
                            </li>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </ul>
            </div>
        </div>

        <!-- Bottom Area -->
        <div class="border-t border-slate-800 pt-8 flex flex-col md:flex-row justify-between items-center text-xs text-slate-500">
            <p><?php echo wp_kses_post( $copyright_text ); ?></p>
            <div class="flex gap-6 mt-4 md:mt-0 flex-wrap">
                <?php if ( ! empty( $bottom_items ) && is_array( $bottom_items ) ) : ?>
                     <?php foreach ( $bottom_items as $item ) : ?>
                        <a href="<?php echo esc_url( $item->url ); ?>" class="hover:text-white text-slate-400 transition"><?php echo esc_html( $item->title ); ?></a>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>
    </div>
</footer>
