<?php

namespace App\Support;

use App\Models\AppSetting;
use Illuminate\Support\Facades\Crypt;
use Throwable;

/**
 * Resolves WhatsApp Cloud API credentials from app_settings (DB) with fallback to config/.env.
 */
class WhatsAppConfig
{
    private const KEY_TOKEN_ENC = 'whatsapp_access_token_enc';

    private const KEY_APP_SECRET_ENC = 'whatsapp_app_secret_enc';

    private const KEY_PHONE_NUMBER_ID = 'whatsapp_phone_number_id';

    private const KEY_VERIFY_TOKEN = 'whatsapp_verify_token';

    private const KEY_API_VERSION = 'whatsapp_api_version';

    public static function accessToken(): string
    {
        $enc = AppSetting::getValue(self::KEY_TOKEN_ENC);
        if ($enc !== null && $enc !== '') {
            try {
                return Crypt::decryptString($enc);
            } catch (Throwable) {
            }
        }

        return (string) (config('services.whatsapp.token') ?? '');
    }

    public static function appSecret(): string
    {
        $enc = AppSetting::getValue(self::KEY_APP_SECRET_ENC);
        if ($enc !== null && $enc !== '') {
            try {
                return Crypt::decryptString($enc);
            } catch (Throwable) {
            }
        }

        return (string) (config('services.whatsapp.app_secret') ?? '');
    }

    public static function phoneNumberId(): string
    {
        $v = AppSetting::getValue(self::KEY_PHONE_NUMBER_ID);

        return $v !== null && $v !== ''
            ? $v
            : (string) (config('services.whatsapp.phone_number_id') ?? '');
    }

    public static function verifyToken(): string
    {
        $v = AppSetting::getValue(self::KEY_VERIFY_TOKEN);

        return $v !== null && $v !== ''
            ? $v
            : (string) (config('services.whatsapp.verify_token') ?? '');
    }

    public static function apiVersion(): string
    {
        $v = AppSetting::getValue(self::KEY_API_VERSION);

        return $v !== null && $v !== ''
            ? $v
            : (string) (config('services.whatsapp.api_version') ?? 'v21.0');
    }

    public static function baseUrl(): string
    {
        return 'https://graph.facebook.com/' . self::apiVersion();
    }

    public static function hasStoredAccessToken(): bool
    {
        $v = AppSetting::getValue(self::KEY_TOKEN_ENC);

        return $v !== null && $v !== '';
    }

    public static function hasStoredAppSecret(): bool
    {
        $v = AppSetting::getValue(self::KEY_APP_SECRET_ENC);

        return $v !== null && $v !== '';
    }

    public static function hasStoredPhoneNumberId(): bool
    {
        $v = AppSetting::getValue(self::KEY_PHONE_NUMBER_ID);

        return $v !== null && $v !== '';
    }

    public static function hasStoredVerifyToken(): bool
    {
        $v = AppSetting::getValue(self::KEY_VERIFY_TOKEN);

        return $v !== null && $v !== '';
    }

    public static function hasStoredApiVersion(): bool
    {
        $v = AppSetting::getValue(self::KEY_API_VERSION);

        return $v !== null && $v !== '';
    }

    public static function keyTokenEnc(): string
    {
        return self::KEY_TOKEN_ENC;
    }

    public static function keyAppSecretEnc(): string
    {
        return self::KEY_APP_SECRET_ENC;
    }

    public static function keyPhoneNumberId(): string
    {
        return self::KEY_PHONE_NUMBER_ID;
    }

    public static function keyVerifyToken(): string
    {
        return self::KEY_VERIFY_TOKEN;
    }

    public static function keyApiVersion(): string
    {
        return self::KEY_API_VERSION;
    }
}
