<?php
/**
 * Collect all theme / block / global styles used on the public front
 * so editor canvas and builder content share the same look.
 *
 * @package AuraBuilder
 */

declare(strict_types=1);

namespace AuraBuilder\Theme;

final class ThemeStyles {

	/**
	 * Stylesheet URLs that the front theme relies on.
	 *
	 * @return list<string>
	 */
	public function links(): array {
		$links   = array();
		$links[] = includes_url( 'css/dist/block-library/style.min.css' );
		$links[] = includes_url( 'css/dist/block-library/theme.min.css' );

		$nav = ABSPATH . WPINC . '/blocks/navigation/style.min.css';
		if ( is_file( $nav ) ) {
			$links[] = includes_url( 'blocks/navigation/style.min.css' );
		}

		$style = get_stylesheet_uri();
		if ( is_string( $style ) && $style !== '' ) {
			$links[] = $style;
		}
		$min = get_stylesheet_directory() . '/style.min.css';
		if ( is_file( $min ) ) {
			$links[] = get_stylesheet_directory_uri() . '/style.min.css';
		}
		if ( get_template_directory() !== get_stylesheet_directory() ) {
			$links[] = get_template_directory_uri() . '/style.css';
		}

		return array_values( array_unique( array_filter( $links ) ) );
	}

	/**
	 * Inline CSS: theme.json global styles + fonts + builder theme bridge.
	 */
	public function inline(): string {
		$css = '';

		if ( function_exists( 'wp_get_global_stylesheet' ) ) {
			foreach ( array( array( 'variables' ), array( 'presets' ), array( 'styles' ) ) as $types ) {
				$chunk = wp_get_global_stylesheet( $types );
				if ( is_string( $chunk ) && $chunk !== '' ) {
					$css .= $chunk . "\n";
				}
			}
		}

		if ( function_exists( 'wp_print_font_faces' ) ) {
			ob_start();
			wp_print_font_faces();
			$fonts = (string) ob_get_clean();
			if ( preg_match_all( '/<style[^>]*>(.*?)<\/style>/is', $fonts, $m ) ) {
				foreach ( $m[1] as $part ) {
					$css .= $part . "\n";
				}
			}
		}

		$css .= $this->bridge_css();

		return $css;
	}

