<?php

namespace Licenzo\Services\Notification;

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

use Licenzo\Helper\Action;

class NotificationHookHandler
{
    public static function register(): void
    {
        Action::listen('notification_customer_welcome', [static::class, 'handleCustomerWelcome'], 10, 1);
        Action::listen('notification_license_activation', [static::class, 'handleLicenseActivation'], 10, 1);
        Action::listen('notification_maximum_activations', [static::class, 'handleMaximumActivations'], 10, 1);
        Action::listen('notification_license_expiration', [static::class, 'handleLicenseExpiration'], 10, 1);
        Action::listen('notification_license_expiry_warning', [static::class, 'handleLicenseExpiryWarning'], 10, 1);
        Action::listen('notification_renewal_reminder', [static::class, 'handleRenewalReminder'], 10, 1);
        Action::listen('notification_payment_failed', [static::class, 'handlePaymentFailed'], 10, 1);
        Action::listen('notification_grace_period_started', [static::class, 'handleGracePeriodStarted'], 10, 1);
        Action::listen('notification_subscription_suspended', [static::class, 'handleSubscriptionSuspended'], 10, 1);
        Action::listen('notification_subscription_renewed', [static::class, 'handleSubscriptionRenewed'], 10, 1);
        Action::listen('notification_subscription_paused', [static::class, 'handleSubscriptionPaused'], 10, 1);
        Action::listen('notification_subscription_resumed', [static::class, 'handleSubscriptionResumed'], 10, 1);
        Action::listen('notification_subscription_canceled', [static::class, 'handleSubscriptionCanceled'], 10, 1);
        Action::listen('notification_subscription_requires_action', [static::class, 'handleSubscriptionRequiresAction'], 10, 1);
    }

    public static function handleCustomerWelcome($payload = []): void
    {
        static::factory()->sendCustomerWelcome(static::normalizePayload($payload));
    }

    public static function handleLicenseActivation($payload = []): void
    {
        static::factory()->sendLicenseActivation(static::normalizePayload($payload));
    }

    public static function handleMaximumActivations($payload = []): void
    {
        static::factory()->sendMaximumActivations(static::normalizePayload($payload));
    }

    public static function handleLicenseExpiration($payload = []): void
    {
        static::factory()->sendLicenseExpiration(static::normalizePayload($payload));
    }

    public static function handleLicenseExpiryWarning($payload = []): void
    {
        static::factory()->sendLicenseExpiryWarning(static::normalizePayload($payload));
    }

    public static function handleRenewalReminder($payload = []): void
    {
        static::factory()->sendRenewalReminder(static::normalizePayload($payload));
    }

    public static function handlePaymentFailed($payload = []): void
    {
        static::factory()->sendPaymentFailedNotification(static::normalizePayload($payload));
    }

    public static function handleGracePeriodStarted($payload = []): void
    {
        static::factory()->sendGracePeriodNotification(static::normalizePayload($payload));
    }

    public static function handleSubscriptionSuspended($payload = []): void
    {
        static::factory()->sendSubscriptionSuspendedNotification(static::normalizePayload($payload));
    }

    public static function handleSubscriptionRenewed($payload = []): void
    {
        static::factory()->sendSubscriptionRenewedNotification(static::normalizePayload($payload));
    }

    public static function handleSubscriptionPaused($payload = []): void
    {
        static::factory()->sendSubscriptionPausedNotification(static::normalizePayload($payload));
    }

    public static function handleSubscriptionResumed($payload = []): void
    {
        static::factory()->sendSubscriptionResumedNotification(static::normalizePayload($payload));
    }

    public static function handleSubscriptionCanceled($payload = []): void
    {
        static::factory()->sendSubscriptionCanceledNotification(static::normalizePayload($payload));
    }

    public static function handleSubscriptionRequiresAction($payload = []): void
    {
        static::factory()->sendSubscriptionRequiresActionNotification(static::normalizePayload($payload));
    }

    private static function factory(): EmailNotificationService
    {
        return EmailNotificationService::factory();
    }

    /**
     * @return array<string,mixed>
     */
    private static function normalizePayload($payload): array
    {
        return is_array($payload) ? $payload : [];
    }
}
