<?php
/**
 * Premium demo polish and modern editor support for MoneySite.
 *
 * @package moneysite
 */

function moneysite_sanitize_checkbox( $checked ) {
	return ( isset( $checked ) && true == $checked );
}

function moneysite_premium_customize_register( $wp_customize ) {
	$wp_customize->add_panel(
		'moneysite_premium_options',
		array(
			'priority'   => 40,
			'capability' => 'edit_theme_options',
			'title'      => esc_html__( 'Premium Demo Options', 'moneysite' ),
		)
	);

	$wp_customize->add_section(
		'moneysite_premium_hero',
		array(
			'title' => esc_html__( 'Homepage Hero', 'moneysite' ),
			'panel' => 'moneysite_premium_options',
		)
	);

	$wp_customize->add_setting(
		'moneysite_hero_title',
		array(
			'default'           => esc_html__( 'MoneySite Finance Demo', 'moneysite' ),
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'moneysite_hero_title',
		array(
			'label'   => esc_html__( 'Hero Title', 'moneysite' ),
			'section' => 'moneysite_premium_hero',
			'type'    => 'text',
		)
	);

	$wp_customize->add_setting(
		'moneysite_hero_text',
		array(
			'default'           => esc_html__( 'A premium testing demo for credit, banking, investing, money management, comparison tables, nested menus, comments, widgets, and formatting.', 'moneysite' ),
			'sanitize_callback' => 'sanitize_textarea_field',
		)
	);

	$wp_customize->add_control(
		'moneysite_hero_text',
		array(
			'label'   => esc_html__( 'Hero Text', 'moneysite' ),
			'section' => 'moneysite_premium_hero',
			'type'    => 'textarea',
		)
	);

	$wp_customize->add_setting(
		'moneysite_hero_button_text',
		array(
			'default'           => esc_html__( 'Explore Money Guides', 'moneysite' ),
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'moneysite_hero_button_text',
		array(
			'label'   => esc_html__( 'Button Text', 'moneysite' ),
			'section' => 'moneysite_premium_hero',
			'type'    => 'text',
		)
	);

	$wp_customize->add_setting(
		'moneysite_hero_button_url',
		array(
			'default'           => home_url( '/category/money-guides/' ),
			'sanitize_callback' => 'esc_url_raw',
		)
	);

	$wp_customize->add_control(
		'moneysite_hero_button_url',
		array(
			'label'   => esc_html__( 'Button URL', 'moneysite' ),
			'section' => 'moneysite_premium_hero',
			'type'    => 'url',
		)
	);

	$wp_customize->add_section(
		'moneysite_premium_design',
		array(
			'title' => esc_html__( 'Premium Design', 'moneysite' ),
			'panel' => 'moneysite_premium_options',
		)
	);

	$wp_customize->add_setting(
		'moneysite_accent_color',
		array(
			'default'           => '#0f766e',
			'sanitize_callback' => 'sanitize_hex_color',
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'moneysite_accent_color',
			array(
				'label'   => esc_html__( 'Accent Color', 'moneysite' ),
				'section' => 'moneysite_premium_design',
			)
		)
	);

	$wp_customize->add_setting(
		'moneysite_card_radius',
		array(
			'default'           => 12,
			'sanitize_callback' => 'absint',
		)
	);

	$wp_customize->add_control(
		'moneysite_card_radius',
		array(
			'label'       => esc_html__( 'Card Radius', 'moneysite' ),
			'section'     => 'moneysite_premium_design',
			'type'        => 'number',
			'input_attrs' => array(
				'min' => 0,
				'max' => 24,
			),
		)
	);

	$wp_customize->add_setting(
		'moneysite_sidebar_heading_size',
		array(
			'default'           => 15,
			'sanitize_callback' => 'absint',
		)
	);

	$wp_customize->add_control(
		'moneysite_sidebar_heading_size',
		array(
			'label'       => esc_html__( 'Sidebar Heading Size', 'moneysite' ),
			'section'     => 'moneysite_premium_design',
			'type'        => 'number',
			'input_attrs' => array(
				'min' => 12,
				'max' => 24,
			),
		)
	);

	$wp_customize->add_setting(
		'moneysite_footer_credit_nofollow',
		array(
			'default'           => true,
			'sanitize_callback' => 'moneysite_sanitize_checkbox',
		)
	);

	$wp_customize->add_control(
		'moneysite_footer_credit_nofollow',
		array(
			'label'   => esc_html__( 'Add nofollow to footer credit', 'moneysite' ),
			'section' => 'moneysite_premium_design',
			'type'    => 'checkbox',
		)
	);
}
add_action( 'customize_register', 'moneysite_premium_customize_register' );

function moneysite_home_hero() {
	$title       = get_theme_mod( 'moneysite_hero_title', esc_html__( 'MoneySite Finance Demo', 'moneysite' ) );
	$text        = get_theme_mod( 'moneysite_hero_text', esc_html__( 'A premium testing demo for credit, banking, investing, money management, comparison tables, nested menus, comments, widgets, and formatting.', 'moneysite' ) );
	$button_text = get_theme_mod( 'moneysite_hero_button_text', esc_html__( 'Explore Money Guides', 'moneysite' ) );
	$button_url  = get_theme_mod( 'moneysite_hero_button_url', home_url( '/category/money-guides/' ) );
	?>
	<section class="moneysite-hero">
		<div class="container">
			<div class="moneysite-hero-grid">
				<div class="moneysite-hero-copy">
					<span class="moneysite-kicker"><?php esc_html_e( 'Finance Theme Testing Lab', 'moneysite' ); ?></span>
					<h1><?php echo esc_html( $title ); ?></h1>
					<p><?php echo esc_html( $text ); ?></p>
					<div class="moneysite-hero-actions">
						<a class="btn btn-tislider-two" href="<?php echo esc_url( $button_url ); ?>"><?php echo esc_html( $button_text ); ?></a>
						<a class="moneysite-secondary-link" href="<?php echo esc_url( home_url( '/formatting-lab/' ) ); ?>"><?php esc_html_e( 'View Formatting Lab', 'moneysite' ); ?></a>
					</div>
				</div>
				<div class="moneysite-hero-card" role="img" aria-label="<?php esc_attr_e( 'Finance dashboard illustration', 'moneysite' ); ?>">
					<div class="moneysite-chart-card">
						<span></span><span></span><span></span><span></span><span></span>
					</div>
					<ul>
						<li><?php esc_html_e( 'Credit score basics', 'moneysite' ); ?></li>
						<li><?php esc_html_e( 'Budget planning widgets', 'moneysite' ); ?></li>
						<li><?php esc_html_e( 'Nested money menus', 'moneysite' ); ?></li>
					</ul>
				</div>
			</div>
		</div>
	</section>
	<?php
}
add_action( 'moneysite_home_hero', 'moneysite_home_hero' );

function moneysite_premium_inline_styles() {
	$accent               = get_theme_mod( 'moneysite_accent_color', '#0f766e' );
	$radius               = absint( get_theme_mod( 'moneysite_card_radius', 12 ) );
	$sidebar_heading_size = absint( get_theme_mod( 'moneysite_sidebar_heading_size', 15 ) );

	$css = '
	:root {
		--moneysite-accent: ' . esc_html( $accent ) . ';
		--moneysite-radius: ' . esc_html( $radius ) . 'px;
	}
	.ms-sidebar .ms-widget h6,
	footer .ms-widget h6 { font-size: ' . esc_html( $sidebar_heading_size ) . 'px; }
	';

	wp_add_inline_style( 'moneysite-style', $css );
}
add_action( 'wp_enqueue_scripts', 'moneysite_premium_inline_styles', 20 );

function moneysite_register_block_assets() {
	if ( function_exists( 'register_block_style' ) ) {
		register_block_style(
			'core/quote',
			array(
				'name'         => 'moneysite-market-note',
				'label'        => esc_html__( 'Market Note', 'moneysite' ),
				'inline_style' => '.is-style-moneysite-market-note{border-left:4px solid #0f766e;background:#f0fdfa;padding:18px 22px;}',
			)
		);

		register_block_style(
			'core/table',
			array(
				'name'         => 'moneysite-rates',
				'label'        => esc_html__( 'Rate Table', 'moneysite' ),
				'inline_style' => '.is-style-moneysite-rates table{border-collapse:separate;border-spacing:0;box-shadow:0 12px 35px rgba(15,23,42,.1);}.is-style-moneysite-rates th{background:#0f172a;color:#fff;}',
			)
		);
	}

	if ( function_exists( 'register_block_pattern' ) ) {
		register_block_pattern(
			'moneysite/rate-comparison',
			array(
				'title'       => esc_html__( 'Rate Comparison', 'moneysite' ),
				'description' => esc_html__( 'A finance comparison block for cards, banks, or savings products.', 'moneysite' ),
				'categories'  => array( 'featured', 'text' ),
				'content'     => '<!-- wp:group {"className":"moneysite-pattern-rate","style":{"spacing":{"padding":{"top":"28px","right":"28px","bottom":"28px","left":"28px"}}},"layout":{"type":"constrained"}} --><div class="wp-block-group moneysite-pattern-rate" style="padding-top:28px;padding-right:28px;padding-bottom:28px;padding-left:28px"><!-- wp:heading --><h2>' . esc_html__( 'Compare the numbers before you decide', 'moneysite' ) . '</h2><!-- /wp:heading --><!-- wp:paragraph --><p>' . esc_html__( 'Use this pattern for credit card APRs, savings rates, banking fees, or personal finance product comparisons.', 'moneysite' ) . '</p><!-- /wp:paragraph --></div><!-- /wp:group -->',
			)
		);
	}
}
add_action( 'init', 'moneysite_register_block_assets' );
