<?php

declare(strict_types=1);

namespace Signocore\Multilanguage\Support;

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

use Signocore\Multilanguage\Features\LanguageManager;
use Signocore\Multilanguage\Infrastructure\Database\TranslationRepository;

final class TranslationHelper
{
	/**
	 * Get all translations in a group, keyed by language code.
	 *
	 * @return array<string, int>
	 */
	public static function getTranslationGroup(int $elementId, string $elementType = 'post'): array
	{
		$resolvedType = str_contains($elementType, '_') ? $elementType : self::resolveElementType($elementId);

		$groupId = TranslationRepository::getGroupId($elementId, $resolvedType);
		if ($groupId === null) {
			return [];
		}

		$rows = TranslationRepository::findByGroup($groupId);
		$translations = [];

		foreach ($rows as $row) {
			$translations[$row->language_code] = (int) $row->element_id;
		}

		return $translations;
	}

	/**
	 * Check if a translation exists for a given element and language.
	 */
	public static function hasTranslation(int $elementId, string $langCode, string $elementType = 'post'): bool
	{
		return self::getTranslation($elementId, $langCode, $elementType) !== null;
	}

	/**
	 * Get the element ID of a translation for a specific language.
	 */
	public static function getTranslation(int $elementId, string $langCode, string $elementType = 'post'): ?int
	{
		$group = self::getTranslationGroup($elementId, $elementType);

		return isset($group[$langCode]) ? $group[$langCode] : null;
	}

	/**
	 * Get the translation status for a specific language.
	 */
	public static function getTranslationStatus(int $elementId, string $langCode, string $elementType = 'post'): ?string
	{
		$resolvedType = str_contains($elementType, '_') ? $elementType : self::resolveElementType($elementId);

		$translationId = self::getTranslation($elementId, $langCode, $resolvedType);
		if ($translationId === null) {
			return null;
		}

		$targetType = str_contains($elementType, '_') ? $elementType : self::resolveElementType($translationId);
		$record = TranslationRepository::findByElementAndLanguage($translationId, $targetType, $langCode);

		return $record !== null ? $record->status : null;
	}

	/**
	 * Get the language code assigned to an element.
	 *
	 * Falls back to the default language when no translation record exists.
	 */
	public static function getElementLanguage(int $elementId, string $elementType = 'post'): string
	{
		$resolvedType = str_contains($elementType, '_') ? $elementType : self::resolveElementType($elementId);

		$record = TranslationRepository::findByElement($elementId, $resolvedType);

		return $record !== null
			? $record->language_code
			: LanguageManager::load()->getDefaultLanguageCode();
	}

	/**
	 * Resolve element type for a post (e.g. 'post_post', 'post_page', 'post_product').
	 */
	public static function resolveElementType(int $postId): string
	{
		$postType = get_post_type($postId);

		return $postType !== false ? 'post_' . $postType : 'post_post';
	}

	/**
	 * Resolve element type for a taxonomy term (e.g. 'tax_category', 'tax_post_tag').
	 */
	public static function resolveTermElementType(int $termId, string $taxonomy): string
	{
		return 'tax_' . $taxonomy;
	}
}
