<?php

namespace PloverCRM\Modules\PushNotifications;

use PloverCRM\Core\AbstractModule;

/**
 * Push Notifications Module
 * 
 * Handles sending push notifications to mobile app when new contacts are created
 */
class PushNotificationsModule extends AbstractModule
{
    protected $id = 'push_notifications';
    protected $dependencies = [];

    /**
     * {@inheritdoc}
     */
    public function getMetadata(): array
    {
        return [
            'name' => 'Push Notifications',
            'description' => 'Send push notifications to mobile devices when new contacts are created',
            'version' => '1.0.0',
            'author' => 'WebPlover',
            'required' => false,
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function register(): void
    {
        // Register settings
        $settings = new Settings();
        $settings->register();
    }

    /**
     * {@inheritdoc}
     */
    public function boot(): void
    {
        // Hook into the scheduled contact enrichment action
        add_action('plover_crm_after_contact_created_actions', [$this, 'handleContactCreated'], 20, 6);
    }

    /**
     * {@inheritdoc}
     */
    public function isEnabled(): bool
    {
        // Only load if CRM is available
        $crm = $this->container->get('crm');
        return $crm->isAvailable();
    }

    /**
     * Handle contact created event
     * 
     * @param int $subscriber_id
     * @param string $userIp
     * @param string $userAgent
     * @param string $current_url
     * @param array $cookie_data
     */
    public function handleContactCreated($subscriber_id, $userIp, $userAgent, $current_url, $cookie_data, $provider_id = ''): void
    {
        Services\NotificationSender::sendNewContactNotification($subscriber_id, (string) $provider_id);
    }
}
