<?php
/**
 * Registers YeePDF as a Forminator Integration so PDF template settings
 * show up natively in the form builder (Integrations tab) and on the
 * Forminator > Integrations page.
 */
if (!defined('ABSPATH'))
    exit; // Exit if accessed directly

if (!defined('YEEPDF_FORMINATOR_INTEGRATION_VERSION')) {
    define('YEEPDF_FORMINATOR_INTEGRATION_VERSION', '1.0');
}

function yeepdf_forminator_integration_dir()
{
    return trailingslashit(__DIR__);
}

function yeepdf_forminator_integration_url()
{
    return trailingslashit(YEEPDF_FORMINATOR_PLUGIN_URL . 'forminator/integration');
}

/**
 * Get the PDF connection settings saved for a form via the Integrations tab.
 * Returns the first connection array (template_id, file_name, password,
 * save_dropbox) or an empty array when the form has no connection.
 *
 * @param int $form_id Form ID.
 *
 * @return array
 */
function yeepdf_forminator_get_form_settings($form_id)
{
    $addon_settings = get_post_meta($form_id, 'forminator_addon_yeepdf_form_settings', true);
    if (is_array($addon_settings)) {
        foreach ($addon_settings as $connection) {
            if (!empty($connection['template_id'])) {
                return wp_parse_args(
                    $connection,
                    array(
                        'template_id'  => 0,
                        'file_name'    => '',
                        'password'     => '',
                        'save_dropbox' => 0,
                    )
                );
            }
        }
    }

    return array();
}

/**
 * Resolve the PDF template selected for a form.
 * Integration settings (Integrations tab) win, legacy meta from the old
 * PDF tab UI is used as fallback.
 *
 * @param int $form_id Form ID.
 *
 * @return int
 */
function yeepdf_forminator_get_selected_template($form_id)
{
    $connection = yeepdf_forminator_get_form_settings($form_id);
    if (!empty($connection['template_id'])) {
        return (int) $connection['template_id'];
    }

    return (int) get_post_meta($form_id, '_yeepdf_selected_template', true);
}

/**
 * Whether a Dropbox account is connected globally (client id + access token).
 *
 * @return bool
 */
function yeepdf_forminator_dropbox_connected()
{
    $client_id = get_option('pdf_creator_dropbox_token', '');
    if (empty($client_id)) {
        return false;
    }
    $token = get_option('_yeepdf_dropbox_api_token', '');

    return is_array($token) && !empty($token['access_token']);
}

add_action('forminator_addons_loaded', 'yeepdf_forminator_register_integration');
function yeepdf_forminator_register_integration()
{
    if (!class_exists('Forminator_Integration_Loader') || !class_exists('Forminator_Integration')) {
        return;
    }
    require_once yeepdf_forminator_integration_dir() . 'class-forminator-yeepdf.php';
    require_once yeepdf_forminator_integration_dir() . 'class-forminator-yeepdf-form-settings.php';
    Forminator_Integration_Loader::get_instance()->register('Forminator_Yeepdf');
}
