<?php

/**
 * Central config + white-label helper.
 *
 * @package Leadite
 */
namespace Leadite;

defined( 'ABSPATH' ) || exit;
/**
 * Read-only access to plugin configuration and white-label branding.
 *
 * Agencies can override branding either via the wp-config constant
 * leadite_BRAND (array) or via the stored option 'leadite_settings'.
 */
class Config {
    const OPTION = 'leadite_settings';

    /**
     * Required capability to manage leads.
     *
     * Falls back to 'manage_options' if 'manage_leads' has not been granted yet
     * (e.g. plugin was updated without deactivation/reactivation). This prevents
     * administrators from being locked out of the plugin's admin pages.
     */
    public static function capability() {
        $cap = apply_filters( 'Leadite/capability', 'manage_leads' );
        // Safety net: if the custom capability has not been assigned to any role
        // (e.g. fresh install without activation hook firing), fall back so admins
        // can still access the plugin and trigger the self-heal in maybe_upgrade().
        if ( $cap === 'manage_leads' && !current_user_can( 'manage_leads' ) && current_user_can( 'manage_options' ) ) {
            return 'manage_options';
        }
        return $cap;
    }

    /**
     * Capability required to view (read-only) leads.
     *
     * Users with this capability can browse leads and open detail cards but
     * cannot change status, assign, add notes, delete or export.
     *
     * @return string
     */
    public static function view_capability() {
        return apply_filters( 'Leadite/view_capability', 'view_leads' );
    }

    /**
     * Admin menu icon.
     *
     * Supports Dashicons, image URLs and SVG data URIs.
     *
     * @return string
     */
    public static function menu_icon() {
        $icon = trim( (string) self::get( 'menu_icon', '' ) );
        // Replace the old default with the branded Leadite icon.
        if ( '' === $icon || 'dashicons-email-alt' === $icon ) {
            return self::default_menu_icon();
        }
        // Keep white-label/custom overrides safe and flexible.
        if ( 0 === strpos( $icon, 'dashicons-' ) || 0 === strpos( $icon, 'data:image/svg+xml;base64,' ) || 0 === strpos( $icon, 'data:image/svg+xml;utf8,' ) || filter_var( $icon, FILTER_VALIDATE_URL ) ) {
            return $icon;
        }
        return self::default_menu_icon();
    }

    /**
     * Default Leadite admin menu SVG icon.
     *
     * @return string
     */
    private static function default_menu_icon() {
        $svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" width="20" height="20" aria-hidden="true" focusable="false">
			<g fill="black" fill-rule="evenodd">
				<path d="M2.6 2.1h2.9a.8.8 0 0 1 .8.8v3.6a.8.8 0 0 1-.8.8H2.6a.8.8 0 0 1-.8-.8V2.9a.8.8 0 0 1 .8-.8zm.55 1.65a.55.55 0 1 0 0 1.1h1.8a.55.55 0 1 0 0-1.1h-1.8z"/>
				<path d="M8.55 1.55h2.9a.8.8 0 0 1 .8.8v3.6a.8.8 0 0 1-.8.8h-2.9a.8.8 0 0 1-.8-.8v-3.6a.8.8 0 0 1 .8-.8zm.6 1.65a.55.55 0 1 0 0 1.1h1.7a.55.55 0 1 0 0-1.1h-1.7z"/>
				<path d="M14.5 2.1h2.9a.8.8 0 0 1 .8.8v3.6a.8.8 0 0 1-.8.8h-2.9a.8.8 0 0 1-.8-.8V2.9a.8.8 0 0 1 .8-.8zm.55 1.65a.55.55 0 1 0 0 1.1h1.8a.55.55 0 1 0 0-1.1h-1.8z"/>

				<path d="M3.27 7.6h1.56v1.5a.9.9 0 0 0 .9.9h1.9a.78.78 0 0 1 0 1.56h-1.9a2.46 2.46 0 0 1-2.46-2.46V7.6z"/>
				<path d="M9.22 6.8h1.56v3.3a.78.78 0 0 1-1.56 0V6.8z"/>
				<path d="M16.73 7.6v1.5a2.46 2.46 0 0 1-2.46 2.46h-1.9a.78.78 0 0 1 0-1.56h1.9a.9.9 0 0 0 .9-.9V7.6h1.56z"/>

				<circle cx="10" cy="11" r="1.4"/>

				<path d="M6.9 11.05a1.7 1.7 0 0 0-1.66 1.33l-1.06 4.02a1.2 1.2 0 0 0 1.16 1.5h9.32a1.2 1.2 0 0 0 1.16-1.5l-1.06-4.02a1.7 1.7 0 0 0-1.66-1.33h-.5a2.9 2.9 0 0 1-5.2 0h-.5zm1.15 3.85h3.9a.62.62 0 1 1 0 1.24h-3.9a.62.62 0 1 1 0-1.24z"/>
			</g>
		</svg>';
        return 'data:image/svg+xml;base64,' . \base64_encode( $svg );
    }

    /**
     * Full settings array (with defaults merged in).
     *
     * @return array
     */
    public static function all() {
        $defaults = array(
            'brand_name'                => 'Leadite',
            'menu_icon'                 => self::default_menu_icon(),
            'hide_branding'             => false,
            'per_page'                  => 25,
            'enabled_sources'           => array(
                'cf7',
                'elementor',
                'wpforms',
                'ninjaforms',
                'gravityforms',
                'fluentforms'
            ),
            'google_sheets_enabled'     => false,
            'google_sheets_url'         => '',
            'telegram_enabled'          => false,
            'telegram_token'            => '',
            'telegram_chat_id'          => '',
            'telegram_quick_contact'    => false,
            'webhook_enabled'           => false,
            'webhook_url'               => '',
            'delete_on_uninstall'       => false,
            'att_filtering_enabled'     => true,
            'att_max_size_kb'           => 10240,
            'att_allowed_extensions'    => 'jpg,jpeg,png,gif,webp,pdf,doc,docx,xls,xlsx,ppt,pptx,txt,csv,zip,odt,ods',
            'anonymize_ip'              => true,
            'autopurge_enabled'         => false,
            'autopurge_days'            => 365,
            'autopurge_only_closed'     => true,
            'followup_enabled'          => true,
            'followup_not_opened_min'   => 15,
            'followup_not_closed_hours' => 24,
            'followup_hot_threshold'    => 50,
            'followup_digest_enabled'   => false,
            'followup_digest_email'     => '',
            'followup_digest_hour'      => 8,
            'followup_overdue_badge'    => true,
            'role_access'               => array(),
        );
        // wp-config constant wins for locked agency deployments.
        if ( defined( 'leadite_BRAND' ) && is_array( leadite_BRAND ) ) {
            $defaults = array_merge( $defaults, leadite_BRAND );
        }
        $stored = get_option( self::OPTION, array() );
        $stored = ( is_array( $stored ) ? $stored : array() );
        return array_merge( $defaults, $stored );
    }

    /**
     * Get a single setting.
     *
     * @param string $key     Setting key.
     * @param mixed  $default Fallback.
     * @return mixed
     */
    public static function get( $key, $default = null ) {
        $all = self::all();
        return ( array_key_exists( $key, $all ) ? $all[$key] : $default );
    }

    /**
     * White-label display name shown across the admin UI.
     *
     * @return string
     */
    public static function brand() {
        return 'Leadite';
    }

    /**
     * Whether "powered by" footer branding is hidden.
     *
     * @return bool
     */
    public static function branding_hidden() {
        return false;
    }

}
