from typing import Dict, Union
from sqlalchemy.orm import Session
from src.apps.settings.helper import get_setting_record
from src.core.config import settings
from src.middleware.errors import APIException
from starlette import status
# from src.database.models.subscriptions import Subscription
from src.apps.site_settings.models.site_settings import SiteSettings


def get_brand_primary_color(
    merchant_id: int,
) -> str:
    """
    Fetch the primary color of merchant
    """
    setting = get_setting_record(
        field_name="branding_colors_primary",
        key="branding",
        merchant_id=merchant_id,
    )
    return (
        setting.value
        if setting and setting.value and len(setting.value) > 0
        else settings.APP_PRIMARY_COLOR
    )


def get_brand_secondary_color(
    merchant_id: int,
) -> str:
    """
    Fetch the secondary color of merchant
    """
    setting = get_setting_record(
        field_name="branding_colors_secondary",
        key="branding",
        merchant_id=merchant_id,
    )
    return (
        setting.value
        if setting and setting.value and len(setting.value) > 0
        else settings.APP_SECONDARY_COLOR
    )


def get_timezone(
    merchant_id: int,
) -> str:
    """
    Fetch the timezone of merchant
    """
    setting = get_setting_record(
        field_name="info_timezone",
        key="info",
        merchant_id=merchant_id,
    )
    return (
        setting.value
        if setting and setting.value and len(setting.value) > 0
        else settings.APP_TIMEZONE
    )


def get_whitelabel(merchant_id: str):
    return {
        "privacy_policy_url": settings.APP_PRIVACY_POLICY_URL,
        "tnc_url": settings.APP_TNC_URL,
        "support_phone": settings.APP_SUPPORT_PHONE,
        "support_email": settings.APP_SUPPORT_EMAIL,
    }

def get_value_by_key_from_saas_settings(data, target_key):
    # for item in data.get("saas_settings", []):
    for item in data:
        if item.key == target_key:
            return item.value
    return None

def cancel_subscription_guard( db: Session, merchant_id: int):
    merchant_subscription = (
        db.query(Subscription)
        .filter(
            Subscription.merchant_id == merchant_id,
            Subscription.deleted_at.is_(None),
        )
        .first()
    )
    
    if merchant_subscription:
        # Check if the subscription has been canceled
        if merchant_subscription.cancelled_at is not None:            
            raise APIException(
                module=__name__,
                error={},
                status_code=status.HTTP_400_BAD_REQUEST,
                message="Your subscription is canceled",
            )
        # Check if the subscription has expired
        suspend_account_when_subscription_fails_value = (
            db.query(SiteSettings.value)
            .filter(
                SiteSettings.key == "suspend_account_when_subscription_fails",
                SiteSettings.is_visible == True,
            )
            .first()
        )
        if (
            suspend_account_when_subscription_fails_value
            and suspend_account_when_subscription_fails_value.value == "true"
        ):
            if merchant_subscription.status == "expired":            
                raise APIException(
                    module=__name__,
                    error={},
                    status_code=status.HTTP_400_BAD_REQUEST,
                    message="Your subscription has failed",
                )