<?php
/**
 * Helper functions for EasySite Pro.
 *
 * @package EasySite_Pro
 */

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

/**
 * Get theme mod with default.
 *
 * @param string $key     Theme mod key.
 * @param mixed  $default Default value.
 * @return mixed
 */
function easysite_get_mod( $key, $default = '' ) {
	return get_theme_mod( $key, $default );
}

/**
 * Get placeholder image URL.
 *
 * @param string $type Image type key.
 * @return string
 */
function easysite_placeholder_image( $type = 'hero' ) {
	$map = array(
		'hero'    => 'placeholder-hero.png',
		'product' => 'placeholder-product.png',
		'team'    => 'placeholder-team.png',
		'service' => 'placeholder-service.png',
		'bg'      => 'placeholder-bg.png',
	);

	$file = isset( $map[ $type ] ) ? $map[ $type ] : $map['hero'];

	return EASYSITE_PRO_URI . '/assets/images/' . $file;
}

/**
 * Get curated font pairs.
 *
 * @return array
 */
function easysite_get_font_pairs() {
	return array(
		'inter-system'   => array(
			'label'    => 'Inter + System UI',
			'heading'  => "'Inter', sans-serif",
			'body'     => "system-ui, -apple-system, sans-serif",
			'google'   => 'Inter:400,600,700',
		),
		'playfair-lato'  => array(
			'label'    => 'Playfair + Lato',
			'heading'  => "'Playfair Display', serif",
			'body'     => "'Lato', sans-serif",
			'google'   => 'Playfair+Display:600,700|Lato:400,700',
		),
		'montserrat-open' => array(
			'label'    => 'Montserrat + Open Sans',
			'heading'  => "'Montserrat', sans-serif",
			'body'     => "'Open Sans', sans-serif",
			'google'   => 'Montserrat:600,700|Open+Sans:400,600',
		),
		'poppins-roboto' => array(
			'label'    => 'Poppins + Roboto',
			'heading'  => "'Poppins', sans-serif",
			'body'     => "'Roboto', sans-serif",
			'google'   => 'Poppins:600,700|Roboto:400,500',
		),
		'merriweather-source' => array(
			'label'    => 'Merriweather + Source Sans',
			'heading'  => "'Merriweather', serif",
			'body'     => "'Source Sans 3', sans-serif",
			'google'   => 'Merriweather:700|Source+Sans+3:400,600',
		),
		'raleway-nunito' => array(
			'label'    => 'Raleway + Nunito',
			'heading'  => "'Raleway', sans-serif",
			'body'     => "'Nunito', sans-serif",
			'google'   => 'Raleway:600,700|Nunito:400,600',
		),
		'oswald-roboto'  => array(
			'label'    => 'Oswald + Roboto',
			'heading'  => "'Oswald', sans-serif",
			'body'     => "'Roboto', sans-serif",
			'google'   => 'Oswald:500,600|Roboto:400,500',
		),
		'libre-work'     => array(
			'label'    => 'Libre Baskerville + Work Sans',
			'heading'  => "'Libre Baskerville', serif",
			'body'     => "'Work Sans', sans-serif",
			'google'   => 'Libre+Baskerville:700|Work+Sans:400,600',
		),
	);
}

/**
 * Get design presets.
 *
 * @return array
 */
function easysite_get_design_presets() {
	return array(
		'modern'  => array(
			'label'       => __( 'Modern', 'easysite-pro' ),
			'main_color'  => '#2563eb',
			'accent_color'=> '#0ea5e9',
			'font_pair'   => 'inter-system',
		),
		'minimal' => array(
			'label'       => __( 'Minimal', 'easysite-pro' ),
			'main_color'  => '#111827',
			'accent_color'=> '#6b7280',
			'font_pair'   => 'playfair-lato',
		),
		'bold'    => array(
			'label'       => __( 'Bold', 'easysite-pro' ),
			'main_color'  => '#dc2626',
			'accent_color'=> '#f59e0b',
			'font_pair'   => 'oswald-roboto',
		),
		'elegant' => array(
			'label'       => __( 'Elegant', 'easysite-pro' ),
			'main_color'  => '#1e3a5f',
			'accent_color'=> '#c9a227',
			'font_pair'   => 'playfair-lato',
		),
		'luxury'  => array(
			'label'       => __( 'Luxury', 'easysite-pro' ),
			'main_color'  => '#0f0f0f',
			'accent_color'=> '#b8860b',
			'font_pair'   => 'libre-work',
		),
		'startup' => array(
			'label'       => __( 'Startup', 'easysite-pro' ),
			'main_color'  => '#7c3aed',
			'accent_color'=> '#06b6d4',
			'font_pair'   => 'poppins-roboto',
		),
		'corporate' => array(
			'label'       => __( 'Corporate', 'easysite-pro' ),
			'main_color'  => '#1d4ed8',
			'accent_color'=> '#64748b',
			'font_pair'   => 'montserrat-open',
		),
	);
}

/**
 * Get starter site definitions.
 *
 * @return array
 */
