{% set mauticBaseUrl = app.request ? app.request.schemeAndHttpHost : 'https://mautic.firmoptic.com' %}

<div class="panel panel-default">
    <div class="panel-heading">
        <div class="panel-title">{{ 'mautic.dealflow.config.header'|trans }}</div>
    </div>
    <div class="panel-body">
        {{ include('@DealFlow/ApiDocs/_styles.html.twig') }}

        <div class="dealflow-api-docs">
            <h4>{{ 'mautic.dealflow.config.license_section'|trans }}</h4>
            <p class="text-muted">
                {{ 'mautic.dealflow.config.license_intro'|trans }}
            </p>

            {% set licenseStatus = form.dealflow_license_status.vars.value|default('not_configured') %}
            {% set updatesUntil = form.dealflow_license_updates_until.vars.value|default('') %}

            <div style="margin-bottom: 15px;">
                {% if licenseStatus == 'active' %}
                    {% if updatesUntil and ("now"|date("Y-m-d")) <= (updatesUntil[:10]|default('')) %}
                        <span class="label label-success" style="font-size: 13px; padding: 5px 10px;">
                            {{ 'mautic.dealflow.config.license_active'|trans({'%date%': updatesUntil[:10]}) }}
                        </span>
                    {% elseif updatesUntil %}
                        <span class="label label-warning" style="font-size: 13px; padding: 5px 10px;">
                            {{ 'mautic.dealflow.config.license_active_expired'|trans({'%date%': updatesUntil[:10]}) }}
                        </span>
                    {% else %}
                        <span class="label label-success" style="font-size: 13px; padding: 5px 10px;">
                            {{ 'mautic.dealflow.config.license_active_nodate'|trans }}
                        </span>
                    {% endif %}
                {% elseif licenseStatus == 'invalid' %}
                    <span class="label label-danger" style="font-size: 13px; padding: 5px 10px;">
                        {{ 'mautic.dealflow.config.license_invalid'|trans }}
                    </span>
                {% elseif licenseStatus == 'pending' %}
                    <span class="label label-warning" style="font-size: 13px; padding: 5px 10px;">
                        {{ 'mautic.dealflow.config.license_pending'|trans }}
                    </span>
                {% else %}
                    <span class="label label-default" style="font-size: 13px; padding: 5px 10px;">
                        {{ 'mautic.dealflow.config.license_not_configured'|trans }}
                    </span>
                {% endif %}
            </div>

            {{ form_row(form.dealflow_license_key) }}

            {{ form_widget(form.dealflow_license_status) }}
            {{ form_widget(form.dealflow_license_updates_until) }}
            {{ form_widget(form.dealflow_license_last_validated_at) }}
            {{ form_widget(form.dealflow_license_last_validation_valid) }}

            <hr />
            <h4>{{ 'mautic.dealflow.config.defaults_section'|trans }}</h4>
            <p class="text-muted">
                {{ 'mautic.dealflow.config.defaults_intro'|trans }}
            </p>
            {{ form_row(form.dealflow_default_association_labels) }}
            {{ form_row(form.dealflow_default_sources) }}

            <hr />
            <h4>General Settings</h4>
            <p class="text-muted">Currency, pagination, health thresholds, and task defaults.</p>
            {{ form_row(form.dealflow_default_currency) }}
            {{ form_row(form.dealflow_items_per_page) }}
            {{ form_row(form.dealflow_health_yellow_days) }}
            {{ form_row(form.dealflow_health_red_days) }}
            {{ form_row(form.dealflow_default_task_priority) }}

            <hr />
            <h4>{{ 'mautic.dealflow.config.webhook_section'|trans }}</h4>
            <p class="text-muted">
                {{ 'mautic.dealflow.config.webhook_intro'|trans }}
            </p>
            <p>
                <a class="btn btn-default" href="{{ path('mautic_dealflow_inbound_webhook_index') }}">
                    {{ 'mautic.dealflow.config.webhook_sources_link'|trans }}
                </a>
            </p>
            {{ form_row(form.dealflow_inbound_webhook_secret) }}
            <p class="help-block">
                {{ 'mautic.dealflow.config.webhook_secret_help'|trans }}
            </p>
            <p class="help-block">
                {{ 'mautic.dealflow.config.webhook_headers'|trans }}
            </p>
            <p><strong>{{ 'mautic.dealflow.config.webhook_example'|trans }}</strong></p>
            <pre class="dealflow-docs-code"><code>TIMESTAMP=$(date +%s)
BODY='{"external_ref":"crm-123","note_body":"Discovery completed"}'
SIGNATURE=$(printf '%s.%s' "$TIMESTAMP" "$BODY" | openssl dgst -sha256 -hmac "$WEBHOOK_SECRET" -binary | xxd -p -c 256)

curl -X POST "{{ mauticBaseUrl }}/dealflow/webhooks/inbound/src_example" \
  -H "Content-Type: application/json" \
  -H "X-DealFlow-Timestamp: $TIMESTAMP" \
  -H "X-DealFlow-Signature: $SIGNATURE" \
  -d "$BODY"</code></pre>
            <pre class="dealflow-docs-code"><code>BODY='{"external_ref":"crm-123","note_body":"Discovery completed"}'
SIGNATURE=$(printf '%s' "$BODY" | openssl dgst -sha256 -hmac "$WEBHOOK_SECRET" -binary | openssl base64)

curl -X POST "{{ mauticBaseUrl }}/dealflow/webhooks/inbound/src_example" \
  -H "Content-Type: application/json" \
  -H "Webhook-Signature: $SIGNATURE" \
  -d "$BODY"</code></pre>

            <hr />
            <h4 id="dealflow-api-docs">{{ 'mautic.dealflow.config.docs_section'|trans }}</h4>
            <p class="text-muted">
                {{ 'mautic.dealflow.config.docs_intro'|trans }}
            </p>
            {{ include('@DealFlow/ApiDocs/_documentation.html.twig') }}
            <p>
                <a class="btn btn-default" href="{{ path('mautic_dealflow_api_docs') }}">
                    {{ 'mautic.dealflow.config.docs_link'|trans }}
                </a>
            </p>
            <p class="help-block">
                {{ 'mautic.dealflow.config.docs_help'|trans }}
            </p>
        </div>
    </div>
</div>
