<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }

function lt_theme_preset_values( $preset ) {
    $sets = array(
        'classic'=>array(
            'primary'=>'#064559','secondary'=>'#102B3A','accent'=>'#BB8031','gold'=>'#BB8031','heading'=>'#064559','text'=>'#20272B','muted'=>'#667078','background'=>'#FFFFFF','alternate'=>'#FAF8F4','dark'=>'#102B3A','border'=>'#E6E1D8','footer'=>'#102B3A','footer_text'=>'#DCE4E7',
            'heading_font'=>'Georgia, "Times New Roman", serif','body_font'=>'Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif','header_layout'=>'logo-left','footer_layout'=>'columns','h1_size'=>64,'h2_size'=>46,'h3_size'=>28,'heading_weight'=>500,'body_line'=>1.65,'nav_size'=>14,'nav_weight'=>600,'nav_letter'=>0.25,'nav_transform'=>'none','button_size'=>13,'button_weight'=>600,'button_letter'=>0.25,'button_transform'=>'none','header_height'=>80,'section_y'=>78,'content_width'=>1160,'grid_gap'=>32,'hero_desktop_height'=>600,'hero_mobile_height'=>560,'radius'=>0,'card_shadow'=>'none','card_hover'=>'image'
        ),
        'modern'=>array(
            'primary'=>'#123B46','secondary'=>'#18313A','accent'=>'#B58A52','gold'=>'#B58A52','heading'=>'#123B46','text'=>'#242A2D','muted'=>'#667177','background'=>'#FFFFFF','alternate'=>'#F5F7F6','dark'=>'#18313A','border'=>'#DDE3E1',
            'heading_font'=>'Georgia, "Times New Roman", serif','body_font'=>'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif','header_layout'=>'menu-center','footer_layout'=>'minimal','radius'=>2,'card_shadow'=>'none','card_hover'=>'lift'
        ),
        'executive'=>array(
            'primary'=>'#0E2637','secondary'=>'#091C29','accent'=>'#C19A5B','gold'=>'#C19A5B','heading'=>'#0E2637','text'=>'#20272C','muted'=>'#68727B','background'=>'#FFFFFF','alternate'=>'#F3F2EE','dark'=>'#091C29','border'=>'#D9D8D3',
            'heading_font'=>'Georgia, "Times New Roman", serif','body_font'=>'Arial, Helvetica, sans-serif','header_layout'=>'topbar','footer_layout'=>'editorial','radius'=>0,'card_shadow'=>'subtle','card_hover'=>'border'
        ),
        'editorial'=>array(
            'primary'=>'#173E45','secondary'=>'#263135','accent'=>'#A87637','gold'=>'#A87637','heading'=>'#18282D','text'=>'#2D3336','muted'=>'#70777A','background'=>'#FFFDFC','alternate'=>'#F7F4EF','dark'=>'#263135','border'=>'#E5DFD5',
            'heading_font'=>'Georgia, "Times New Roman", serif','body_font'=>'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif','header_layout'=>'centered','footer_layout'=>'editorial','h1_size'=>82,'h2_size'=>60,'section_y'=>104,'radius'=>0,'card_shadow'=>'none','card_hover'=>'none'
        ),
        'international'=>array(
            'primary'=>'#17344B','secondary'=>'#0E2233','accent'=>'#B18A54','gold'=>'#B18A54','heading'=>'#17344B','text'=>'#22292E','muted'=>'#64707A','background'=>'#FFFFFF','alternate'=>'#F5F6F7','dark'=>'#0E2233','border'=>'#DDE1E4',
            'heading_font'=>'Georgia, "Times New Roman", serif','body_font'=>'Arial, Helvetica, sans-serif','header_layout'=>'logo-left','footer_layout'=>'columns','content_width'=>1240,'wide_width'=>1360,'radius'=>0,'card_shadow'=>'none','card_hover'=>'border'
        ),
    );
    return apply_filters( 'lt_theme_preset_values', $sets[ $preset ] ?? $sets['classic'], $preset );
}

