<?php
/**
 * Themecut functions and definitions.
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package WordPress
 * @subpackage Themecut
 * @since Themecut 1.0
 */

// Adds theme support for post formats, title tag, and featured images.
if ( ! function_exists( 'themecut_post_format_setup' ) ) :
	/**
	 * Adds theme support for post formats, title tag, and featured images.
	 *
	 * @since Themecut 1.0
	 *
	 * @return void
	 */
	function themecut_post_format_setup() {
		add_theme_support( 'title-tag' );
		add_theme_support( 'post-thumbnails' );
		add_theme_support( 'post-formats', array( 'aside', 'audio', 'chat', 'gallery', 'image', 'link', 'quote', 'status', 'video' ) );
	}
endif;
add_action( 'after_setup_theme', 'themecut_post_format_setup' );

// Enqueues editor-style.css in the editors.
if ( ! function_exists( 'themecut_editor_style' ) ) :
	/**
	 * Enqueues editor-style.css in the editors.
	 *
	 * @since Themecut 1.0
	 *
	 * @return void
	 */
	function themecut_editor_style() {
		add_editor_style( 'assets/css/editor-style.css' );
		add_editor_style( 'assets/css/themecut.css' );
	}
endif;
add_action( 'after_setup_theme', 'themecut_editor_style' );

// Enqueues the theme stylesheet on the front.
if ( ! function_exists( 'themecut_enqueue_styles' ) ) :
	/**
	 * Enqueues the theme stylesheet on the front.
	 *
	 * @since Themecut 1.0
	 *
	 * @return void
	 */
	function themecut_enqueue_styles() {
		$suffix = SCRIPT_DEBUG ? '' : '.min';
		$src    = 'style' . $suffix . '.css';

		wp_enqueue_style(
			'themecut-style',
			get_parent_theme_file_uri( $src ),
			array(),
			wp_get_theme()->get( 'Version' )
		);
		wp_style_add_data(
			'themecut-style',
			'path',
			get_parent_theme_file_path( $src )
		);
	}
endif;
add_action( 'wp_enqueue_scripts', 'themecut_enqueue_styles' );

// Enqueues Themecut section layout styles (grid, cards, section backgrounds).
if ( ! function_exists( 'themecut_sections_style' ) ) :
	function themecut_sections_style() {
		wp_enqueue_style(
			'themecut-sections',
			get_theme_file_uri( 'assets/css/themecut.css' ),
			array( 'themecut-style' ),
			wp_get_theme()->get( 'Version' )
		);
	}
endif;
add_action( 'wp_enqueue_scripts', 'themecut_sections_style' );

// Enqueues Feather Icons (CDN) and the Themecut UI library.
if ( ! function_exists( 'themecut_enqueue_scripts' ) ) :
	function themecut_enqueue_scripts() {
		wp_enqueue_script(
			'feather-icons',
			get_template_directory_uri() . '/assets/js/feather.min.js',
			array(),
			'4.29.2',
			true
		);
		wp_enqueue_script(
			'themecut-lib',
			get_theme_file_uri( 'assets/js/themecut.lib.js' ),
			array( 'feather-icons' ),
			wp_get_theme()->get( 'Version' ),
			true
		);
	}
endif;
add_action( 'wp_enqueue_scripts', 'themecut_enqueue_scripts' );

// Registers custom block styles.
if ( ! function_exists( 'themecut_block_styles' ) ) :
	/**
	 * Registers custom block styles.
	 *
	 * @since Themecut 1.0
	 *
	 * @return void
	 */
	function themecut_block_styles() {
		register_block_style(
			'core/list',
			array(
				'name'         => 'checkmark-list',
				'label'        => __( 'Checkmark', 'themecut' ),
				'inline_style' => '
				ul.is-style-checkmark-list {
					list-style-type: "\2713";
				}

				ul.is-style-checkmark-list li {
					padding-inline-start: 1ch;
				}',
			)
		);
	}
endif;
add_action( 'init', 'themecut_block_styles' );

// Registers pattern categories.
if ( ! function_exists( 'themecut_pattern_categories' ) ) :
	/**
	 * Registers pattern categories.
	 *
	 * @since Themecut 1.0
	 *
	 * @return void
	 */
	function themecut_pattern_categories() {

		register_block_pattern_category(
			'themecut_page',
			array(
				'label'       => __( 'Pages', 'themecut' ),
				'description' => __( 'A collection of full page layouts.', 'themecut' ),
			)
		);

		register_block_pattern_category(
			'themecut_post-format',
			array(
				'label'       => __( 'Post formats', 'themecut' ),
				'description' => __( 'A collection of post format patterns.', 'themecut' ),
			)
		);
	}
