"""Config service cons"""

from typing import Literal
from azure.identity import DefaultAzureCredential
from savcfg.keyvault import KeyVaultVars
from .read import ConfigService
from .edit import ConfigEditor


def get_savia_config_service(
    env: Literal["prod", "pre", "devel"], app_name: str
) -> ConfigService:
    """Construct the config service for"""
    readers = []
    credential = DefaultAzureCredential()
    match env:
        case "prod":
            readers = [
                KeyVaultVars("savia-production", credential),
                KeyVaultVars("savia-common", credential),
            ]
        case "pre":
            readers = [
                KeyVaultVars("savia-preproduction", credential),
                KeyVaultVars("savia-common", credential),
            ]
        case "devel":
            readers = [KeyVaultVars("savia-development", credential)]

    return ConfigService(app_name, *readers)


def get_savia_config_editor(key: Literal["prod", "pre", "devel", "common"]):
    var_editor = None
    credential = DefaultAzureCredential()
    match key:
        case "prod":
            var_editor = KeyVaultVars("savia-production", credential)
        case "pre":
            var_editor = KeyVaultVars("savia-preproduction", credential)
        case "common":
            var_editor = KeyVaultVars("savia-common", credential)
        case "devel":
            var_editor = KeyVaultVars("savia-development", credential)

    return ConfigEditor(var_editor)
