<?php

namespace Formatrica\Capability;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Canonical capability key registry for Formatrica.
 *
 * Keys listed here are a public API. They may appear in saved form config,
 * in REST payloads, and in the builder boot data. New keys may be added;
 * existing keys must NOT be renamed or removed without an explicit migration
 * plan.
 *
 */
final class Capabilities
{
    // Authoring / layout
    // LAYOUT_TWO_COLUMN is Free-granted (RELEASE_N_GRANTS) — historical key, no longer gates.
    public const LAYOUT_TWO_COLUMN = 'layout.two_column';
    public const FORMS_MULTI_STEP  = 'forms.multi_step';
    public const FORMS_WRAPPER_CLASS = 'forms.wrapper_class';
    public const FORMS_MODAL          = 'forms.modal';
    public const FORMS_QUICK_ACTIONS  = 'forms.quick_actions';
    public const FORMS_THEMES         = 'forms.themes';

    // Logic
    public const LOGIC_VISIBILITY         = 'logic.visibility';
    public const LOGIC_REQUIRED_AND_STEPS = 'logic.required_and_steps';

    // Submissions
    public const SUBMISSIONS_PREVIEW  = 'submissions.preview';
    public const SUBMISSIONS_DRAFTS   = 'submissions.drafts';
    public const SUBMISSIONS_REDIRECT = 'submissions.redirect';

    // Documents
    public const DOCUMENTS_PDF = 'documents.pdf';

    // Integrations
    public const INTEGRATIONS_CRM_HUBSPOT       = 'integrations.crm_hubspot';
    public const INTEGRATIONS_CRM_SALESFORCE    = 'integrations.crm_salesforce';
    public const INTEGRATIONS_WEBHOOK_AUTH      = 'integrations.webhook_auth';
    public const INTEGRATIONS_WEBHOOK_UNLIMITED = 'integrations.webhook_unlimited';
    public const INTEGRATIONS_SMS_TWILIO        = 'integrations.sms_twilio';

    // AI
    public const AI_ASSISTANT = 'ai.assistant';

    // Pro field types
    public const FIELDS_MULTI_FILE          = 'fields.multi_file';
    public const FIELDS_CHECKBOX_GROUP      = 'fields.checkbox_group';
    public const FIELDS_CUSTOM_ATTRS        = 'fields.custom_attrs';
    public const FIELDS_VALIDATION_PRO_RULES = 'fields.validation_pro_rules';
    public const FIELDS_GEOLOCATION         = 'fields.geolocation';
    public const FIELDS_RICHTEXT            = 'fields.richtext';
    public const FIELDS_STATIC_IMAGE        = 'fields.static_image';
    public const FIELDS_DIVIDER             = 'fields.divider';
    public const FIELDS_HEADING             = 'fields.heading';
    public const FIELDS_RATING              = 'fields.rating';
    public const FIELDS_DYNAMIC_SELECT      = 'fields.dynamic_select';
    public const FIELDS_CALCULATION         = 'fields.calculation';
    public const FIELDS_SIGNATURE           = 'fields.signature'; // reserved (roadmap)

    // Payments
    public const PAYMENTS_BASIC = 'payments.basic';

    // Emails
    public const EMAILS_TEMPLATE_CONTROLS      = 'emails.template_controls';
    public const EMAILS_SUBMITTER_CONFIRMATION = 'emails.submitter_confirmation';

    // API
    public const API_HEADLESS = 'api.headless';

    /**
     * The complete canonical list of capability keys.
     *
     * Authoritative. Feature_Gate uses it to seed the default map
     * (every key -> false) before providers are merged in.
     *
     * @return list<string>
     */
    public static function all(): array
    {
        return [
            self::LAYOUT_TWO_COLUMN,
            self::FORMS_MULTI_STEP,
            self::FORMS_WRAPPER_CLASS,
            self::FORMS_MODAL,
            self::FORMS_QUICK_ACTIONS,
            self::FORMS_THEMES,
            self::LOGIC_VISIBILITY,
            self::LOGIC_REQUIRED_AND_STEPS,
            self::SUBMISSIONS_PREVIEW,
            self::SUBMISSIONS_DRAFTS,
            self::SUBMISSIONS_REDIRECT,
            self::DOCUMENTS_PDF,
            self::INTEGRATIONS_CRM_HUBSPOT,
            self::INTEGRATIONS_CRM_SALESFORCE,
            self::INTEGRATIONS_WEBHOOK_AUTH,
            self::INTEGRATIONS_WEBHOOK_UNLIMITED,
            self::INTEGRATIONS_SMS_TWILIO,
            self::AI_ASSISTANT,
            self::FIELDS_MULTI_FILE,
            self::FIELDS_CHECKBOX_GROUP,
            self::FIELDS_CUSTOM_ATTRS,
            self::FIELDS_VALIDATION_PRO_RULES,
            self::FIELDS_GEOLOCATION,
            self::FIELDS_RICHTEXT,
            self::FIELDS_STATIC_IMAGE,
            self::FIELDS_DIVIDER,
            self::FIELDS_HEADING,
            self::FIELDS_RATING,
            self::FIELDS_DYNAMIC_SELECT,
            self::FIELDS_CALCULATION,
            self::FIELDS_SIGNATURE,
            self::PAYMENTS_BASIC,
            self::EMAILS_TEMPLATE_CONTROLS,
            self::EMAILS_SUBMITTER_CONFIRMATION,
            self::API_HEADLESS,
        ];
    }

    /**
     * Whether a given key is part of the canonical registry.
     */
    public static function is_known(string $key): bool
    {
        return in_array($key, self::all(), true);
    }

    private function __construct()
    {
        // Static registry; not instantiable.
    }
}
