<?php
/**
 * File Path: includes/metaboxes/book-header/class-mbs-book-header-core-render.php
 *
 * Book Header Child Section — Core Fields Render Class
 *
 * @package    My_Book_Showroom
 * @subpackage Includes/Metaboxes/BookHeader
 * @since      1.0.0
 */

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

// phpcs:disable WordPress.Security.NonceVerification -- nonce verified in parent class verify_save() / check_admin_referer() before $_POST access; PHPCS cannot trace cross-method nonce verification
class MBS_Book_Header_Core_Render { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

    public static function render( $post ) {
        // Fall back to post_title for books that were titled via the WP title bar before
        // bidirectional sync was introduced (D-5 deferred, now implemented).
        $book_title      = get_post_meta( $post->ID, 'mbs_book_title', true );
        if ( '' === $book_title ) {
            $book_title = $post->post_title;
        }
        $publisher_url   = get_post_meta( $post->ID, 'mbs_publisher_url', true );
        $teaser          = get_post_meta( $post->ID, 'mbs_teaser', true );
        $tagline         = get_post_meta( $post->ID, 'mbs_tagline', true );
        $synopsis        = get_post_meta( $post->ID, 'mbs_synopsis', true );
        ?>
        <!-- Core Fields -->
        <tr>
            <th scope="row">
                <label for="mbs_book_title"><?php esc_html_e( 'Book Title', 'my-book-showroom' ); ?></label>
            </th>
            <td>
                <?php
                // SV-11 / D-5: mbs_book_title is the canonical display title (templates read this).
                // On save it is synced → wp_posts.post_title so the admin list and permalink stay correct.
                // The WP "Add title" bar is hidden on the book editor screen (class-mbs-admin-assets.php).
                ?>
                <input type="text" id="mbs_book_title" name="mbs_book_title" class="regular-text"
                       value="<?php echo esc_attr( $book_title ); ?>"
                       autocomplete="off" />
            </td>
        </tr>

        <tr>
            <th scope="row">
                <label for="mbs_publisher_url"><?php esc_html_e( 'Publisher URL', 'my-book-showroom' ); ?></label>
            </th>
            <td>
                <input type="url" id="mbs_publisher_url" name="mbs_publisher_url" class="regular-text"
                       value="<?php echo esc_attr( $publisher_url ); ?>"
                       placeholder="https://publisherwebsite.com" />
                <p class="description"><?php esc_html_e( 'Optional. When set, the publisher name becomes a link on the book page.', 'my-book-showroom' ); ?></p>
            </td>
        </tr>

        <tr>
            <th scope="row">
                <label for="mbs_tagline"><?php esc_html_e( 'Tagline', 'my-book-showroom' ); ?></label>
            </th>
            <td>
                <input type="text" id="mbs_tagline" name="mbs_tagline" class="large-text"
                       value="<?php echo esc_attr( $tagline ); ?>"
                       placeholder="<?php esc_attr_e( 'A one-sentence marketing hook for distributor metadata and the Book Bible.', 'my-book-showroom' ); ?>" />
                <p class="description"><?php esc_html_e( 'Keep it punchy and under 150 characters. This is not the blurb &mdash; it\'s the one line that makes a reader want to read the blurb.', 'my-book-showroom' ); ?></p>
            </td>
        </tr>

        <tr>
            <th scope="row">
                <span class="mbs-label-tip-pair"><label for="mbs_teaser"><?php esc_html_e( 'Teaser / Short Description', 'my-book-showroom' ); ?></label><?php mbs_help_tip(
                    __( 'The Teaser is a short marketing excerpt — typically two to four sentences — used in places where the full description would be too long, such as book cards, search results, and social sharing previews. It is separate from the main description (the WordPress post content) and from the Tagline. If left blank, MBS falls back to a trimmed version of the main description.', 'my-book-showroom' ),
                    __( 'About Teaser / Short Description', 'my-book-showroom' ),
                    'mbs-help-tip--wide'
                ); ?></span>
            </th>
            <td>
                <textarea id="mbs_teaser" name="mbs_teaser" rows="4" class="large-text"><?php echo esc_textarea( $teaser ); ?></textarea>
            </td>
        </tr>

        <?php /* ── Back Cover / Flap Copy (PRO) — ONIX TextType 23 ───────────────── */ ?>
        <tr>
            <th scope="row">
                <?php if ( mbs_is_pro() ) : ?>
                <label for="mbs_synopsis"><?php esc_html_e( 'Back Cover / Flap Copy', 'my-book-showroom' ); ?></label>
                <?php else : ?>
                <?php esc_html_e( 'Back Cover / Flap Copy', 'my-book-showroom' ); ?>
                <?php endif; ?>
            </th>
            <td>
                <?php if ( mbs_is_pro() ) : ?>
                <textarea id="mbs_synopsis" name="mbs_synopsis" rows="6" class="large-text"><?php echo esc_textarea( $synopsis ); ?></textarea>
                <p class="description"><?php esc_html_e( 'Polished back cover or flap copy — distinct from the main description above. Exported to ONIX as TextType 23.', 'my-book-showroom' ); ?></p>
                <?php else : ?>
                <!-- MBS: Back Cover / Flap Copy requires PRO license -->
                <p class="description"><?php esc_html_e( 'PRO feature: back cover / flap copy, exported to ONIX as TextType 23.', 'my-book-showroom' ); ?></p>
                <?php endif; ?>
            </td>
        </tr>

        <?php
    }

    /**
     * Display an admin error notice if the series field rejected a crafted multi-value submission.
     *
     * Uses a short-lived transient keyed on post_id so the message survives the save_post redirect.
     */
    public static function maybe_show_series_error(): void {
        $screen = get_current_screen();
        if ( ! $screen || MBS_POST_TYPE !== $screen->post_type ) {
            return;
        }
        $post_id = isset( $_GET['post'] ) ? (int) $_GET['post'] : 0;
        if ( ! $post_id ) {
            return;
        }
        if ( get_transient( 'mbs_series_save_error_' . $post_id ) ) {
            delete_transient( 'mbs_series_save_error_' . $post_id );
            echo '<div class="notice notice-error is-dismissible"><p>';
            esc_html_e( 'Series assignment error: only one series may be assigned per book. The series field was not saved.', 'my-book-showroom' );
            echo '</p></div>';
        }
    }
}