function lt_theme_apply_preset() {
    if ( ! current_user_can( 'edit_theme_options' ) ) { wp_die( esc_html__( 'You are not allowed to apply design presets.', 'law-temple-theme' ) ); }
    $preset = sanitize_key( $_GET['preset'] ?? 'classic' );
    check_admin_referer( 'lt_theme_apply_preset_' . $preset );
    $current = lt_theme_options();
    update_option( 'lt_theme_options_backup', $current, false );
    $current['preset'] = $preset;
    $current = array_merge( $current, lt_theme_preset_values( $preset ) );
    update_option( 'lt_theme_options_v3', lt_theme_sanitize_options( $current ), false );
    wp_safe_redirect( add_query_arg( 'lt_preset_applied', $preset, lt_theme_options_admin_url( 'branding' ) ) );
    exit;
}
add_action( 'admin_post_lt_theme_apply_preset', 'lt_theme_apply_preset' );

function lt_theme_preset_apply_url( $preset ) {
    return wp_nonce_url( admin_url( 'admin-post.php?action=lt_theme_apply_preset&preset=' . sanitize_key( $preset ) ), 'lt_theme_apply_preset_' . sanitize_key( $preset ) );
}

function lt_theme_admin_header_preview() {
    $layout = lt_theme_option( 'header_layout', 'logo-left' );
    echo '<section class="lt-admin-preview-block"><div class="lt-admin-preview-head"><div><h2>' . esc_html__( 'Header preview', 'law-temple-theme' ) . '</h2><p>' . esc_html__( 'A live administrative approximation of the selected header layout and colours.', 'law-temple-theme' ) . '</p></div><span class="lt-preview-badge">' . esc_html( $layout ) . '</span></div>';
    echo '<div class="lt-header-preview" data-lt-header-preview data-layout="' . esc_attr( $layout ) . '" style="--preview-header:' . esc_attr( lt_theme_option('header') ) . ';--preview-text:' . esc_attr( lt_theme_option('header_text') ) . ';--preview-accent:' . esc_attr( lt_theme_option('accent') ) . '">';
    echo '<div class="lt-header-preview-top"' . ( lt_theme_option('show_topbar') ? '' : ' hidden' ) . '><span>' . esc_html( lt_theme_option('topbar_text') ?: __( 'Telephone · Email · Office', 'law-temple-theme' ) ) . '</span></div>';
    echo '<div class="lt-header-preview-main"><div class="lt-header-preview-logo">' . esc_html__( 'LAW FIRM', 'law-temple-theme' ) . '</div><div class="lt-header-preview-nav"><span>' . esc_html__( 'About', 'law-temple-theme' ) . '</span><span>' . esc_html__( 'Expertise', 'law-temple-theme' ) . '</span><span>' . esc_html__( 'People', 'law-temple-theme' ) . '</span><span>' . esc_html__( 'Insights', 'law-temple-theme' ) . '</span></div><div class="lt-header-preview-cta">' . esc_html( lt_theme_option('header_cta_label') ?: __( 'Enquiry', 'law-temple-theme' ) ) . '</div></div></div></section>';
}

function lt_theme_admin_footer_preview() {
    $layout = lt_theme_option( 'footer_layout', 'columns' );
    echo '<section class="lt-admin-preview-block"><div class="lt-admin-preview-head"><div><h2>' . esc_html__( 'Footer preview', 'law-temple-theme' ) . '</h2><p>' . esc_html__( 'Preview the broad structure while retaining native footer menus and widget areas.', 'law-temple-theme' ) . '</p></div><span class="lt-preview-badge">' . esc_html( $layout ) . '</span></div>';
    echo '<div class="lt-footer-preview" data-lt-footer-preview data-layout="' . esc_attr( $layout ) . '" style="--preview-footer:' . esc_attr( lt_theme_option('footer') ) . ';--preview-footer-text:' . esc_attr( lt_theme_option('footer_text') ) . ';--preview-accent:' . esc_attr( lt_theme_option('accent') ) . '">';
    echo '<div class="lt-footer-preview-brand"><strong>' . esc_html__( 'LAW FIRM', 'law-temple-theme' ) . '</strong><p>' . esc_html( lt_theme_option('footer_description') ?: __( 'Professional legal counsel and representation.', 'law-temple-theme' ) ) . '</p></div><div><strong>' . esc_html__( 'Firm', 'law-temple-theme' ) . '</strong><span>About</span><span>People</span><span>Careers</span></div><div><strong>' . esc_html__( 'Expertise', 'law-temple-theme' ) . '</strong><span>Practice Areas</span><span>Sectors</span><span>Insights</span></div><div><strong>' . esc_html__( 'Contact', 'law-temple-theme' ) . '</strong><span>Office</span><span>Email</span><span>Telephone</span></div></div></section>';
}

