<?php

declare(strict_types=1);

/**
 * DentalCare deployment-mode helpers.
 *
 * Modes:
 * - development: owner/developer copy, shows internal QA/debug/audit gates
 * - pilot: controlled clinic testing, shows go-live and role-flow QA gates
 * - production: enterprise/client operation, hides internal testing wording by default
 * - demo: demonstration/orientation copy, demo controls only
 */

if (!function_exists('dental_deployment_modes')) {
    function dental_deployment_modes(): array
    {
        return [
            'development' => 'Development',
            'pilot' => 'Pilot',
            'production' => 'Production',
            'demo' => 'Demo',
        ];
    }
}

if (!function_exists('dental_deployment_mode')) {
    function dental_deployment_mode(): string
    {
        if (function_exists('app_demo_read_only') && app_demo_read_only()) {
            return 'demo';
        }
        try {
            $mode = strtolower(trim((string) setting_get('system', 'deployment_mode', 'production')));
        } catch (Throwable) {
            $mode = 'production';
        }
        return array_key_exists($mode, dental_deployment_modes()) ? $mode : 'production';
    }
}

if (!function_exists('dental_deployment_mode_label')) {
    function dental_deployment_mode_label(?string $mode = null): string
    {
        $mode = $mode ?: dental_deployment_mode();
        $labels = dental_deployment_modes();
        return (string) ($labels[$mode] ?? 'Production');
    }
}

if (!function_exists('dental_production_mode')) {
    function dental_production_mode(): bool
    {
        return dental_deployment_mode() === 'production';
    }
}

if (!function_exists('dental_demo_deployment_mode')) {
    function dental_demo_deployment_mode(): bool
    {
        return dental_deployment_mode() === 'demo';
    }
}

if (!function_exists('dental_internal_qa_visible')) {
    function dental_internal_qa_visible(): bool
    {
        $mode = dental_deployment_mode();
        if (in_array($mode, ['development', 'pilot'], true)) {
            return true;
        }
        try {
            return (string) setting_get('system', 'show_internal_qa_tools', '0') === '1';
        } catch (Throwable) {
            return false;
        }
    }
}

if (!function_exists('dental_client_testing_wording_hidden')) {
    function dental_client_testing_wording_hidden(): bool
    {
        return dental_production_mode() && !dental_internal_qa_visible();
    }
}
