<?php

/**
 * Integrations manager: processes Google Sheets, Telegram, and outbound Webhooks.
 *
 * SSRF guard: every user-supplied URL (Google Sheets, Webhook) is validated
 * with UrlValidator::is_safe_url() immediately before each outbound request.
 * Re-resolving the hostname at dispatch-time prevents DNS-rebinding attacks
 * where a hostname pointed to a public IP at save-time but now resolves to an
 * internal/private address.
 *
 * @package Leadite
 */
namespace Leadite\Integrations;

use Leadite\Config;
use Leadite\Pro;
use Leadite\Storage\Lead;
use Leadite\Support\UrlValidator;
defined( 'ABSPATH' ) || exit;
class IntegrationsManager {
    /**
     * Register actions.
     */
    public function boot() {
        add_action(
            'Leadite/lead_stored',
            array($this, 'dispatch_integrations'),
            10,
            2
        );
        // f-pro-end
    }

    /**
     * Process all enabled integrations for a newly stored lead.
     *
     * @param int  $submission_id Stored submission ID.
     * @param Lead $lead          The lead object.
     */
    public function dispatch_integrations( $submission_id, Lead $lead ) {
        // f-pro-end
    }

    /**
     * Sync lead data to Google Sheets via non-blocking script endpoint.
     *
     * @param int  $submission_id Submission ID.
     * @param Lead $lead          Lead object.
     */
    protected function dispatch_google_sheets( $submission_id, Lead $lead ) {
    }

    // f-pro-start
    /**
     * Dispatch lead details notification to a Telegram Chat ID.
     *
     * Note: the Telegram API hostname is hardcoded to api.telegram.org;
     * the bot token is only a URL path segment — it does not affect routing.
     * SSRF validation is therefore not applicable here.
     *
     * @param int  $submission_id Submission ID.
     * @param Lead $lead          Lead object.
     */
    protected function dispatch_telegram( $submission_id, Lead $lead ) {
    }

    /**
     * Send full lead JSON webhook payload.
     *
     * @param int  $submission_id Submission ID.
     * @param Lead $lead          Lead object.
     */
    protected function dispatch_webhook( $submission_id, Lead $lead ) {
    }

    // f-pro-end
    /**
     * Reconstruct a Lead object from stored database data.
     *
     * @param array $data Stored lead data from LeadsRepository::find().
     * @return Lead
     */
    protected function reconstruct_lead( array $data ) : Lead {
        $row = $data['row'];
        $fields = $data['fields'];
        $lead = new Lead();
        $lead->source = $row->source ?? '';
        $lead->form_id = $row->form_id ?? '';
        $lead->form_name = $row->form_name ?? '';
        foreach ( $fields as $f ) {
            $lead->fields[] = array(
                'key'       => (string) ($f->field_key ?? ''),
                'label'     => (string) ($f->field_label ?? ''),
                'value'     => (string) ($f->field_value ?? ''),
                'type'      => (string) ($f->field_type ?? 'text'),
                'canonical' => (string) ($f->canonical ?? ''),
            );
        }
        $lead->meta = array(
            'ip_address' => $row->ip ?? '',
            'user_agent' => $row->user_agent ?? '',
            'referer'    => $row->referer ?? '',
            'page_url'   => $row->page_url ?? '',
            'lang'       => $row->lang ?? '',
        );
        return $lead;
    }

    /**
     * WP-Cron handler: Telegram notification.
     *
     * @param int $submission_id Submission ID.
     */
    public function handle_dispatch_telegram( $submission_id ) {
    }

    /**
     * WP-Cron handler: Webhook notification.
     *
     * @param int $submission_id Submission ID.
     */
    public function handle_dispatch_webhook( $submission_id ) {
    }

}
