<?php

namespace App\Services\LinkedIn;

/**
 * The LinkedIn professional targeting facets ARB can build, in one place.
 *
 * Searchable facets are found with the typeahead finder and need a query.
 * Enumerated facets are a short fixed list from the adTargetingFacet finder.
 * Company size, age and gender URNs are not numeric, e.g.
 * urn:li:staffCountRange:(11,50), so each facet carries its own URN pattern.
 *
 * Exclusions follow LinkedIn's facet reference: age, gender, groups and
 * interface locales may only be included.
 */
class LinkedInTargetingFacets
{
    public const SMALL_AUDIENCE_MINIMUM = 300;

    /**
     * @var array<string, array{label: string, searchable: bool, urn: string, policy: bool, exclude: bool, placeholder?: string}>
     */
    private const FACETS = [
        'titles' => ['exclude' => true, 'label' => 'Job titles', 'searchable' => true, 'urn' => '/^urn:li:[A-Za-z]+:[0-9]+$/D', 'policy' => false, 'placeholder' => 'e.g. Software Engineer'],
        'jobFunctions' => ['exclude' => true, 'label' => 'Job functions', 'searchable' => false, 'urn' => '/^urn:li:function:[0-9]+$/D', 'policy' => false],
        'seniorities' => ['exclude' => true, 'label' => 'Seniorities', 'searchable' => false, 'urn' => '/^urn:li:seniority:[0-9]+$/D', 'policy' => false],
        'skills' => ['exclude' => true, 'label' => 'Skills', 'searchable' => true, 'urn' => '/^urn:li:[A-Za-z]+:[0-9]+$/D', 'policy' => false, 'placeholder' => 'e.g. Java'],
        'yearsOfExperienceRanges' => ['exclude' => true, 'label' => 'Years of experience', 'searchable' => false, 'urn' => '/^urn:li:yearsOfExperience:[0-9]+$/D', 'policy' => false],
        'employers' => ['exclude' => true, 'label' => 'Companies', 'searchable' => true, 'urn' => '/^urn:li:[A-Za-z]+:[0-9]+$/D', 'policy' => false, 'placeholder' => 'e.g. Microsoft'],
        'industries' => ['exclude' => true, 'label' => 'Industries', 'searchable' => true, 'urn' => '/^urn:li:[A-Za-z]+:[0-9]+$/D', 'policy' => false, 'placeholder' => 'e.g. Software'],
        'staffCountRanges' => ['exclude' => true, 'label' => 'Company size', 'searchable' => false, 'urn' => '/^urn:li:staffCountRange:\([0-9]+,[0-9]+\)$/D', 'policy' => false],
        'growthRate' => ['exclude' => true, 'label' => 'Company growth rate', 'searchable' => false, 'urn' => '/^urn:li:growthRate:\(-?[0-9]+,-?[0-9]+\)$/D', 'policy' => false],
        'companyCategory' => ['exclude' => true, 'label' => 'Company category', 'searchable' => false, 'urn' => '/^urn:li:organizationRankingList:[0-9]+$/D', 'policy' => false],
        'followedCompanies' => ['exclude' => true, 'label' => 'Company followers', 'searchable' => true, 'urn' => '/^urn:li:organization:[0-9]+$/D', 'policy' => false, 'placeholder' => 'e.g. Microsoft'],
        'degrees' => ['exclude' => true, 'label' => 'Degrees', 'searchable' => true, 'urn' => '/^urn:li:degree:[0-9]+$/D', 'policy' => false, 'placeholder' => 'e.g. Bachelor'],
        'fieldsOfStudy' => ['exclude' => true, 'label' => 'Fields of study', 'searchable' => true, 'urn' => '/^urn:li:fieldOfStudy:[0-9]+$/D', 'policy' => false, 'placeholder' => 'e.g. Computer Science'],
        'schools' => ['exclude' => true, 'label' => 'Member schools', 'searchable' => true, 'urn' => '/^urn:li:organization:[0-9]+$/D', 'policy' => false, 'placeholder' => 'e.g. Stanford'],
        'interests' => ['exclude' => true, 'label' => 'Member interests', 'searchable' => true, 'urn' => '/^urn:li:interest:[0-9]+$/D', 'policy' => false, 'placeholder' => 'e.g. Marketing'],
        'groups' => ['exclude' => false, 'label' => 'Member groups', 'searchable' => true, 'urn' => '/^urn:li:group:[0-9]+$/D', 'policy' => false, 'placeholder' => 'e.g. Marketing'],
        'memberBehaviors' => ['exclude' => true, 'label' => 'Member traits', 'searchable' => false, 'urn' => '/^urn:li:memberBehavior:[0-9]+$/D', 'policy' => false],
        'ageRanges' => ['exclude' => false, 'label' => 'Age', 'searchable' => false, 'urn' => '/^urn:li:ageRange:\([0-9]+,[0-9]+\)$/D', 'policy' => true],
        'genders' => ['exclude' => false, 'label' => 'Gender', 'searchable' => false, 'urn' => '/^urn:li:gender:(MALE|FEMALE)$/D', 'policy' => true],
    ];

    /**
     * Pairs LinkedIn will not combine as included filters in one audience.
     *
     * @var list<array{0: string, 1: string}>
     */
    private const INCOMPATIBLE = [
        ['titles', 'seniorities'],
        ['titles', 'jobFunctions'],
        ['employers', 'industries'],
        ['employers', 'staffCountRanges'],
    ];

    public const POLICY_WARNING = 'Age and gender targeting is not allowed for employment, housing or credit ads under LinkedIn advertising policy, and it narrows reach. Use it only when the offer genuinely depends on it.';

    /** @return list<string> */
    public static function keys(): array
    {
        return array_keys(self::FACETS);
    }

    public static function exists(string $facet): bool
    {
        return isset(self::FACETS[$facet]);
    }

    public static function label(string $facet): string
    {
        return self::FACETS[$facet]['label'] ?? $facet;
    }

    public static function searchable(string $facet): bool
    {
        return self::FACETS[$facet]['searchable'] ?? false;
    }

    public static function hasPolicyRestriction(string $facet): bool
    {
        return self::FACETS[$facet]['policy'] ?? false;
    }

    public static function placeholder(string $facet): string
    {
        return self::FACETS[$facet]['placeholder'] ?? '';
    }

    public static function excludable(string $facet): bool
    {
        return self::FACETS[$facet]['exclude'] ?? false;
    }

    /** @return list<string> */
    public static function excludableKeys(): array
    {
        return array_values(array_filter(self::keys(), self::excludable(...)));
    }

    public static function accepts(string $facet, string $urn): bool
    {
        return self::exists($facet) && preg_match(self::FACETS[$facet]['urn'], $urn) === 1;
    }

    /**
     * Facets that cannot be combined with this one.
     *
     * @return list<string>
     */
    public static function incompatibleWith(string $facet): array
    {
        $others = [];
        foreach (self::INCOMPATIBLE as [$first, $second]) {
            if ($facet === $first) {
                $others[] = $second;
            } elseif ($facet === $second) {
                $others[] = $first;
            }
        }

        return $others;
    }

    /** @return list<array{0: string, 1: string}> */
    public static function incompatiblePairs(): array
    {
        return self::INCOMPATIBLE;
    }
}
