<?php
/**
 * Author Kit Social Profile Class File
 *
 * This file contains the Author_Kit_Social_Profile class responsible for managing
 * social media platform data within Author Kit. It includes retrieving supported
 * platforms, handling selected platform metadata, and caching platform information.
 *
 * @package Author_Kit
 * @since 1.0.0
 */

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

/**
 * Handles social platform data and caching for Author Kit profiles.
 *
 * Provides methods to retrieve supported platforms, fetch selected platforms
 * with metadata, and manage caching of platform information.
 */
class Author_Kit_Social_Profile {



	/**
	 * Get supported platforms with details including name, color, and icon class.
	 *
	 * @return array<string, array{name:string, color:string, fa_icon:string}>
	 *         Associative array of platforms keyed by platform identifier.
	 */
	public static function get_supported_platforms() {
		return array(
			'facebook'           => array(
				'name'    => 'Facebook',
				'color'   => '#1877f2',
				'fa_icon' => 'fa-brands fa-facebook',
			),
			'twitter'            => array(
				'name'    => 'Twitter (Old)',
				'color'   => '#1DA1F2',
				'fa_icon' => 'fa-brands fa-twitter',
			),
			'square-x-twitter'   => array(
				'name'    => 'Twitter X (New)',
				'color'   => '#050505',
				'fa_icon' => 'fa fa-brands fa-x-twitter',
			),
			'linkedin'           => array(
				'name'    => 'LinkedIn',
				'color'   => '#0077b5',
				'fa_icon' => 'fa-brands fa-linkedin-in',
			),
			'instagram'          => array(
				'name'    => 'Instagram',
				'color'   => '#e4405f',
				'fa_icon' => 'fa-brands fa-instagram',
			),
			'youtube'            => array(
				'name'    => 'YouTube',
				'color'   => '#ff0000',
				'fa_icon' => 'fa-brands fa-youtube',
			),
			'github'             => array(
				'name'    => 'GitHub',
				'color'   => '#333333',
				'fa_icon' => 'fa-brands fa-github',
			),
			'pinterest'          => array(
				'name'    => 'Pinterest',
				'color'   => '#bd081c',
				'fa_icon' => 'fa-brands fa-pinterest',
			),
			'tiktok'             => array(
				'name'    => 'TikTok',
				'color'   => '#000000',
				'fa_icon' => 'fa-brands fa-tiktok',
			),
			'snapchat'           => array(
				'name'    => 'Snapchat',
				'color'   => '#fffc00',
				'fa_icon' => 'fa-brands fa-snapchat',
			),
			'whatsapp'           => array(
				'name'    => 'WhatsApp',
				'color'   => '#25d366',
				'fa_icon' => 'fa-brands fa-whatsapp',
			),
			'telegram'           => array(
				'name'    => 'Telegram',
				'color'   => '#26a5e4',
				'fa_icon' => 'fa-brands fa-telegram',
			),
			'discord'            => array(
				'name'    => 'Discord',
				'color'   => '#5865f2',
				'fa_icon' => 'fa-brands fa-discord',
			),
			'twitch'             => array(
				'name'    => 'Twitch',
				'color'   => '#9146ff',
				'fa_icon' => 'fa-brands fa-twitch',
			),
			'medium'             => array(
				'name'    => 'Medium',
				'color'   => '#00ab6c',
				'fa_icon' => 'fa-brands fa-medium',
			),
			'bluesky'            => array(
				'name'    => 'Bluesky',
				'color'   => '#0085ff',
				'fa_icon' => 'fa fa-brands fa-bluesky',
			),
			'mastodon'           => array(
				'name'    => 'Mastodon',
				'color'   => '#6364ff',
				'fa_icon' => 'fa-brands fa-mastodon',
			),
			'threads'            => array(
				'name'    => 'Threads',
				'color'   => '#000000',
				'fa_icon' => 'fa-brands fa-threads',
			),
			'dribbble'           => array(
				'name'    => 'Dribbble',
				'color'   => '#ea4c89',
				'fa_icon' => 'fa-brands fa-dribbble',
			),
			'behance'            => array(
				'name'    => 'Behance',
				'color'   => '#0057ff',
				'fa_icon' => 'fa-brands fa-behance',
			),
			'vk'                 => array(
				'name'    => 'VK',
				'color'   => '#4c75a3',
				'fa_icon' => 'fa-brands fa-vk',
			),
			'flickr'             => array(
				'name'    => 'Flickr',
				'color'   => '#ff0084',
				'fa_icon' => 'fa-brands fa-flickr',
			),
			'spotify'            => array(
				'name'    => 'Spotify',
				'color'   => '#1db954',
				'fa_icon' => 'fa-brands fa-spotify',
			),
			'apple'              => array(
				'name'    => 'Apple',
				'color'   => '#000000',
				'fa_icon' => 'fa-brands fa-apple',
			),
			'tumblr'             => array(
				'name'    => 'Tumblr',
				'color'   => '#36465d',
				'fa_icon' => 'fa-brands fa-tumblr',
			),
			'yelp'               => array(
				'name'    => 'Yelp',
				'color'   => '#d32323',
				'fa_icon' => 'fa-brands fa-yelp',
			),
			'stack-overflow'     => array(
				'name'    => 'Stack Overflow',
				'color'   => '#f48024',
				'fa_icon' => 'fa-brands fa-stack-overflow',
			),
			'quora'              => array(
				'name'    => 'Quora',
				'color'   => '#b92b27',
				'fa_icon' => 'fa-brands fa-quora',
			),
			'weibo'              => array(
				'name'    => 'Weibo',
				'color'   => '#e6162d',
				'fa_icon' => 'fa-brands fa-weibo',
			),
			'line'               => array(
				'name'    => 'LINE',
				'color'   => '#00c300',
				'fa_icon' => 'fa-brands fa-line',
			),
			'xing'               => array(
				'name'    => 'Xing',
				'color'   => '#026466',
				'fa_icon' => 'fa-brands fa-xing',
			),
			'vimeo'              => array(
				'name'    => 'Vimeo',
				'color'   => '#1ab7ea',
				'fa_icon' => 'fa-brands fa-vimeo',
			),
			'soundcloud'         => array(
				'name'    => 'SoundCloud',
				'color'   => '#ff5500',
				'fa_icon' => 'fa-brands fa-soundcloud',
			),
			'goodreads'          => array(
				'name'    => 'Goodreads',
				'color'   => '#372213',
				'fa_icon' => 'fa fa-brands fa-goodreads',
			),
			'wordpress'          => array(
				'name'    => 'WordPress',
				'color'   => '#21759b',
				'fa_icon' => 'fa-brands fa-wordpress-simple',
			),
			'viber'              => array(
				'name'    => 'Viber',
				'color'   => '#665cac',
				'fa_icon' => 'fa-brands fa-viber',
			),
			'hubspot'            => array(
				'name'    => 'HubSpot',
				'color'   => '#ff7a59',
				'fa_icon' => 'fa-brands fa-hubspot',
			),
			'blogger'            => array(
				'name'    => 'Blogger',
				'color'   => '#ff5722',
				'fa_icon' => 'fa-brands fa-blogger-b',
			),
			'wpbeginner'         => array(
				'name'    => 'WPBeginner',
				'color'   => '#f47c20',
				'fa_icon' => 'fa-brands fa-wpbeginner',
			),
			'dev'                => array(
				'name'    => 'DEV Community',
				'color'   => '#0a0a0a',
				'fa_icon' => 'fa-brands fa-dev',
			),
			'meetup'             => array(
				'name'    => 'Meetup',
				'color'   => '#f64060',
				'fa_icon' => 'fa-brands fa-meetup',
			),
			'steam'              => array(
				'name'    => 'Steam',
				'color'   => '#171a21',
				'fa_icon' => 'fa-brands fa-steam',
			),
			'gitlab'             => array(
				'name'    => 'GitLab',
				'color'   => '#fca121',
				'fa_icon' => 'fa-brands fa-gitlab',
			),
			'slack'              => array(
				'name'    => 'Slack',
				'color'   => '#4A154B',
				'fa_icon' => 'fa-brands fa-slack',
			),
			'facebook-messenger' => array(
				'name'    => 'Messenger',
				'color'   => '#0099ff',
				'fa_icon' => 'fa-brands fa-facebook-messenger',
			),
			'skype'              => array(
				'name'    => 'Skype',
				'color'   => '#00aff0',
				'fa_icon' => 'fa-brands fa-skype',
			),
			'patreon'            => array(
				'name'    => 'Patreon',
				'color'   => '#f96854',
				'fa_icon' => 'fa-brands fa-patreon',
			),
			'codepen'            => array(
				'name'    => 'CodePen',
				'color'   => '#000000',
				'fa_icon' => 'fa-brands fa-codepen',
			),
			'researchgate'       => array(
				'name'    => 'ResearchGate',
				'color'   => '#00ccbb',
				'fa_icon' => 'fa-solid fa-flask',
			),
			'kickstarter'        => array(
				'name'    => 'Kickstarter',
				'color'   => '#2bde73',
				'fa_icon' => 'fa-brands fa-kickstarter',
			),
			'paypal'             => array(
				'name'    => 'PayPal',
				'color'   => '#00457c',
				'fa_icon' => 'fa-brands fa-paypal',
			),
		);
	}