	/**
	 * Make Aura Builder widgets inherit theme tokens / typography.
	 */
	private function bridge_css(): string {
		return <<<'CSS'
/* Aura Builder ↔ theme bridge (front + editor canvas) */
.ab-document,
.ab-page-body,
.ab-theme-surface {
  color: var(--wp--preset--color--foreground, inherit);
  font-family: var(--wp--preset--font-family--system-font, var(--wp--preset--font-family--body, inherit));
  font-size: var(--wp--preset--font-size--medium, inherit);
  line-height: var(--wp--custom--typography--line-height--normal, 1.6);
}
.ab-document .ab-heading,
.ab-page-body .ab-heading,
.ab-theme-surface .ab-heading {
  font-family: var(--wp--preset--font-family--source-serif-pro, var(--wp--preset--font-family--heading, inherit));
  font-weight: var(--wp--custom--typography--font-weight--heading, 400);
  line-height: var(--wp--custom--typography--line-height--tiny, 1.15);
  /* Do NOT force color: inherit here — node/leaf inline color must win */
}
.ab-document .ab-heading h1,
.ab-document h1.ab-heading,
.ab-type-heading h1 {
  font-size: var(--wp--custom--typography--font-size--colossal, clamp(2.5rem, 6vw, 4rem));
}
.ab-document h2.ab-heading,
.ab-type-heading h2 {
  font-size: var(--wp--preset--font-size--x-large, 2rem);
}
.ab-document h3.ab-heading,
.ab-type-heading h3 {
  font-size: var(--wp--preset--font-size--large, 1.5rem);
}
.ab-document .ab-text,
.ab-page-body .ab-text {
  font-size: var(--wp--preset--font-size--medium, 1.125rem);
  line-height: var(--wp--custom--typography--line-height--normal, 1.6);
}
/* Defaults — white text so theme link colors cannot paint solid buttons red.
   --ab-btn-color is set per node by StyleEngine when the user picks a label color
   (white buttons on blue cards). Without it, white-on-white hid the text on the site. */
.ab-document .ab-btn,
.ab-page-body .ab-btn,
.ab-document a.ab-btn,
.ab-document a.ab-btn.wp-element-button,
.ab-document--editor .ab-btn,
.ab-document--editor a.ab-btn {
  background: var(--ab-btn-bg, var(--ab-color-primary, var(--wp--preset--color--primary, #2563eb)));
  color: var(--ab-btn-color, var(--ab-color-btn-text, #ffffff)) !important;
  -webkit-text-fill-color: var(--ab-btn-color, var(--ab-color-btn-text, #ffffff)) !important;
  font-family: inherit;
  font-size: var(--wp--preset--font-size--medium, 1rem);
  border-radius: var(--ab-radius-md, 8px);
  padding: calc(0.667em + 2px) calc(1.333em + 2px);
  text-decoration: none !important;
  box-shadow: none;
}
.ab-document .ab-btn:hover,
.ab-page-body .ab-btn:hover,
.ab-document a.ab-btn:hover,
.ab-document a.ab-btn:visited,
.ab-document a.ab-btn:focus {
  opacity: 0.9;
  color: var(--ab-btn-color, var(--ab-color-btn-text, #ffffff)) !important;
  -webkit-text-fill-color: var(--ab-btn-color, var(--ab-color-btn-text, #ffffff)) !important;
  text-decoration: none !important;
}
.ab-document .ab-divider,
.ab-page-body .ab-divider {
  border-color: currentColor;
  opacity: 0.25;
}
/* Document width is owned by root node (StyleEngine content_width / max_width).
   Do NOT force theme content-size (650px) — that made the live page narrower
   than the editor and left uneven side gaps. */
.ab-document.ab-document--front,
.ab-content-wrap > .ab-document,
.ab-page-body .ab-document {
  width: 100%;
  max-width: none;
  margin-left: 0;
  margin-right: 0;
  box-sizing: border-box;
}
body.ab-builder-page {
  --ab-page-gutter: 16px;
  overflow-x: clip;
}
@media (min-width: 768px) {
  body.ab-builder-page { --ab-page-gutter: 20px; }
}
@media (min-width: 1024px) {
  body.ab-builder-page { --ab-page-gutter: 24px; }
}
.ab-content-wrap {
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  /* Match editor gutters (16 / 20 / 24) */
  padding-left: var(--ab-page-gutter, 16px);
  padding-right: var(--ab-page-gutter, 16px);
  margin-left: 0;
  margin-right: 0;
}
/* Escape theme constrained layouts (Twenty Twenty-Four etc.) */
body.ab-builder-page .is-layout-constrained > :where(:not(.alignleft):not(.alignright):not(.alignfull)),
body.ab-builder-page .is-layout-constrained > .ab-content-wrap,
body.ab-builder-page .wp-block-post-content.is-layout-constrained {
  max-width: none !important;
  width: 100% !important;
  margin-left: 0 !important;
  margin-right: 0 !important;
}
body.ab-builder-page .alignfull,
body.ab-builder-page .ab-content-wrap.alignfull {
  width: 100% !important;
  max-width: 100% !important;
  margin-left: 0 !important;
  margin-right: 0 !important;
  margin-inline: 0 !important;
  left: auto !important;
  right: auto !important;
  position: relative !important;
}
/*
 * Do NOT force margin-bottom on .ab-type-section.
 * Spacing between sections is controlled by the Root "gap" setting
 * (and per-node margins). A theme-like margin here created white bands
 * between footer/header sections on the public site (editor had margin:0).
 */
.ab-type-section {
  margin-top: 0;
  margin-bottom: 0;
}
/* Theme Builder chrome: never allow theme block-gap between stacked sections */
.ab-theme-builder-header .ab-type-section,
.ab-theme-builder-footer .ab-type-section,
.ab-theme-builder-header .ab-type-root > *,
.ab-theme-builder-footer .ab-type-root > * {
  margin-block-start: 0 !important;
  margin-block-end: 0 !important;
  margin-top: 0 !important;
  margin-bottom: 0 !important;
}
.ab-theme-builder-header .ab-type-root,
.ab-theme-builder-footer .ab-type-root {
  gap: 0;
  row-gap: 0;
  --wp--style--block-gap: 0px;
}
CSS;
	}

	/**
	 * Enqueue everything needed on the public singular page.
	 */
	public function enqueue_front(): void {
		foreach ( $this->links() as $i => $url ) {
			wp_enqueue_style( 'aura-builder-theme-' . $i, $url, array(), null );
		}
		wp_register_style( 'aura-builder-theme-inline', false, array(), AURA_BUILDER_VERSION );
		wp_enqueue_style( 'aura-builder-theme-inline' );
		wp_add_inline_style( 'aura-builder-theme-inline', $this->inline() );

		wp_enqueue_style(
			'aura-builder-front',
			AURA_BUILDER_URL . 'frontend/css/front.css',
			array( 'aura-builder-theme-inline' ),
			AURA_BUILDER_VERSION
		);
	}

	/**
	 * Payload for editor (inject into canvas).
	 *
	 * @return array{links:list<string>,inline:string}
	 */
	public function for_editor(): array {
		return array(
			'links'  => $this->links(),
			'inline' => $this->inline(),
		);
	}
}
