<?php
declare(strict_types=1);

namespace WP_AUX\Enum;

use WP_AUX\Interfaces\OptionsEnumInterface;
use WP_AUX\Traits\EnumTraits;

enum SocialMediaProfilesEnum: string implements OptionsEnumInterface
{
  use EnumTraits;

  case Facebook = 'facebook';
  case Instagram = 'instagram';
  case LinkedIn = 'linkedin';
  case Reddit = 'reddit';
  case Snapchat = 'snapchat';
  case X = 'x';
  case YouTube = 'youtube';

  public const SETTING_LABEL = 'Social Media Options';

  public function label(): string
  {
    return match($this) {
      self::Facebook => 'Facebook',
      self::Instagram => 'Instagram',
      self::X => 'X (Formerly Twitter)',
      self::LinkedIn => 'LinkedIn',
      self::Reddit => 'Reddit',
      self::Snapchat => 'Snapchat',
      self::YouTube => 'YouTube',
    };
  }

  /**
   * Get the AUX icon for a social media platform
   * @since 1.0.0
   */
  public function icon(): IconsEnum
  {
    return match($this) {
      self::Facebook => IconsEnum::FACEBOOK,
      self::Instagram => IconsEnum::INSTAGRAM,
      self::X => IconsEnum::X_TWITTER,
      self::LinkedIn => IconsEnum::LINKEDIN,
      self::Reddit => IconsEnum::REDDIT,
      self::Snapchat => IconsEnum::SNAPCHAT,
      self::YouTube => IconsEnum::YOUTUBE,
    };
  }

  /**
   * Get the URL for the social media platform
   *
   * If a user provides a URL for their "handle", that will be returned.
   *
   * @since 1.0.0
   */
  public function url(?string $handle): ?string
  {
    if (empty($handle)) {
      return null;
    }

    /**
     * If the "handle" is a URL, return it.
     *
     * This supports cases where a user does not have a handle for a social media platform,
     * or did not at the time of configuring the social media link.
     */
    if (filter_var($handle, FILTER_VALIDATE_URL)) {
      return $handle;
    }

    $pattern = match($this) {
      self::Facebook => 'https://www.facebook.com/%s/',
      self::Instagram => 'https://www.instagram.com/%s/',
      self::X => 'https://x.com/%s/',
      self::LinkedIn => 'https://www.linkedin.com/in/%s/',
      self::Reddit => 'https://www.reddit.com/user/%s/',
      self::Snapchat => 'https://www.snapchat.com/add/%s',
      self::YouTube => 'https://www.youtube.com/%s/',
    };

    return sprintf($pattern, $handle);
  }
}