function easysite_get_starter_sites() {
	$basic = array(
		'business' => array(
			'label'       => __( 'General Business', 'easysite-pro' ),
			'description' => __( 'A friendly starter site for any small business.', 'easysite-pro' ),
			'basic'       => true,
			'pages'       => array( 'home', 'about', 'services', 'contact', 'blog' ),
		),
	);

	if ( ! easysite_is_bundle() ) {
		return $basic;
	}

	return array_merge( $basic, array(
		'shop'        => array(
			'label'       => __( 'Online Shop', 'easysite-pro' ),
			'description' => __( 'Sell products with a ready-made storefront.', 'easysite-pro' ),
			'pages'       => array( 'home', 'about', 'services', 'contact', 'shop', 'blog' ),
		),
		'portfolio'   => array(
			'label'       => __( 'Portfolio', 'easysite-pro' ),
			'description' => __( 'Showcase your creative work.', 'easysite-pro' ),
			'pages'       => array( 'home', 'about', 'services', 'contact', 'blog' ),
		),
		'restaurant'  => array(
			'label'       => __( 'Restaurant', 'easysite-pro' ),
			'description' => __( 'Menu, hours, and reservations ready to customize.', 'easysite-pro' ),
			'pages'       => array( 'home', 'about', 'services', 'contact', 'blog' ),
		),
		'beauty'      => array(
			'label'       => __( 'Hair & Beauty', 'easysite-pro' ),
			'description' => __( 'Salon or spa with booking-friendly layout.', 'easysite-pro' ),
			'pages'       => array( 'home', 'about', 'services', 'contact', 'blog' ),
		),
		'tradesman'   => array(
			'label'       => __( 'Tradesman', 'easysite-pro' ),
			'description' => __( 'Plumber, electrician, or handyman site.', 'easysite-pro' ),
			'pages'       => array( 'home', 'about', 'services', 'contact', 'blog' ),
		),
		'fitness'     => array(
			'label'       => __( 'Fitness / Trainer', 'easysite-pro' ),
			'description' => __( 'Personal trainer or gym website.', 'easysite-pro' ),
			'pages'       => array( 'home', 'about', 'services', 'contact', 'blog' ),
		),
		'coach'       => array(
			'label'       => __( 'Life / Business Coach', 'easysite-pro' ),
			'description' => __( 'Coaching services with booking-friendly layout.', 'easysite-pro' ),
			'pages'       => array( 'home', 'about', 'services', 'contact', 'blog' ),
		),
		'blogger'     => array(
			'label'       => __( 'Blogger / Influencer', 'easysite-pro' ),
			'description' => __( 'Content-first layout with social focus.', 'easysite-pro' ),
			'pages'       => array( 'home', 'about', 'contact', 'blog' ),
		),
		'photography' => array(
			'label'       => __( 'Photography', 'easysite-pro' ),
			'description' => __( 'Visual portfolio for photographers.', 'easysite-pro' ),
			'pages'       => array( 'home', 'about', 'services', 'contact', 'blog' ),
		),
		'realestate'  => array(
			'label'       => __( 'Real Estate', 'easysite-pro' ),
			'description' => __( 'Listings and agent profile layout.', 'easysite-pro' ),
			'pages'       => array( 'home', 'about', 'services', 'contact', 'blog' ),
		),
		'charity'     => array(
			'label'       => __( 'Charity / Non-profit', 'easysite-pro' ),
			'description' => __( 'Donation-friendly nonprofit site.', 'easysite-pro' ),
			'pages'       => array( 'home', 'about', 'services', 'contact', 'blog' ),
		),
	) 	);
}

/**
 * Get white-label / agency brand name for UI.
 *
 * @return string
 */
function easysite_get_brand_name() {
	$agency = easysite_get_mod( 'easysite_agency_name', '' );
	if ( $agency ) {
		return $agency;
	}
	return __( 'EasySite', 'easysite-pro' );
}

/**
 * Get detected industry/starter key for AI prompts.
 *
 * @return string
 */
function easysite_get_industry_key() {
	$options = get_option( 'easysite_onboarding', array() );
	return isset( $options['starter_site'] ) ? sanitize_key( $options['starter_site'] ) : 'business';
}

/**
 * Sanitize hex color.
 *
 * @param string $color Color value.
 * @return string
 */
function easysite_sanitize_hex_color( $color ) {
	if ( empty( $color ) || is_array( $color ) ) {
		return '';
	}
	return sanitize_hex_color( $color ) ? sanitize_hex_color( $color ) : '';
}

/**
 * Output social links.
 */
function easysite_social_links() {
	$networks = array(
		'facebook'  => array( 'label' => 'Facebook', 'icon' => 'facebook' ),
		'instagram' => array( 'label' => 'Instagram', 'icon' => 'instagram' ),
		'twitter'   => array( 'label' => 'X (Twitter)', 'icon' => 'twitter' ),
		'linkedin'  => array( 'label' => 'LinkedIn', 'icon' => 'linkedin' ),
	);

	echo '<ul class="easysite-social-links">';
	foreach ( $networks as $key => $data ) {
		$url = easysite_get_mod( 'easysite_social_' . $key );
		if ( ! $url ) {
			continue;
		}
		printf(
			'<li><a href="%s" target="_blank" rel="noopener noreferrer" aria-label="%s">%s</a></li>',
			esc_url( $url ),
			esc_attr( $data['label'] ),
			esc_html( $data['label'] )
		);
	}
	echo '</ul>';
}

/**
 * Render a section partial.
 *
 * @param string $slug Section slug.
 * @param array  $args Arguments.
 */
function easysite_get_section( $slug, $args = array() ) {
	$template = locate_template( 'template-parts/sections/' . $slug . '.php' );
	if ( ! $template ) {
		$template = EASYSITE_PRO_DIR . '/template-parts/sections/' . $slug . '.php';
	}
	if ( file_exists( $template ) ) {
		load_template( $template, false, $args );
	}
}

/**
 * Check if WooCommerce is active.
 *
 * @return bool
 */
function easysite_wc_active() {
	return class_exists( 'WooCommerce' );
}
