<?php
defined( 'ABSPATH' ) || exit;

/**
 * Link parsing, prompt formatting and source merging helpers for the pipeline.
 */
final class Costudio_Pipeline_Links {

	private function __construct() {
	}

	public static function parse_links_internos( string $raw ): array {
		$empty = array(
			'internal' => array(),
			'external' => array(),
			'legacy'   => false,
		);

		if ( '' === $raw ) {
			return $empty;
		}

		$decoded = json_decode( $raw, true );

		if ( JSON_ERROR_NONE !== json_last_error() || ! isset( $decoded['internal'] ) ) {
			return array_merge( $empty, array( 'legacy' => $raw ) );
		}

		return array(
			'internal' => (array) ( $decoded['internal'] ?? array() ),
			'external' => (array) ( $decoded['external'] ?? array() ),
			'legacy'   => false,
		);
	}

	/**
	 * Build the links/references block injected into agent prompts.
	 *
	 * @param string $raw             Raw `links_internos` DB value.
	 * @param int    $grounded_offset Number of grounded sources already in the
	 *                                citable pool (= count of `sources_json`).
	 *                                User externals are numbered AFTER them so
	 *                                `{{ref:N}}` matches the References Renderer
	 *                                pool exactly (grounded 1..G, then G+1..).
	 * @param bool   $read_content    When true (citation agents 3 & 4), fetch and
	 *                                embed the extracted CONTENT of each external
	 *                                URL so the model grounds on it, not just cites.
	 */
	public static function format_for_prompt( string $raw, int $grounded_offset = 0, bool $read_content = false ): string {
		$parsed = self::parse_links_internos( $raw );

		if ( false === $parsed['legacy'] && empty( $parsed['internal'] ) && empty( $parsed['external'] ) ) {
			return '';
		}

		if ( false !== $parsed['legacy'] ) {
			return '## ' . __( 'LINKS INTERNOS DISPONÍVEIS PARA INTERLINKAGEM', 'contentosapp-studio' ) . "\n" . $parsed['legacy'];
		}

		$sections = array();

		$selected_internal = array_filter(
			$parsed['internal'],
			static fn( $item ) => ! empty( $item['selected'] )
		);

		if ( ! empty( $selected_internal ) ) {
			$lines = array(
				'## ' . __( 'LINKS INTERNOS DISPONÍVEIS PARA INTERLINKAGEM', 'contentosapp-studio' ),
				__( 'Use estes links como âncoras naturais no texto, quando o contexto for relevante:', 'contentosapp-studio' ),
			);

			foreach ( $selected_internal as $item ) {
				$title     = $item['title'] ?? '';
				$url       = $item['url'] ?? '';
				$site_name = $item['site_name'] ?? '';
				$post_type = $item['post_type'] ?? 'post';
				$suffix    = ( 'page' === $post_type ) ? ' · Página' : '';

				$lines[] = "- [{$title}]({$url}) — {$site_name}{$suffix}";
			}

			$sections[] = implode( "\n", $lines );
		}

		if ( ! empty( $parsed['external'] ) ) {
			$external_block = $read_content
				? self::format_externals_with_content( $parsed['external'], $grounded_offset )
				: self::format_externals_cite_only( $parsed['external'] );

			if ( $external_block !== '' ) {
				$sections[] = $external_block;
			}
		}

		return implode( "\n\n", $sections );
	}

	/**
	 * Lightweight external-links block for non-citation agents (1, 2, 5, 6, 7):
	 * just URL + user description, no fetch.
	 *
	 * @param array $externals
	 * @return string
	 */
	private static function format_externals_cite_only( array $externals ): string {
		$lines = array(
			'## ' . __( 'REFERÊNCIAS EXTERNAS ADICIONAIS', 'contentosapp-studio' ),
			__( 'Use estes links para embasar dados, estatísticas ou afirmações ao longo do artigo. Cite-os obrigatoriamente como links de âncora natural no formato `[frase descritiva]({{ref:N}})`. Não use `{{ref:N}}` solto no fim da frase:', 'contentosapp-studio' ),
		);

		foreach ( $externals as $item ) {
			$url  = isset( $item['url'] ) ? trim( (string) $item['url'] ) : '';
			if ( '' === $url ) {
				continue;
			}
			$desc = trim( (string) ( $item['description'] ?? '' ) );
			if ( '' === $desc ) {
				$desc = '(sem descrição)';
			}
			$lines[] = "- [{$url}]({$url}) — {$desc}";
		}

		return implode( "\n", $lines );
	}

