<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
/** @var WP_Post $post */
$meta = DC_V2_Document_CPT::get_meta( $post->ID );
$appearance = DC_V2_Public::resolve_appearance();
$theme_class = ! empty( $appearance['preset'] )
	? ' dc-v2-theme-' . sanitize_html_class( $appearance['preset'] )
	: '';
$is_preview      = isset( $_GET['preview'] );
$is_admin_view   = $is_preview && is_user_logged_in() && current_user_can( 'manage_woocommerce' );
$html_class      = $is_preview ? 'dc-v2-html--embed' : '';
$body_extra      = $is_preview ? ' dc-v2-body--embed' : '';
$brand           = dc_v2_resolve_header_brand( $appearance['logo_url'] ?? '' );
$mark_class      = in_array( $brand['mode'], array( 'favicon', 'plugin' ), true ) ? ' dc-v2-brand-mark--favicon' : '';
$brand_src       = $brand['src'];
$store_name      = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
$page_title      = sprintf( '%s | Order From DM', $store_name );
$og_description  = function_exists( 'dc_v2_share_preview_text' )
	? dc_v2_share_preview_text( $meta['copy'] ?? array() )
	: __( 'Finish your order', 'order-from-dm' );
$og_image        = class_exists( 'DC_V2_Og_Image', false ) && ! empty( $meta['slug'] )
	? DC_V2_Og_Image::url_for_slug( $meta['slug'] )
	: '';
$og_url          = ! empty( $meta['slug'] ) ? DC_V2_Document_CPT::public_url( $meta['slug'] ) : '';

$dc_ent     = DC_V2_License::entitlement();
$dc_blocked = empty( $dc_ent['unlimited'] ) && empty( $dc_ent['allowed'] );

if ( $dc_blocked ) {
	if ( $is_admin_view ) {
		include DC_V2_PATH . 'public-v2/views/chat-locked.php';
		return;
	}
	status_header( 404 );
	wp_die(
		esc_html__( 'This order is not available.', 'order-from-dm' ),
		esc_html__( 'Not available', 'order-from-dm' ),
		array( 'response' => 404 )
	);
}

$dc_runtime = DC_V2_Chat_Runtime::resolve();
if ( empty( $dc_runtime['ready'] ) ) {
	// Still load CSS shell for the error view.
	$css = 'public-v2/css/chat-checkout.css';
	wp_enqueue_style(
		'dc-v2-chat',
		dc_v2_asset_url( $css ),
		array(),
		DC_V2_VERSION
	);
	include DC_V2_PATH . 'public-v2/views/chat-runtime-unavailable.php';
	return;
}

DC_V2_Public::enqueue_for_chat( $post, $meta );
?><!DOCTYPE html>
<html <?php language_attributes(); ?> class="<?php echo esc_attr( $html_class ); ?>">
<head>
	<meta charset="<?php bloginfo( 'charset' ); ?>">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, viewport-fit=cover">
	<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
	<meta http-equiv="Pragma" content="no-cache">
	<meta http-equiv="Expires" content="0">
	<meta name="theme-color" content="<?php echo esc_attr( $appearance['background'] ?? '#000000' ); ?>">
	<title><?php echo esc_html( $page_title ); ?></title>
	<meta name="description" content="<?php echo esc_attr( $og_description ); ?>">
	<meta property="og:type" content="website">
	<meta property="og:site_name" content="Order From DM">
	<meta property="og:title" content="<?php echo esc_attr( $page_title ); ?>">
	<meta property="og:description" content="<?php echo esc_attr( $og_description ); ?>">
	<?php if ( $og_url ) : ?>
		<meta property="og:url" content="<?php echo esc_url( $og_url ); ?>">
	<?php endif; ?>
	<?php if ( $og_image ) : ?>
		<meta property="og:image" content="<?php echo esc_url( $og_image ); ?>">
		<meta property="og:image:width" content="1200">
		<meta property="og:image:height" content="630">
		<meta property="og:image:type" content="image/png">
	<?php endif; ?>
	<meta name="twitter:card" content="summary_large_image">
	<meta name="twitter:title" content="<?php echo esc_attr( $page_title ); ?>">
	<meta name="twitter:description" content="<?php echo esc_attr( $og_description ); ?>">
	<?php if ( $og_image ) : ?>
		<meta name="twitter:image" content="<?php echo esc_url( $og_image ); ?>">
	<?php endif; ?>
	<?php wp_head(); ?>
</head>
<body class="dc-v2-body<?php echo esc_attr( $theme_class . $body_extra ); ?>">
	<div class="dc-v2-bg-blobs" aria-hidden="true">
		<span class="dc-v2-blob dc-v2-blob--1"></span>
		<span class="dc-v2-blob dc-v2-blob--2"></span>
		<span class="dc-v2-blob dc-v2-blob--3"></span>
	</div>
	<div id="dc-v2-chat" class="dc-v2-chat" data-dc-v2-version="<?php echo esc_attr( DC_V2_VERSION ); ?>">
		<header class="dc-v2-header">
			<div class="dc-v2-brand-mark<?php echo esc_attr( $mark_class ); ?>" id="dc-v2-brand-mark">
				<?php if ( $brand_src ) : ?>
					<img id="dc-v2-logo" class="dc-v2-logo" src="<?php echo esc_url( $brand_src ); ?>" alt="" />
				<?php else : ?>
					<img id="dc-v2-logo" class="dc-v2-logo" alt="" hidden />
				<?php endif; ?>
			</div>
			<span class="dc-v2-badge" id="dc-v2-badge">
				<span class="dc-v2-badge-dot" aria-hidden="true"></span>
				<span><?php echo esc_html( dc_v2_badge_text() ); ?></span>
			</span>
		</header>
		<div class="dc-v2-thread" id="dc-v2-thread" role="log" aria-live="polite"></div>
		<div class="dc-v2-toast" id="dc-v2-toast" role="alert" hidden></div>
		<footer class="dc-v2-footer">
			<div class="dc-v2-footer-summary" id="dc-v2-summary" hidden></div>
			<p class="dc-v2-footer-trust" id="dc-v2-trust"><?php echo esc_html( __( 'Secure bank transfer payment', 'order-from-dm' ) ); ?></p>
			<button type="button" class="dc-v2-btn dc-v2-btn--primary" id="dc-v2-action" disabled><?php echo esc_html( __( 'Continue', 'order-from-dm' ) ); ?></button>
			<p class="dc-v2-footer-powered">
				<a href="<?php echo esc_url( dc_v2_powered_by_url() ); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html( dc_v2_powered_by_label() ); ?></a>
			</p>
		</footer>
	</div>
	<?php wp_footer(); ?>
</body>
</html>
