<?php
/**
 * UFPE Design System — setup do tema block.
 *
 * @package ufpe-ds
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Sem acesso direto.
}

define( 'UFPE_DS_VERSION', '0.1.1' );

/**
 * Enfileira os design tokens (@ufpe/tokens) e o style.css do tema.
 *
 * O tokens.css traz as CSS custom properties canônicas (--color-bordo,
 * --space-*, --font-*) e é carregado ANTES do style.css, que as consome.
 * Vale tanto no front-end quanto no editor de blocos.
 */
function ufpe_ds_enqueue_assets() {
	$tokens_rel  = '/assets/tokens.css';
	$tokens_path = get_theme_file_path( $tokens_rel );
	$tokens_ver  = file_exists( $tokens_path ) ? filemtime( $tokens_path ) : UFPE_DS_VERSION;

	wp_enqueue_style(
		'ufpe-tokens',
		get_theme_file_uri( $tokens_rel ),
		array(),
		$tokens_ver
	);

	wp_enqueue_style(
		'ufpe-ds-style',
		get_stylesheet_uri(),
		array( 'ufpe-tokens' ),
		UFPE_DS_VERSION
	);
}
add_action( 'wp_enqueue_scripts', 'ufpe_ds_enqueue_assets' );

/**
 * Carrega os tokens também dentro do editor de blocos.
 */
function ufpe_ds_editor_assets() {
	add_editor_style( array( 'assets/tokens.css', 'style.css' ) );
}
add_action( 'after_setup_theme', 'ufpe_ds_editor_assets' );

/**
 * Suporte a recursos do tema.
 */
function ufpe_ds_setup() {
	add_theme_support( 'wp-block-styles' );
	add_theme_support( 'responsive-embeds' );
	add_theme_support( 'editor-styles' );
	add_theme_support( 'html5', array( 'search-form', 'gallery', 'caption', 'style', 'script' ) );
	load_theme_textdomain( 'ufpe-ds', get_template_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'ufpe_ds_setup' );

/**
 * Categoria própria para os block patterns institucionais.
 */
function ufpe_ds_register_pattern_category() {
	if ( function_exists( 'register_block_pattern_category' ) ) {
		register_block_pattern_category(
			'ufpe',
			array( 'label' => __( 'UFPE', 'ufpe-ds' ) )
		);
	}
}
add_action( 'init', 'ufpe_ds_register_pattern_category' );
