<?php

declare(strict_types=1);

namespace App\Framework\Webhook\ValueObjects;

use InvalidArgumentException;

/**
 * Value object for webhook provider identification
 * Provides standardized provider handling with known configurations
 */
final readonly class WebhookProvider
{
    private function __construct(
        public string $name,
        public string $signatureAlgorithm = 'sha256',
        public string $signatureHeader = 'X-Signature',
        public string $eventTypeHeader = 'X-Event-Type'
    ) {
        if (empty(trim($name))) {
            throw new InvalidArgumentException('Provider name cannot be empty');
        }
    }

    public static function create(string $name): self
    {
        return new self(strtolower(trim($name)));
    }

    public static function stripe(): self
    {
        return new self(
            name: 'stripe',
            signatureAlgorithm: 'sha256',
            signatureHeader: 'Stripe-Signature',
            eventTypeHeader: 'Stripe-Event'
        );
    }

    public static function github(): self
    {
        return new self(
            name: 'github',
            signatureAlgorithm: 'sha256',
            signatureHeader: 'X-Hub-Signature-256',
            eventTypeHeader: 'X-GitHub-Event'
        );
    }

    public static function generic(string $name): self
    {
        return new self(
            name: strtolower(trim($name)),
            signatureAlgorithm: 'sha256',
            signatureHeader: 'X-Signature',
            eventTypeHeader: 'X-Event-Type'
        );
    }

    public static function legalService(): self
    {
        return new self(
            name: 'legal-service',
            signatureAlgorithm: 'sha256',
            signatureHeader: 'X-Legal-Signature',
            eventTypeHeader: 'X-Legal-Event-Type'
        );
    }

    public static function fromString(string $name): self
    {
        return match (strtolower(trim($name))) {
            'stripe' => self::stripe(),
            'github' => self::github(),
            'legal-service' => self::legalService(),
            default => self::generic($name)
        };
    }

    public function toString(): string
    {
        return $this->name;
    }

    public function equals(self $other): bool
    {
        return $this->name === $other->name;
    }

    public function isKnownProvider(): bool
    {
        return in_array($this->name, ['stripe', 'github', 'paypal', 'legal-service'], true);
    }
}
