# -*- coding: utf-8 -*-
from __future__ import annotations

import json
import os

from keep_it_secret import EnvField, LiteralField, SecretsField

from hotpocket_backend.secrets.base import (
    CelerySecrets,
    DatabaseSecrets,
    OIDCSecrets,
)
from hotpocket_backend.secrets.webapp import WebAppSecrets


class DeploymentDatabaseSecrets(DatabaseSecrets):
    payload: str = LiteralField.new(
        json.dumps({
            'engine': os.getenv('HOTPOCKET_BACKEND_DATABASE_ENGINE', 'django.db.backends.sqlite3'),
            'name': os.getenv('HOTPOCKET_BACKEND_DATABASE_NAME', '/srv/run/hotpocket-backend-aio.sqlite'),
            'user': os.getenv('HOTPOCKET_BACKEND_DATABASE_USER', ''),
            'password': os.getenv('HOTPOCKET_BACKEND_DATABASE_PASSWORD', ''),
            'host': os.getenv('HOTPOCKET_BACKEND_DATABASE_HOST', ''),
            'port': os.getenv('HOTPOCKET_BACKEND_DATABASE_PORT', ''),
        }),
    )


class DeploymentOIDCSecrets(OIDCSecrets):
    payload: str | None = EnvField.new(
        'HOTPOCKET_BACKEND_OIDC_PAYLOAD', default=None, required=False,
    )


class DeploymentCelerySecrets(CelerySecrets):
    broker_url: str = EnvField.new(
        'HOTPOCKET_BACKEND_CELERY_BROKER_URL',
        default='amqp://guest@rabbitmq.aio.hotpocket/',
        required=False,
    )
    result_backend: str = EnvField.new(
        'HOTPOCKET_BACKEND_CELERY_RESULT_BACKEND',
        default='disabled://',
        required=False,
    )


class AIOWebAppSecrets(WebAppSecrets):
    SECRET_KEY: str = EnvField.new('HOTPOCKET_BACKEND_SECRET_KEY')

    DATABASE: DeploymentDatabaseSecrets = SecretsField.new(DeploymentDatabaseSecrets)
    CELERY: DeploymentCelerySecrets = SecretsField.new(DeploymentCelerySecrets)

    OIDC: DeploymentOIDCSecrets = SecretsField.new(DeploymentOIDCSecrets)


__secrets__ = AIOWebAppSecrets()
