from __future__ import annotations

from datetime import datetime, timezone


class SystemSettingsRepositoryMixin:
    """Original encrypted system-setting persistence methods."""

    def get_system_setting(self, key: str) -> str:
        with self.connect() as connection:
            row = connection.execute(
                "SELECT encrypted_payload FROM system_settings WHERE setting_key = ?",
                (key,),
            ).fetchone()
        return str(row["encrypted_payload"]) if row else ""

    def save_system_setting(self, key: str, encrypted_payload: str) -> None:
        now = datetime.now(timezone.utc).isoformat(timespec="seconds")
        with self.connect() as connection:
            connection.execute(
                """
                INSERT INTO system_settings (setting_key, encrypted_payload, updated_at)
                VALUES (?, ?, ?)
                ON CONFLICT(setting_key) DO UPDATE SET
                    encrypted_payload = excluded.encrypted_payload,
                    updated_at = excluded.updated_at
                """,
                (key, encrypted_payload, now),
            )
