<?php
/**
 * Term Meta Manager
 *
 * @package SEOvaultSEO
 */

namespace SEOvaultSEO\Core;

/**
 * Handles SEO term meta with safe getter/setter methods.
 *
 * Keys match ImporterUtils / SeoAssessment (`_seovault_seo_title`, etc.).
 */
class TermMeta {

	/**
	 * Meta key prefix.
	 */
	const PREFIX = '_seovault_';

	/**
	 * Get term meta value.
	 *
	 * @param int    $term_id Term ID.
	 * @param string $key     Meta key (without prefix).
	 * @param mixed  $default Default value.
	 * @return mixed
	 */
	public static function get( $term_id, $key, $default = '' ) {
		$value = get_term_meta( (int) $term_id, self::PREFIX . $key, true );
		if ( '' === $value || false === $value || null === $value ) {
			return $default;
		}
		return $value;
	}

	/**
	 * Set term meta value.
	 *
	 * @param int    $term_id Term ID.
	 * @param string $key     Meta key (without prefix).
	 * @param mixed  $value   Meta value.
	 * @return bool|int
	 */
	public static function set( $term_id, $key, $value ) {
		return update_term_meta( (int) $term_id, self::PREFIX . $key, $value );
	}

	/**
	 * Delete term meta value.
	 *
	 * @param int    $term_id Term ID.
	 * @param string $key     Meta key (without prefix).
	 * @return bool
	 */
	public static function delete( $term_id, $key ) {
		return delete_term_meta( (int) $term_id, self::PREFIX . $key );
	}

	/**
	 * Get SEO title.
	 *
	 * @param int $term_id Term ID.
	 * @return string
	 */
	public static function get_seo_title( $term_id ) {
		return (string) self::get( $term_id, 'seo_title', '' );
	}

	/**
	 * Set SEO title.
	 *
	 * @param int    $term_id Term ID.
	 * @param string $title   SEO title.
	 * @return bool|int
	 */
	public static function set_seo_title( $term_id, $title ) {
		return self::set( $term_id, 'seo_title', $title );
	}

	/**
	 * Get SEO description.
	 *
	 * @param int $term_id Term ID.
	 * @return string
	 */
	public static function get_seo_description( $term_id ) {
		return (string) self::get( $term_id, 'seo_description', '' );
	}

	/**
	 * Set SEO description.
	 *
	 * @param int    $term_id     Term ID.
	 * @param string $description SEO description.
	 * @return bool|int
	 */
	public static function set_seo_description( $term_id, $description ) {
		return self::set( $term_id, 'seo_description', $description );
	}

	/**
	 * Get canonical URL.
	 *
	 * @param int $term_id Term ID.
	 * @return string
	 */
	public static function get_canonical_url( $term_id ) {
		return (string) self::get( $term_id, 'canonical_url', '' );
	}

	/**
	 * Set canonical URL.
	 *
	 * @param int    $term_id Term ID.
	 * @param string $url     Canonical URL.
	 * @return bool|int
	 */
	public static function set_canonical_url( $term_id, $url ) {
		return self::set( $term_id, 'canonical_url', $url );
	}

	/**
	 * Whether noindex is set.
	 *
	 * @param int $term_id Term ID.
	 * @return bool
	 */
	public static function is_noindex( $term_id ) {
		return (bool) self::get( $term_id, 'robots_noindex', false );
	}

	/**
	 * Whether an explicit robots_noindex value is stored.
	 *
	 * @param int $term_id Term ID.
	 * @return bool
	 */
	public static function has_noindex_meta( $term_id ) {
		return metadata_exists( 'term', (int) $term_id, self::PREFIX . 'robots_noindex' );
	}

	/**
	 * Set noindex.
	 *
	 * @param int  $term_id Term ID.
	 * @param bool $noindex Whether to noindex.
	 * @return bool|int
	 */
	public static function set_noindex( $term_id, $noindex ) {
		return self::set( $term_id, 'robots_noindex', (bool) $noindex );
	}

	/**
	 * Whether nofollow is set.
	 *
	 * @param int $term_id Term ID.
	 * @return bool
	 */
	public static function is_nofollow( $term_id ) {
		return (bool) self::get( $term_id, 'robots_nofollow', false );
	}

	/**
	 * Set nofollow.
	 *
	 * @param int  $term_id  Term ID.
	 * @param bool $nofollow Whether to nofollow.
	 * @return bool|int
	 */
	public static function set_nofollow( $term_id, $nofollow ) {
		return self::set( $term_id, 'robots_nofollow', (bool) $nofollow );
	}

	/**
	 * Get OG title.
	 *
	 * @param int $term_id Term ID.
	 * @return string
	 */
	public static function get_og_title( $term_id ) {
		return (string) self::get( $term_id, 'og_title', '' );
	}

	/**
	 * Set OG title.
	 *
	 * @param int    $term_id Term ID.
	 * @param string $title   OG title.
	 * @return bool|int
	 */
	public static function set_og_title( $term_id, $title ) {
		return self::set( $term_id, 'og_title', $title );
	}

	/**
	 * Get OG description.
	 *
	 * @param int $term_id Term ID.
	 * @return string
	 */
	public static function get_og_description( $term_id ) {
		return (string) self::get( $term_id, 'og_description', '' );
	}

	/**
	 * Set OG description.
	 *
	 * @param int    $term_id     Term ID.
	 * @param string $description OG description.
	 * @return bool|int
	 */
	public static function set_og_description( $term_id, $description ) {
		return self::set( $term_id, 'og_description', $description );
	}

	/**
	 * Get OG image.
	 *
	 * @param int $term_id Term ID.
	 * @return string
	 */
	public static function get_og_image( $term_id ) {
		return (string) self::get( $term_id, 'og_image', '' );
	}

	/**
	 * Set OG image.
	 *
	 * @param int    $term_id Term ID.
	 * @param string $url     OG image URL.
	 * @return bool|int
	 */
	public static function set_og_image( $term_id, $url ) {
		return self::set( $term_id, 'og_image', $url );
	}
}
