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

/** Visual preview and quick actions for hero slides. */
function lt_core_add_hero_preview_box() {
    add_meta_box(
        'lt-hero-preview',
        __( 'Hero Visual Preview', 'law-temple-core' ),
        'lt_core_render_hero_preview_box',
        'hero_slide',
        'normal',
        'high'
    );
}
add_action( 'add_meta_boxes_hero_slide', 'lt_core_add_hero_preview_box' );

function lt_core_render_hero_preview_box( $post ) {
    $layout = get_post_meta( $post->ID, 'lt_hero_layout', true ) ?: 'split';
    $eyebrow = get_post_meta( $post->ID, 'lt_eyebrow', true );
    $heading = $post->post_title;
    $description = $post->post_excerpt;
    $bg = get_post_meta( $post->ID, 'lt_background_color', true ) ?: '#FBFAF7';
    $text = get_post_meta( $post->ID, 'lt_text_color', true ) ?: '#064559';
    $overlay = max( 0, min( 100, absint( get_post_meta( $post->ID, 'lt_overlay', true ) ) ) );
    $desktop = get_the_post_thumbnail_url( $post->ID, 'large' );
    $mobile_id = absint( get_post_meta( $post->ID, 'lt_mobile_image_id', true ) );
    $mobile = $mobile_id ? wp_get_attachment_image_url( $mobile_id, 'large' ) : $desktop;
    $primary = get_post_meta( $post->ID, 'lt_primary_label', true );
    $secondary = get_post_meta( $post->ID, 'lt_secondary_label', true );

    echo '<div class="lt-hero-preview-tools" role="group" aria-label="' . esc_attr__( 'Preview size', 'law-temple-core' ) . '">';
    echo '<button type="button" class="button button-secondary is-active" data-lt-hero-device="desktop">' . esc_html__( 'Desktop', 'law-temple-core' ) . '</button> ';
    echo '<button type="button" class="button button-secondary" data-lt-hero-device="mobile">' . esc_html__( 'Mobile', 'law-temple-core' ) . '</button>';
    echo '</div>';
    echo '<div class="lt-hero-preview-shell" data-lt-hero-preview data-layout="' . esc_attr( $layout ) . '" style="--lt-preview-bg:' . esc_attr( $bg ) . ';--lt-preview-text:' . esc_attr( $text ) . ';--lt-preview-overlay:' . esc_attr( $overlay / 100 ) . '">';
    echo '<div class="lt-hero-preview-media" data-desktop-image="' . esc_url( $desktop ?: '' ) . '" data-mobile-image="' . esc_url( $mobile ?: '' ) . '"' . ( $desktop ? ' style="background-image:url(' . esc_url( $desktop ) . ')"' : '' ) . '></div>';
    echo '<div class="lt-hero-preview-overlay"></div><div class="lt-hero-preview-content">';
    echo '<span class="lt-hero-preview-eyebrow">' . esc_html( $eyebrow ) . '</span>';
    echo '<strong class="lt-hero-preview-heading">' . esc_html( $heading ?: __( 'Hero heading', 'law-temple-core' ) ) . '</strong>';
    echo '<p class="lt-hero-preview-description">' . esc_html( $description ) . '</p>';
    echo '<div class="lt-hero-preview-actions"><span class="lt-preview-button lt-preview-primary">' . esc_html( $primary ?: __( 'Primary CTA', 'law-temple-core' ) ) . '</span><span class="lt-preview-button lt-preview-secondary">' . esc_html( $secondary ?: __( 'Secondary CTA', 'law-temple-core' ) ) . '</span></div>';
    echo '</div></div>';
    echo '<p class="description">' . esc_html__( 'This preview updates while you edit the slide. It is an administrative approximation; the saved frontend theme remains authoritative.', 'law-temple-core' ) . '</p>';
}

function lt_core_duplicate_hero_slide() {
    if ( ! current_user_can( 'edit_lt_hero_slides' ) && ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to duplicate hero slides.', 'law-temple-core' ) ); }
    $id = absint( $_GET['slide'] ?? 0 );
    check_admin_referer( 'lt_duplicate_hero_' . $id );
    $post = get_post( $id );
    if ( ! $post || 'hero_slide' !== $post->post_type || ! current_user_can( 'edit_post', $id ) ) { wp_die( esc_html__( 'Invalid hero slide.', 'law-temple-core' ) ); }
    $new_id = wp_insert_post( array(
        'post_type'    => 'hero_slide',
        'post_status'  => 'draft',
        'post_title'   => sprintf( __( '%s (Copy)', 'law-temple-core' ), $post->post_title ),
        'post_excerpt' => $post->post_excerpt,
        'post_content' => $post->post_content,
        'menu_order'   => $post->menu_order + 1,
    ), true );
    if ( ! is_wp_error( $new_id ) ) {
        foreach ( lt_core_meta_schema()['hero_slide'] ?? array() as $key => $field ) {
            $value = get_post_meta( $id, $key, true );
            if ( '' !== $value ) { update_post_meta( $new_id, $key, $value ); }
        }
        $thumb = get_post_thumbnail_id( $id );
        if ( $thumb ) { set_post_thumbnail( $new_id, $thumb ); }
    }
    wp_safe_redirect( admin_url( 'admin.php?page=lt-hero-manager' ) );
    exit;
}
add_action( 'admin_post_lt_duplicate_hero', 'lt_core_duplicate_hero_slide' );

function lt_core_toggle_hero_slide() {
    if ( ! current_user_can( 'edit_lt_hero_slides' ) && ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to modify hero slides.', 'law-temple-core' ) ); }
    $id = absint( $_GET['slide'] ?? 0 );
    check_admin_referer( 'lt_toggle_hero_' . $id );
    if ( 'hero_slide' !== get_post_type( $id ) || ! current_user_can( 'edit_post', $id ) ) { wp_die( esc_html__( 'Invalid hero slide.', 'law-temple-core' ) ); }
    $enabled = (bool) get_post_meta( $id, 'lt_enabled', true );
    update_post_meta( $id, 'lt_enabled', $enabled ? 0 : 1 );
    wp_safe_redirect( admin_url( 'admin.php?page=lt-hero-manager' ) );
    exit;
}
add_action( 'admin_post_lt_toggle_hero', 'lt_core_toggle_hero_slide' );