	/**
	 * Retrieves metadata for selected platforms including name, color, and icon.
	 *
	 * Uses cached data if available; otherwise, fetches selected platforms,
	 * maps their details from supported platforms, caches the result for one year,
	 * and returns the platforms metadata.
	 *
	 * @return array<string, array{name:string, color:string, fa_icon:string}>
	 *         Metadata of selected platforms keyed by platform slug.
	 */
	public static function get_selected_platforms_with_meta() {
		$cached = get_transient( 'author_kit_selected_platforms_meta' );

		if ( false !== $cached ) {
			return $cached;
		}

		$selected_platforms  = get_option( 'author_kit_selected_platforms', array() );
		$supported_platforms = self::get_supported_platforms();

		$platforms_meta = array();

		foreach ( $selected_platforms as $platform ) {
			if ( isset( $supported_platforms[ $platform ] ) ) {
				$platforms_meta[ $platform ] = array(
					'name'    => $supported_platforms[ $platform ]['name'],
					'color'   => $supported_platforms[ $platform ]['color'],
					'fa_icon' => $supported_platforms[ $platform ]['fa_icon'],
				);
			}
		}

		// Cache for 1 year (can be adjusted).
		set_transient( 'author_kit_selected_platforms_meta', $platforms_meta, YEAR_IN_SECONDS );
		update_option( 'author_kit_selected_platforms_meta', $platforms_meta );

		return $platforms_meta;
	}

	/**
	 * Clears the cached metadata of selected platforms.
	 *
	 * Deletes the transient cache holding selected platforms data.
	 *
	 * @return void
	 */
	public static function clear_selected_platforms_cache() {
		delete_transient( 'author_kit_selected_platforms_meta' );
	}
}

// Hook to clear cache when platforms setting is updated.
add_action( 'update_option_author_kit_selected_platforms', array( 'Author_Kit_Social_Profile', 'clear_selected_platforms_cache' ), 10, 2 );