endif;
add_action( 'init', 'themecut_pattern_categories' );

// Registers block binding sources.
if ( ! function_exists( 'themecut_register_block_bindings' ) ) :
	/**
	 * Registers the post format block binding source.
	 *
	 * @since Themecut 1.0
	 *
	 * @return void
	 */
	function themecut_register_block_bindings() {
		register_block_bindings_source(
			'themecut/format',
			array(
				'label'              => _x( 'Post format name', 'Label for the block binding placeholder in the editor', 'themecut' ),
				'get_value_callback' => 'themecut_format_binding',
			)
		);
	}
endif;
add_action( 'init', 'themecut_register_block_bindings' );

// Registers block binding callback function for the post format name.
if ( ! function_exists( 'themecut_format_binding' ) ) :
	/**
	 * Callback function for the post format name block binding source.
	 *
	 * @since Themecut 1.0
	 *
	 * @return string|void Post format name, or nothing if the format is 'standard'.
	 */
	function themecut_format_binding() {
		$post_format_slug = get_post_format();

		if ( $post_format_slug && 'standard' !== $post_format_slug ) {
			return get_post_format_string( $post_format_slug );
		}
	}
endif;

// Recommends the Themecut plugin from the Appearance → Themes screen.
if ( ! function_exists( 'themecut_plugin_recommendation_notice' ) ) :
	function themecut_plugin_recommendation_notice(): void {
		$screen = get_current_screen();
		if ( ! $screen || 'themes' !== $screen->id ) {
			return;
		}
		if ( is_plugin_active( 'themecut/themecut.php' ) ) {
			return;
		}
		if ( get_user_meta( get_current_user_id(), 'themecut_dismissed_plugin_notice', true ) ) {
			return;
		}
		$nonce = wp_create_nonce( 'themecut_dismiss_plugin_notice' );
		?>
		<div class="notice notice-info is-dismissible" data-tc-notice="plugin">
			<p>
				<strong><?php esc_html_e( 'Themecut', 'themecut' ); ?></strong> —
				<?php esc_html_e( 'Generate a complete AI-powered WordPress site in seconds. Import 8 free starter themes or paste a share hash — no account needed.', 'themecut' ); ?>
				<a href="<?php echo esc_url( 'https://wordpress.org/plugins/themecut/' ); ?>" target="_blank" rel="noopener noreferrer">
					<?php esc_html_e( 'Install free →', 'themecut' ); ?>
				</a>
			</p>
		</div>
		<script>
		(function () {
			document.addEventListener('DOMContentLoaded', function () {
				var el = document.querySelector('[data-tc-notice="plugin"]');
				if (!el) return;
				el.addEventListener('click', function (e) {
					if (!e.target.classList.contains('notice-dismiss')) return;
					var fd = new FormData();
					fd.append('action', 'themecut_dismiss_plugin_notice');
					fd.append('_wpnonce', '<?php echo esc_js( $nonce ); ?>');
					fetch('<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>', { method: 'POST', body: fd });
				});
			});
		})();
		</script>
		<?php
	}
endif;
add_action( 'admin_notices', 'themecut_plugin_recommendation_notice' );

add_action( 'wp_ajax_themecut_dismiss_plugin_notice', function (): void {
	check_ajax_referer( 'themecut_dismiss_plugin_notice' );
	update_user_meta( get_current_user_id(), 'themecut_dismissed_plugin_notice', '1' );
	wp_die();
} );

// Sets the bundled logo.png as the site logo on first activation.
if ( ! function_exists( 'themecut_set_default_logo' ) ) :
	function themecut_set_default_logo(): void {
		if ( get_theme_mod( 'custom_logo' ) ) {
			return;
		}

		$logo_path = get_theme_file_path( 'assets/images/logo.png' );
		if ( ! file_exists( $logo_path ) ) {
			return;
		}

		require_once ABSPATH . 'wp-admin/includes/file.php';
		require_once ABSPATH . 'wp-admin/includes/media.php';
		require_once ABSPATH . 'wp-admin/includes/image.php';

		$tmp = wp_tempnam( 'themecut-logo.png' );
		copy( $logo_path, $tmp );

		$file = [
			'name'     => 'themecut-logo.png',
			'type'     => 'image/png',
			'tmp_name' => $tmp,
			'error'    => 0,
			'size'     => filesize( $tmp ),
		];

		$attachment_id = media_handle_sideload( $file, 0, 'Themecut Logo' );

		if ( ! is_wp_error( $attachment_id ) ) {
			set_theme_mod( 'custom_logo', $attachment_id );
		}
	}
endif;
add_action( 'after_switch_theme', 'themecut_set_default_logo' );

