<?php
/**
 * File Path: includes/metaboxes/class-mbs-format-repeater-data.php
 *
 * Format Repeater — Per-Row Data Assembly
 *
 * Provides build_row_context() which converts a raw DB row object (or null for
 * the JS-clone template row) into a flat associative array of all scalar values
 * needed by the MBS_Metabox_Format_Repeater_Render field-group helpers.
 *
 * Keeping data preparation here allows the render class to contain only
 * HTML output logic.
 *
 * Extracts: REF-007 (Session 137).
 *
 * @package MyBookShowroom
 * @since   1.1.0
 */

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

class MBS_Format_Repeater_Data { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	/**
	 * Build the per-row template context array for the format repeater render methods.
	 *
	 * All ternary $is_template checks and null-coalescing fallbacks live here,
	 * keeping the render methods free of data-preparation logic.
	 *
	 * Keys are grouped by the field-group helper that consumes them:
	 *   identity    — is_template, id, format
	 *   visibility  — show_audiobook, show_ebook, show_no_audio  (CSS 'block'/'none')
	 *   core        — format_label … price  (render_row_core_fields)
	 *   format      — page_length … audio_title  (render_row_format_fields)
	 *   media       — cover_image_id … spinning_video_title  (render_row_media_fields)
	 *   supply      — price_type … returnability  (render_row_supply_chain)
	 *   pos         — square_item_id  (render_row_pos_fields)
	 *
	 * @param object|null $row Saved format row object, or null for the JS-clone template.
	 * @return array<string,mixed>
	 */
	public static function build_row_context( ?object $row ): array {
		$is_template = ( null === $row );
		$format      = $is_template ? '' : ( $row->format ?? '' );

		return array(

			// ── Identity ────────────────────────────────────────────────────────
			'is_template'          => $is_template,
			'id'                   => $is_template ? '' : (int) $row->id,
			'format'               => $format,
			'sort_order'           => $is_template ? 0 : (int) ( $row->sort_order ?? 0 ),

			// ── Visibility flags (CSS display values) ────────────────────────
			'show_audiobook'       => ( ! $is_template && 'audiobook' === $format ) ? 'block' : 'none',
			'show_ebook'           => ( ! $is_template && 'ebook' === $format )     ? 'block' : 'none',
			'show_no_audio'        => ( ! $is_template && '' !== $format && 'audiobook' !== $format && 'ebook' !== $format ) ? 'block' : 'none',

			// ── Core identification fields ───────────────────────────────────
			'format_label'         => $is_template ? '' : ( $row->format_label  ?? '' ),
			'subtitle'             => $is_template ? '' : ( $row->subtitle      ?? '' ),
			'pub_date'             => $is_template ? '' : ( $row->pub_date      ?? '' ),
			'edition_number'       => $is_template ? '' : ( isset( $row->edition_number ) && null !== $row->edition_number ? (int) $row->edition_number : '' ),
			'edition_type'         => $is_template ? '' : ( $row->edition_type  ?? '' ),
			'isbn13'               => $is_template ? '' : ( $row->isbn13        ?? '' ),
			'asin'                 => $is_template ? '' : ( $row->asin          ?? '' ),
			'price'                => $is_template ? '' : ( $row->price         ?? '' ),

			// ── Format-specific fields ───────────────────────────────────────
			'page_length'          => $is_template ? '' : ( $row->page_length   ?? '' ),
			'file_format'          => $is_template ? '' : ( $row->file_format   ?? '' ),
			'narrator'             => $is_template ? '' : ( $row->narrator      ?? '' ),
			'duration_hh'          => $is_template ? '' : ( isset( $row->duration_hh ) && null !== $row->duration_hh ? (int) $row->duration_hh : '' ),
			'duration_mm'          => $is_template ? '' : ( isset( $row->duration_mm ) && null !== $row->duration_mm ? (int) $row->duration_mm : '' ),
			'audio_sample_id'      => $is_template ? 0 : (int) ( $row->audio_sample_id ?? 0 ),
			'audio_title'          => ( ! $is_template && (int) ( $row->audio_sample_id ?? 0 ) ) ? get_the_title( (int) $row->audio_sample_id ) : '',

			// ── Media fields ─────────────────────────────────────────────────
			'cover_image_id'       => $is_template ? 0 : (int) ( $row->cover_image_id    ?? 0 ),
			'spinning_video_id'    => $is_template ? 0 : (int) ( $row->spinning_video_id ?? 0 ),
			'cover_thumb_url'      => ( ! $is_template && (int) ( $row->cover_image_id    ?? 0 ) ) ? wp_get_attachment_image_url( (int) $row->cover_image_id, 'thumbnail' ) : '', // R5-exempt: format data reading context
			'spinning_video_title' => ( ! $is_template && (int) ( $row->spinning_video_id ?? 0 ) ) ? get_the_title( (int) $row->spinning_video_id ) : '',

			// ── Publisher Supply Chain fields ────────────────────────────────
			'price_type'           => $is_template ? '' : ( $row->price_type    ?? '' ),
			'currency_code'        => $is_template ? '' : ( $row->currency_code ?? '' ),
			'supplier_name'        => $is_template ? '' : ( $row->supplier_name ?? '' ),
			'discount_code'        => $is_template ? '' : ( $row->discount_code ?? '' ),
			'on_sale_date'         => $is_template ? '' : ( $row->on_sale_date  ?? '' ),
			'returnability'        => $is_template ? '' : ( $row->returnability ?? '' ),

			// ── POS fields ───────────────────────────────────────────────────
			'square_item_id'       => $is_template ? '' : ( $row->square_item_id ?? '' ),
		);
	}
}