function lt_theme_section_admin_meta( $key ) {
    $meta = array(
        'hero'=>array('dashicons-format-image',__('Hero slides and campaign banners','law-temple-theme')),
        'intro'=>array('dashicons-align-wide',__('Firm introduction and positioning','law-temple-theme')),
        'practices'=>array('dashicons-portfolio',__('Featured Practice Areas','law-temple-theme')),
        'sectors'=>array('dashicons-building',__('Industries and sectors served','law-temple-theme')),
        'people'=>array('dashicons-groups',__('Featured lawyers and professionals','law-temple-theme')),
        'values'=>array('dashicons-awards',__('Values and differentiators','law-temple-theme')),
        'matters'=>array('dashicons-analytics',__('Representative experience','law-temple-theme')),
        'testimonials'=>array('dashicons-format-quote',__('Approved client testimonials','law-temple-theme')),
        'awards'=>array('dashicons-star-filled',__('Awards and recognition','law-temple-theme')),
        'insights'=>array('dashicons-media-document',__('Articles, alerts and publications','law-temple-theme')),
        'offices'=>array('dashicons-location-alt',__('Office locations','law-temple-theme')),
        'careers'=>array('dashicons-id',__('Current opportunities','law-temple-theme')),
        'faq'=>array('dashicons-editor-help',__('Frequently asked questions','law-temple-theme')),
        'custom'=>array('dashicons-block-default',__('Homepage Gutenberg content','law-temple-theme')),
        'contact'=>array('dashicons-email-alt',__('Global contact call to action','law-temple-theme')),
        'biography'=>array('dashicons-id-alt',__('Biography section','law-temple-theme')),
        'practice'=>array('dashicons-portfolio',__('Related Practice Areas','law-temple-theme')),
        'sector'=>array('dashicons-building',__('Related Sectors','law-temple-theme')),
        'experience'=>array('dashicons-analytics',__('Experience narrative','law-temple-theme')),
        'qualifications'=>array('dashicons-welcome-learn-more',__('Professional qualifications','law-temple-theme')),
        'education'=>array('dashicons-welcome-learn-more',__('Education','law-temple-theme')),
        'bar'=>array('dashicons-yes-alt',__('Bar admissions','law-temple-theme')),
        'memberships'=>array('dashicons-groups',__('Professional memberships','law-temple-theme')),
        'languages'=>array('dashicons-translation',__('Languages','law-temple-theme')),
        'downloads'=>array('dashicons-download',__('CV and vCard downloads','law-temple-theme')),
        'contact'=>array('dashicons-email-alt',__('Contact details / CTA','law-temple-theme')),
    );
    return $meta[$key] ?? array('dashicons-admin-generic',__('Configurable section','law-temple-theme'));
}

function lt_theme_post_multiselect_field( $key, $label, $post_type ) {
    $value = lt_theme_option( $key );
    $selected = function_exists( 'lt_core_csv_ids' ) ? lt_core_csv_ids( $value ) : array_values( array_filter( array_map( 'absint', explode( ',', (string) $value ) ) ) );
    $items = array();
    if ( $selected ) {
        $items = get_posts( array( 'post_type'=>$post_type, 'post_status'=>array('publish','draft','private'), 'post__in'=>$selected, 'posts_per_page'=>count($selected), 'orderby'=>'post__in' ) );
    }
    $name = 'lt_theme_options_v3[' . $key . '][]';
    echo '<div class="lt-option-row"><label for="ltopt_' . esc_attr( $key ) . '"><strong>' . esc_html( $label ) . '</strong><span class="description">' . esc_html__( 'Search and select specific items. Leave empty to use the normal featured/order query.', 'law-temple-theme' ) . '</span></label><div class="lt-theme-ajax-relation" data-post-type="' . esc_attr( $post_type ) . '">';
    echo '<input type="search" class="regular-text lt-theme-relation-search" placeholder="' . esc_attr__( 'Search by title…', 'law-temple-theme' ) . '" autocomplete="off"><div class="lt-theme-relation-results" hidden></div>';
    echo '<select multiple size="7" class="lt-theme-relation-select" id="ltopt_' . esc_attr( $key ) . '" name="' . esc_attr( $name ) . '">';
    foreach ( $items as $item ) { echo '<option value="' . esc_attr( $item->ID ) . '" selected>' . esc_html( $item->post_title ) . '</option>'; }
    echo '</select><p class="description">' . esc_html__( 'Click search results to add them. Use Ctrl/Cmd or Shift in the selected list to remove or adjust selections.', 'law-temple-theme' ) . '</p></div></div>';
}
