<?php
/**
 * Brentwood 100 — editor-side warnings.
 *
 * The tile settings can be combined in ways that cannot be drawn: "media only"
 * on an item with no media, or a square tile whose only permitted layouts sit
 * text beside media. bw_hundred_tile_data() resolves all of those at render time
 * so nothing ever breaks on the front end — but silently, which means an author
 * gets no signal that the tile isn't doing what they asked.
 *
 * This says so, on the edit screen and in the list. It never blocks a save: the
 * combination is legal, it just won't be honoured, and interrupting someone
 * mid-edit over a cosmetic mismatch is worse than telling them plainly.
 *
 * @package Kadence-Child
 */

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

require_once get_stylesheet_directory() . '/inc/bw-hundred.php';

/**
 * Problems with one item's settings, in plain language.
 *
 * @param WP_Post $p Item.
 * @return string[] Warnings, empty when the settings can all be honoured.
 */
function bw_hundred_warnings( $p ) {
	$out = array();

	$has_text  = '' !== trim( wp_strip_all_tags( (string) get_field( 'hundred_text', $p->ID ) ) );
	$type      = 'video' === (string) get_field( 'hundred_media_type', $p->ID ) ? 'video' : 'images';
	$images    = array_filter( (array) get_field( 'hundred_images', $p->ID ) );
	$video     = get_field( 'hundred_video_file', $p->ID );
	$video_url = trim( (string) get_field( 'hundred_video_url', $p->ID ) );
	$has_media = ( 'video' === $type ) ? ( $video || '' !== $video_url ) : ! empty( $images );

	$combos = (array) get_post_meta( $p->ID, 'hundred_combos', true );

	if ( ! $has_text && ! $has_media ) {
		$out[] = __( 'This item has neither text nor media, so its tile will show only the title.', 'kadence-child' );
	}
	if ( ! $combos && ( $has_text || $has_media ) ) {
		$out[] = __( 'No tile layouts are ticked in the preview above — the grid will use all of them.', 'kadence-child' );
	}

	if ( 'video' === $type && ! $has_media ) {
		$out[] = __( 'Media type is set to Video, but no video file or link has been added.', 'kadence-child' );
	}
	if ( 'images' === $type && ! $images && ( $video || '' !== $video_url ) ) {
		$out[] = __( 'A video has been added, but Media type is set to Images — the video will be ignored.', 'kadence-child' );
	}

	return $out;
}

/** The warnings for the item being edited, above the editor. */
add_action(
	'edit_form_after_title',
	function ( $post ) {
		if ( 'bw_hundred' !== $post->post_type ) {
			return;
		}
		$warnings = bw_hundred_warnings( $post );
		if ( ! $warnings ) {
			return;
		}
		echo '<div class="notice notice-warning" style="margin:12px 0"><p><strong>'
			. esc_html__( 'This tile won’t display quite as configured:', 'kadence-child' )
			. '</strong></p><ul style="margin:0 0 8px 18px;list-style:disc">';
		foreach ( $warnings as $w ) {
			echo '<li>' . esc_html( $w ) . '</li>';
		}
		echo '</ul><p>' . esc_html__( 'Nothing is broken — the grid falls back to what the item actually has. Adjust the settings below if that isn’t what you want.', 'kadence-child' ) . '</p></div>';
	}
);

/** A "Tile" column in the list, so problems are visible without opening each one. */
add_filter(
	'manage_bw_hundred_posts_columns',
	function ( $cols ) {
		$out = array();
		foreach ( $cols as $k => $v ) {
			$out[ $k ] = $v;
			if ( 'title' === $k ) {
				$out['bw_tile'] = __( 'Tile', 'kadence-child' );
			}
		}
		return $out;
	}
);

add_action(
	'manage_bw_hundred_posts_custom_column',
	function ( $col, $post_id ) {
		if ( 'bw_tile' !== $col ) {
			return;
		}
		$p = get_post( $post_id );
		$warnings = bw_hundred_warnings( $p );
		if ( $warnings ) {
			printf(
				'<span title="%s" style="color:#996800">⚠ %s</span>',
				esc_attr( implode( ' ', $warnings ) ),
				esc_html( sprintf(
					/* translators: %d: number of warnings. */
					_n( '%d issue', '%d issues', count( $warnings ), 'kadence-child' ),
					count( $warnings )
				) )
			);
			return;
		}
		$t = bw_hundred_tile_data( $p );
		printf(
			'<span style="color:#646970">%s</span>',
			esc_html( implode( ' · ', array(
				sprintf(
					/* translators: %d: number of enabled shape/layout permutations. */
					_n( '%d layout', '%d layouts', count( $t['combos'] ), 'kadence-child' ),
					count( $t['combos'] )
				),
				$t['has_media'] ? ( 'video' === $t['media_type'] ? __( 'video', 'kadence-child' ) : sprintf( _n( '%d image', '%d images', count( $t['images'] ), 'kadence-child' ), count( $t['images'] ) ) ) : __( 'no media', 'kadence-child' ),
			) ) )
		);
	},
	10,
	2
);

/**
 * Keep Yoast out of the Brentwood 100 editor.
 *
 * These items have no URL of their own — they exist only as tiles inside the
 * grid — so an SEO panel offers settings that can never apply, on the screen
 * where an author is trying to write two sentences.
 */
add_filter(
	'wpseo_accessible_post_types',
	function ( $types ) {
		unset( $types['bw_hundred'] );
		return $types;
	}
);
