<?php
/**
 * Printable recipe view.
 *
 * @package RestaurantCosting
 * @var array{recipe: object, lines: array, labor: array, totals: array} $data
 */

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

$recipe = $data['recipe'];
$lines  = $data['lines'];
$labor  = $data['labor'];
$totals = $data['totals'];
$is_scaled = (float) $recipe->scale_factor !== 1.0;

$has_nutrition = ! get_option( 'rc_hide_nutrition', 0 );
$has_photo     = (int) ( $recipe->print_image ?? 0 ) === 1 && ! empty( $recipe->image_url );
$has_missing   = false;
if ( $has_nutrition && ! empty( $lines ) ) {
	foreach ( $lines as $line ) {
		if ( ! empty( $line->ingredient_id ) ) {
			$ing = RC_Ingredient_Repository::get( (int) $line->ingredient_id );
			if ( $ing && ( null === $ing->calories || null === $ing->protein_grams ) ) {
				$has_missing = true;
				break;
			}
		}
	}
}
$show_bottom_row = $has_nutrition || $has_photo;
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
	<meta charset="<?php bloginfo( 'charset' ); ?>" />
	<title><?php echo esc_html( (string) $recipe->name ); ?> — <?php esc_html_e( 'Recipe', 'restaurant-costing' ); ?></title>
	<link rel="stylesheet" href="<?php echo esc_url( RC_PLUGIN_URL . 'admin/assets/admin-recipe.css?ver=' . RC_VERSION ); ?>" />
	<style>
		@font-face {
			font-family: 'Roboto';
			src: url('<?php echo esc_url( RC_PLUGIN_URL . 'admin/assets/fonts/Roboto/static/Roboto-Regular.ttf' ); ?>') format('truetype');
			font-weight: 400;
			font-style: normal;
		}
		@font-face {
			font-family: 'Roboto';
			src: url('<?php echo esc_url( RC_PLUGIN_URL . 'admin/assets/fonts/Roboto/static/Roboto-Medium.ttf' ); ?>') format('truetype');
			font-weight: 500;
			font-style: normal;
		}
		@font-face {
			font-family: 'Roboto';
			src: url('<?php echo esc_url( RC_PLUGIN_URL . 'admin/assets/fonts/Roboto/static/Roboto-Bold.ttf' ); ?>') format('truetype');
			font-weight: 700;
			font-style: normal;
		}
		body, body.rc-print-recipe, .rc-print-sheet { font-family: 'Roboto', Georgia, serif; font-size: 0.8em; }
		.rc-print-sheet h1 { font-weight: 500; }
		.rc-print-header-row { display: flex; align-items: flex-start; gap: 16px; }
		.rc-print-header-text { flex: 1; }
		.rc-print-logo { max-height: 48px; width: auto; }
		.rc-print-bottom { display: flex; margin-top: 8px; align-items: flex-start; }
		.rc-print-bottom-left { width: 45%; padding: 5px 8px 5px 0; }
		.rc-print-bottom-mid { width: 20%; padding: 5px 8px; border-left: 1px solid #ccc; border-right: 1px solid #ccc; }
		.rc-print-bottom-right { width: 35%; padding: 5px 0 5px 8px; }
		.rc-print-sheet .rc-print-image { width: 100%; max-width: none; height: auto; display: block; float: none; }
		.rc-nft { font-size: 10px; padding: 5px 0; width: 100%; }
		.rc-nft-header { font-size: 16px; padding: 0 6px; }
		.rc-nft-serving { padding: 0 6px; }
		.rc-nft-row { display: flex; justify-content: space-between; border-bottom: 1px dotted #999; padding: 1px 6px; font-weight: 700; background: #fff; }
		.rc-nft-row-cal { border-bottom: 3px solid #000; font-size: 12px; font-weight: 900; }
		.rc-nft-row-last { border-bottom: none; }
		.rc-nft-sub { padding-left: 22px; font-weight: 400; }
		.rc-nft-divider { border-bottom: 3px solid #000; margin: 1px 0; }
		.rc-nft-label { background: #fff; padding-right: 4px; font-weight: 400; }
		.rc-nft-value { background: #fff; padding-left: 4px; font-weight: 700; white-space: nowrap; }
		.rc-nft-row-cal .rc-nft-label { font-weight: 900; }
		.rc-nft-row-cal .rc-nft-value { font-weight: 900; }
		.rc-print-sheet h2 { font-size: 1.1em; margin: 10px 0 4px; }
		.rc-method { line-height: 1.4; }
		.rc-print-sheet ul { margin: 2px 0 4px 16px; }
		.rc-print-sheet p { margin: 2px 0; }
	</style>
	<?php if ( RC_License::should_watermark() ) : ?>
	<style>
		@media print, screen {
			body.rc-print-recipe::after {
				content: "DEMO";
				position: fixed;
				top: 0; left: 0; right: 0; bottom: 0;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 120px;
				font-weight: 900;
				color: rgba(180, 0, 0, 0.12);
				pointer-events: none;
				z-index: 9999;
				transform: rotate(-30deg);
				font-family: Arial, Helvetica, sans-serif;
				letter-spacing: 40px;
			}
		}
	</style>
	<?php endif; ?>
	<?php // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet -- print-only standalone page. ?>
</head>
<body class="rc-print-recipe">
	<p class="no-print rc-text-center rc-p-12">
		<button type="button" onclick="window.print();" class="button button-primary"><?php esc_html_e( 'Print', 'restaurant-costing' ); ?></button>
		<button type="button" onclick="window.close();" class="button"><?php esc_html_e( 'Close', 'restaurant-costing' ); ?></button>
	</p>

	<article class="rc-print-sheet">
		<div class="rc-print-header-row">
			<div class="rc-print-header-text">
				<h1><?php echo esc_html( (string) $recipe->name ); ?></h1>
				<?php if ( $is_scaled ) : ?>
					<p class="rc-warning-banner">
						<?php printf( esc_html__( 'Scaled Recipe (×%s)', 'restaurant-costing' ), esc_html( rc_fmt_num( $recipe->scale_factor ) ) ); ?>
					</p>
				<?php endif; ?>
				<p class="rc-meta">
			<?php
			printf(
				esc_html__( 'Recipe Makes (Portions): %1$s | Cost/portion: %2$s', 'restaurant-costing' ),
				esc_html( rc_fmt_num( $recipe->portions ) ),
				esc_html( '$' . number_format( (float) $totals['cost_per_portion'], 2 ) )
			);
			if ( $is_scaled ) {
				echo ' | ' . esc_html(
					sprintf(
						__( 'Batch cost: $%s', 'restaurant-costing' ),
						number_format( (float) $totals['batch_cost'], 2 )
					)
				);
			}
			if ( $recipe->menu_price ) {
				echo ' | ' . esc_html(
					sprintf(
						/* translators: 1: menu price 2: food cost percent */
						__( 'Menu: $%1$s (food cost %2$s%%)', 'restaurant-costing' ),
						number_format( (float) $recipe->menu_price, 2 ),
						rc_fmt_num( $totals['food_cost_pct'] )
					)
				);
			}
			?>
		</p>
			</div>
			<?php
			$rc_info = RC_Restaurant_Repository::get();
			if ( $rc_info && ! empty( $rc_info->logo_url ) ) :
				?>
				<img src="<?php echo esc_url( $rc_info->logo_url ); ?>" alt="" class="rc-print-logo" />
			<?php endif; ?>
		</div>

		<h2><?php esc_html_e( 'Ingredients', 'restaurant-costing' ); ?></h2>
		<table>
			<thead>
				<tr>
					<th><?php esc_html_e( 'Ingredient', 'restaurant-costing' ); ?></th>
					<th><?php esc_html_e( 'Metric', 'restaurant-costing' ); ?></th>
					<th><?php esc_html_e( 'Imperial', 'restaurant-costing' ); ?></th>
					<th><?php esc_html_e( 'Cost', 'restaurant-costing' ); ?></th>
				</tr>
			</thead>
			<tbody>
				<?php foreach ( $totals['line_details'] as $line ) : ?>
					<tr>
						<td><?php echo esc_html( (string) $line['ingredient_name'] ); ?></td>
						<td><?php echo esc_html( rc_fmt_num( $line['quantity'] ) . ' ' . $line['unit'] ); ?></td>
						<td><?php echo esc_html( (string) $line['imperial'] ); ?></td>
						<td><?php echo esc_html( '$' . number_format( (float) $line['line_cost'], 2 ) ); ?></td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>

		<?php if ( $show_bottom_row ) : ?>
		<div class="rc-print-bottom">
			<div class="rc-print-bottom-left">
				<?php if ( ! empty( $recipe->method ) ) : ?>
					<h2><?php esc_html_e( 'Method', 'restaurant-costing' ); ?></h2>
					<div class="rc-method"><?php echo nl2br( esc_html( (string) $recipe->method ) ); ?></div>
				<?php endif; ?>
				<?php if ( ! empty( $labor ) ) : ?>
					<h2><?php esc_html_e( 'Prep', 'restaurant-costing' ); ?></h2>
					<ul>
						<?php foreach ( $labor as $lab ) : ?>
							<li>
								<?php
								echo esc_html(
									trim(
										(string) $lab->item_label . ' — ' .
										rc_fmt_num( $lab->minutes ) . ' min'
									)
								);
								?>
							</li>
						<?php endforeach; ?>
					</ul>
					<p><?php printf( esc_html__( 'Prep per portion: %s', 'restaurant-costing' ), esc_html( '$' . number_format( (float) $totals['labor_per_portion'], 2 ) ) ); ?></p>
				<?php endif; ?>
			</div>
			<div class="rc-print-bottom-mid">
				<?php if ( $has_nutrition ) : ?>
					<?php if ( $has_missing ) : ?>
						<p style="color:#b32d2e; font-weight:700;font-size:9px;margin-bottom:4px;"><?php esc_html_e( 'Nutrition may not be accurate.', 'restaurant-costing' ); ?></p>
					<?php endif; ?>
					<?php
					$nutrition      = $totals['nutrition'] ?? array();
					$portion_weight = $totals['portion_weight'] ?? 0;
					include __DIR__ . '/partials/recipe-nft.php';
					?>
				<?php endif; ?>
			</div>
			<div class="rc-print-bottom-right">
				<?php if ( $has_photo ) : ?>
					<img src="<?php echo esc_url( $recipe->image_url ); ?>" alt="" class="rc-print-image" />
				<?php endif; ?>
			</div>
		</div>
		<?php elseif ( ! empty( $recipe->method ) || ! empty( $labor ) ) : ?>
			<?php if ( ! empty( $recipe->method ) ) : ?>
				<h2><?php esc_html_e( 'Method', 'restaurant-costing' ); ?></h2>
				<div class="rc-method"><?php echo nl2br( esc_html( (string) $recipe->method ) ); ?></div>
			<?php endif; ?>
			<?php if ( ! empty( $labor ) ) : ?>
				<h2><?php esc_html_e( 'Prep', 'restaurant-costing' ); ?></h2>
				<ul>
					<?php foreach ( $labor as $lab ) : ?>
						<li>
							<?php
							echo esc_html(
								trim(
									(string) $lab->item_label . ' — ' .
									rc_fmt_num( $lab->minutes ) . ' min'
								)
							);
							?>
						</li>
					<?php endforeach; ?>
				</ul>
				<p><?php printf( esc_html__( 'Prep per portion: %s', 'restaurant-costing' ), esc_html( '$' . number_format( (float) $totals['labor_per_portion'], 2 ) ) ); ?></p>
			<?php endif; ?>
		<?php endif; ?>
	</article>
</body>
</html>
