from .crud import (
    create_castle_settings,
    create_user_wallet_settings,
    get_castle_settings,
    get_user_wallet_settings,
    update_castle_settings,
    update_user_wallet_settings,
)
from .models import CastleSettings, UserWalletSettings


async def get_settings(user_id: str) -> CastleSettings:
    settings = await get_castle_settings(user_id)
    if not settings:
        settings = await create_castle_settings(user_id, CastleSettings())
    return settings


async def update_settings(user_id: str, data: CastleSettings) -> CastleSettings:
    settings = await get_castle_settings(user_id)
    if not settings:
        settings = await create_castle_settings(user_id, data)
    else:
        settings = await update_castle_settings(user_id, data)

    return settings


async def get_user_wallet(user_id: str) -> UserWalletSettings:
    settings = await get_user_wallet_settings(user_id)
    if not settings:
        settings = await create_user_wallet_settings(user_id, UserWalletSettings())
    return settings


async def update_user_wallet(
    user_id: str, data: UserWalletSettings
) -> UserWalletSettings:
    settings = await get_user_wallet_settings(user_id)
    if not settings:
        settings = await create_user_wallet_settings(user_id, data)
    else:
        settings = await update_user_wallet_settings(user_id, data)

    return settings