	/**
	 * Citation-agent (3 & 4) external block: fetch each external URL's main
	 * content via Costudio_Link_Reader and embed it, numbered with the exact
	 * `{{ref:N}}` index the References Renderer will resolve.
	 *
	 * Numbering: external at position j (URL non-empty) → N = grounded_offset + j + 1.
	 * This mirrors the renderer pool (grounded first, then user externals in order).
	 *
	 * @param array $externals
	 * @param int   $grounded_offset
	 * @return string
	 */
	private static function format_externals_with_content( array $externals, int $grounded_offset ): string {
		// The heading is a STRUCTURAL ANCHOR referenced verbatim by the agent
		// prompts (agents 03/04, all locales). It must stay identical across
		// locales — do NOT wrap it in __() / translate it, exactly like the
		// literal `## INDEXED SOURCES …` block.
		$header = array(
			'## FONTES EXTERNAS DO USUÁRIO — CONTEÚDO LIDO',
			__( 'O conteúdo real destas fontes foi extraído e está abaixo. EMBASE afirmações, dados e estatísticas neste conteúdo — não apenas cite o link. Cite cada fonte como `[frase descritiva factual]({{ref:N}})`, usando EXATAMENTE o número N indicado em cada bloco. Nunca escreva `{{ref:N}}` solto no fim da frase.', 'contentosapp-studio' ),
			'',
		);

		$items = array();
		$n     = $grounded_offset;

		foreach ( $externals as $item ) {
			$url = isset( $item['url'] ) ? trim( (string) $item['url'] ) : '';
			if ( '' === $url ) {
				continue;
			}
			$n++;
			$desc = trim( (string) ( $item['description'] ?? '' ) );

			$read = class_exists( 'Costudio_Link_Reader' )
				? Costudio_Link_Reader::read( $url )
				: array( 'ok' => false );

			if ( ! empty( $read['ok'] ) ) {
				$title = '' !== (string) $read['title']
					? (string) $read['title']
					: ( '' !== $desc ? $desc : $url );

				$block  = "### {{ref:{$n}}} — {$title}\n";
				if ( '' !== $desc ) {
					$block .= '_' . __( 'Nota do usuário', 'contentosapp-studio' ) . ":_ {$desc}\n";
				}
				$block .= '_' . __( 'Conteúdo extraído', 'contentosapp-studio' ) . ":_ " . (string) $read['snippet'] . "\n";
				$items[] = $block;
			} else {
				// Fetch failed — keep it citable from the user's description only.
				$label   = '' !== $desc ? $desc : $url;
				$items[] = "### {{ref:{$n}}} — {$label}\n_"
					. __( '(conteúdo não pôde ser lido — cite com base na descrição do usuário)', 'contentosapp-studio' )
					. "_\n";
			}
		}

		if ( empty( $items ) ) {
			return '';
		}

		return implode( "\n", $header ) . "\n" . implode( "\n", $items );
	}

	public static function extract_and_merge_sources( int $production_id, string $content ): void {
		if ( empty( $content ) ) {
			return;
		}

		preg_match_all( '/\[([^\]]+)\]\((https?:\/\/[^\s\)]+)\)/i', $content, $markdown_matches, PREG_SET_ORDER );
		preg_match_all( '/(?<!\(|\[)(https?:\/\/[^\s\)]+)/i', $content, $raw_matches, PREG_SET_ORDER );

		$merged_matches = array();
		foreach ( $markdown_matches as $m ) {
			$merged_matches[] = array(
				'title' => trim( $m[1] ),
				'url'   => $m[2],
			);
		}
		foreach ( $raw_matches as $m ) {
			$merged_matches[] = array(
				'title' => '',
				'url'   => $m[1],
			);
		}

		if ( empty( $merged_matches ) ) {
			return;
		}

		$prod = Costudio_Production::get_by_id( $production_id );
		if ( ! $prod ) {
			return;
		}

		$existing = json_decode( (string) $prod->sources_json, true ) ?: array();
		$urls     = array_column( $existing, 'url' );
		$modified = false;

		// Never re-add a URL the user supplied as an external link — it already
		// lives in the user-externals pool, so adding it to the grounded pool here
		// would double-list it (and break the "appears once, as the user's entry"
		// rule). Uses the SAME normalisation as the grounded dedupe.
		$user_ext = array();
		if ( class_exists( 'Costudio_Pipeline' ) ) {
			$parsed_ext = self::parse_links_internos( (string) $prod->links_internos );
			foreach ( (array) ( $parsed_ext['external'] ?? array() ) as $ux ) {
				$uu = isset( $ux['url'] ) ? trim( (string) $ux['url'] ) : '';
				if ( '' !== $uu ) {
					$user_ext[ Costudio_Pipeline::normalize_url_for_match( $uu ) ] = true;
				}
			}
		}

		foreach ( $merged_matches as $m ) {
			$url = self::clean_url_artifacts( (string) $m['url'] );
			if ( '' === $url ) {
				continue;
			}

			$title = (string) $m['title'];

			if ( false !== strpos( $url, 'contentosapp.com' ) ) {
				continue;
			}

			if ( ! empty( $user_ext ) && isset( $user_ext[ Costudio_Pipeline::normalize_url_for_match( $url ) ] ) ) {
				continue;
			}

			if ( ! in_array( $url, $urls, true ) ) {
				$existing[] = array(
					'title' => $title,
					'url'   => $url,
				);
				$urls[]     = $url;
				$modified   = true;
			}
		}

		if ( $modified ) {
			Costudio_Production::set_sources_json( $production_id, $existing );
		}
	}

	public static function clean_url_artifacts( string $url ): string {
		$url = trim( $url );
		if ( '' === $url ) {
			return '';
		}

		$junk = '`])>';
		while ( '' !== $url && false !== strpos( $junk, $url[ strlen( $url ) - 1 ] ) ) {
			$url = substr( $url, 0, -1 );
		}

		if ( ! preg_match( '#^https?://[^\s]+$#i', $url ) ) {
			return '';
		}

		return $url;
	}
}
