<?php
/**
 * Theme footer template
 *
 * @package KsPortfolio
 * @since 1.0.0
 */

namespace KsPortfolio;

$footerLogo = getFieldValue( 'footer.footer_logo.url' ) ? getFieldValue( 'footer.footer_logo.url' ) : '';
$footerDesc = getFieldValue( 'footer.footer_description' ) ? getFieldValue( 'footer.footer_description' ) : '';

$copyright_text = getFieldValue( 'footer.copyright_text' ) ? getFieldValue( 'footer.copyright_text' ) : 'Copyright &copy; %Y ' . get_bloginfo( 'name' );

$copyright = str_replace( '%Y', gmdate( 'Y' ), $copyright_text );

$locations = get_nav_menu_locations();
$footerNav = ! empty( $locations['footer_navigation'] )
    ? ( wp_get_nav_menu_items( (int) $locations['footer_navigation'] ) ? wp_get_nav_menu_items( (int) $locations['footer_navigation'] ) : array() )
    : array();
?>

	</main>

	<footer role="contentinfo" class="site-footer">
		<div class="container mx-auto py-12">
			<div class="flex flex-col-reverse lg:flex-row justify-between gap-8">
				<div class="site-footer__brand max-w-[40ch]">
					<h2 class="site-footer__title">
						<a href="<?php echo esc_url( home_url( '/' ) ); ?>">
							<?php echo esc_html( get_bloginfo( 'name' ) ); ?>
						</a>
					</h2>

                    <div class="social-links">
                        <?php
                        get_template_part(
                            'views/partials/social-media',
                            null,
                            array(
                                'circle'  => false,
                                'classes' => 'social-icons p-0 mr-1',
                            )
                        );
                        ?>
                    </div>
				</div>

				<?php
				for ( $i = 1; $i <= 3; $i++ ) :
					$area_id = 'footer-' . $i;
					if ( ! is_active_sidebar( $area_id ) ) {
						continue;
					}
					?>
					<div class="site-footer__area">
						<?php dynamic_sidebar( $area_id ); ?>
					</div>
				<?php endfor; ?>

				<?php if ( $footerNav ) : ?>
					<nav class="site-footer__nav" aria-label="<?php echo esc_attr__( 'Footer navigation', 'ks-portfolio' ); ?>">
						<h3><?php echo esc_html__( 'Quick Links', 'ks-portfolio' ); ?></h3>
						<ul>
							<?php
							// Build a tree of parent -> children relationships
							$menu_items_by_parent = array();
							foreach ( $footerNav as $item ) {
								$parent_id = (int) $item->menu_item_parent;
								if ( ! isset( $menu_items_by_parent[ $parent_id ] ) ) {
									$menu_items_by_parent[ $parent_id ] = array();
								}
								$menu_items_by_parent[ $parent_id ][] = $item;
							}

							// Display top-level items (parent = 0)
							if ( isset( $menu_items_by_parent[0] ) ) {
								foreach ( $menu_items_by_parent[0] as $item ) :
									// Check if this is the contact link
									$is_contact = false;
									if ( function_exists( 'get_field' ) ) {
										$contact_link = get_field( 'contact_info.email', 'option' );
										if ( $contact_link && strpos( $item->url, 'contact' ) !== false ) {
											$is_contact = true;
										}
									}
									?>
									<li<?php echo $is_contact ? ' class="site-footer__nav-contact"' : ''; ?>>
										<a href="<?php echo esc_url( $item->url ); ?>" class="<?php echo $is_contact ? 'button button-primary' : ''; ?>">
											<?php echo esc_html( $item->title ); ?>
										</a>

										<?php
										// Check for child items
										$item_id = (int) $item->ID;
										if ( isset( $menu_items_by_parent[ $item_id ] ) ) {
											?>
											<ul class="site-footer__nav-submenu">
												<?php foreach ( $menu_items_by_parent[ $item_id ] as $child_item ) : ?>
													<li class="site-footer__nav-submenu-item">
														<a href="<?php echo esc_url( $child_item->url ); ?>">
															<?php echo esc_html( $child_item->title ); ?>
														</a>
													</li>
												<?php endforeach; ?>
											</ul>
											<?php
										}
										?>
									</li>
                                    <?php
                                endforeach;
							}
							?>
						</ul>
					</nav>
				<?php endif; ?>
			</div>
		</div>

		<div class="site-footer__copyright">
			<?php echo wp_kses_post( $copyright ); ?>
		</div>
	</footer>

	<?php wp_footer(); ?>
</body>
</html>
