<?php

declare(strict_types=1);

namespace App\Framework\Core\ValueObjects;

/**
 * Link Rel Attribute Enum
 *
 * Represents valid rel attribute values for <a> and <link> elements.
 * Based on HTML Living Standard and common practices.
 */
enum LinkRel: string
{
    // Navigation
    case ALTERNATE = 'alternate';
    case AUTHOR = 'author';
    case BOOKMARK = 'bookmark';
    case CANONICAL = 'canonical';
    case HELP = 'help';
    case NEXT = 'next';
    case PREV = 'prev';
    case SEARCH = 'search';

    // Resource References
    case STYLESHEET = 'stylesheet';
    case ICON = 'icon';
    case MANIFEST = 'manifest';
    case PRECONNECT = 'preconnect';
    case PREFETCH = 'prefetch';
    case PRELOAD = 'preload';
    case PRERENDER = 'prerender';
    case DNS_PREFETCH = 'dns-prefetch';

    // Security & Privacy
    case NOFOLLOW = 'nofollow';
    case NOOPENER = 'noopener';
    case NOREFERRER = 'noreferrer';
    case EXTERNAL = 'external';

    // Licensing
    case LICENSE = 'license';
    case TAG = 'tag';

    // SEO & Content Discovery
    case AMPHTML = 'amphtml';
    case RSS = 'rss';
    case ATOM = 'atom';
    case SITEMAP = 'sitemap';

    // Pagination
    case FIRST = 'first';
    case LAST = 'last';

    /**
     * Check if rel indicates external resource
     */
    public function isExternal(): bool
    {
        return match ($this) {
            self::EXTERNAL, self::NOFOLLOW, self::NOOPENER, self::NOREFERRER => true,
            default => false,
        };
    }

    /**
     * Check if rel is for resource hints
     */
    public function isResourceHint(): bool
    {
        return match ($this) {
            self::PRECONNECT, self::PREFETCH, self::PRELOAD, self::PRERENDER, self::DNS_PREFETCH => true,
            default => false,
        };
    }

    /**
     * Check if rel is security-related
     */
    public function isSecurityRelated(): bool
    {
        return match ($this) {
            self::NOFOLLOW, self::NOOPENER, self::NOREFERRER => true,
            default => false,
        };
    }

    /**
     * Check if rel is SEO-related
     */
    public function isSeoRelated(): bool
    {
        return match ($this) {
            self::CANONICAL, self::ALTERNATE, self::AMPHTML, self::RSS, self::ATOM, self::SITEMAP => true,
            default => false,
        };
    }

    /**
     * Check if rel is for pagination
     */
    public function isPagination(): bool
    {
        return match ($this) {
            self::NEXT, self::PREV, self::FIRST, self::LAST => true,
            default => false,
        };
    }

    /**
     * Create from string
     */
    public static function fromString(string $rel): self
    {
        return self::tryFrom(strtolower(trim($rel)))
            ?? throw new \InvalidArgumentException("Invalid link rel: {$rel}");
    }

    /**
     * Try to create from string (returns null on failure)
     */
    public static function tryFromString(string $rel): ?self
    {
        return self::tryFrom(strtolower(trim($rel)));
    }

    /**
     * Combine multiple rel values
     *
     * @param self ...$rels
     * @return string Space-separated rel attribute value
     */
    public static function combine(self ...$rels): string
    {
        return implode(' ', array_map(fn(self $rel) => $rel->value, $rels));
    }
}
