import os
from pathlib import Path
from decouple import config
from datetime import timedelta

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


SECRET_KEY ='django-insecure-xfer_q9mb%-b0iaqgj@+v#5)rl^1+fdr@+@8+b!o7-rm3kuz0n'
DEBUG = True

ALLOWED_HOSTS = ["api.wfrexan.com"]
AUTH_USER_MODEL = 'users.User'


INTERNAL_IPS = [
    '127.0.0.1',
    'localhost',
]
CORS_ALLOWED_ORIGINS = [
    "https://wfrexan.com",
    "https://www.wfrexan.com", 
    "http://wfrexan.com",
    "http://www.wfrexan.com",
]

CORS_ALLOW_CREDENTIALS = True

# Add these for better CORS handling
CORS_EXPOSE_HEADERS = ['Content-Type', 'X-CSRFToken']

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    "whitenoise.runserver_nostatic",
    "corsheaders",
    'drf_spectacular',
    'rest_framework',
    'rest_framework_simplejwt',
    'django_filters',
    "debug_toolbar",
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google',
    'social_django',
    
    'apps.users',
    'apps.core',
    'apps.reviews',
    'apps.level',
    'apps.package',
    'apps.bonus',
    'apps.blance_transfer',
    'apps.withdraw',
    'apps.payment',
    'apps.downline',
    'apps.leaderboard',
    'apps.deposite',
    'apps.profiles',
    'apps.daily_cheack',
    'apps.bnb',
]
SITE_ID = 1

MIDDLEWARE = [
    "corsheaders.middleware.CorsMiddleware",
    'django.middleware.security.SecurityMiddleware',
    "whitenoise.middleware.WhiteNoiseMiddleware",
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    "corsheaders.middleware.CorsMiddleware",
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    "debug_toolbar.middleware.DebugToolbarMiddleware",
    'allauth.account.middleware.AccountMiddleware',
    
]

# google auth settings
SOCIALACCOUNT_PROVIDERS = {
    'google': {
        'SCOPE': [
            'profile',
            'email',
        ],
        'AUTH_PARAMS': {
            'access_type': 'online',
        }
    }
}
SOCIALACCOUNT_EMAIL_VERIFICATION = 'none'
SOCIALACCOUNT_EMAIL_REQUIRED = True
SOCIALACCOUNT_AUTO_SIGNUP = True

ROOT_URLCONF = 'config.urls'


TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'config.wsgi.application'
# WSGI_APPLICATION = 'config.wsgi.app'

STATIC_ROOT = BASE_DIR / "staticfiles"
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')


# DATABASES = {
#     'default': {
#         'ENGINE': 'django.db.backends.sqlite3',
#         'NAME': BASE_DIR / 'db.sqlite3',
#     }
# }


DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.mysql",
        "NAME": "mfaucetc_wfrexan_web", 
        "USER": "mfaucetc_mfaucetc_wfrexan_user",
        "PASSWORD": "iGUXQpFfVXBbyWRo122,",
        "HOST": "localhost",
        "PORT": "3306",
   
    }
}

# # Password validation
# https://docs.djangoproject.com/en/5.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/5.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Dhaka'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.2/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

SIMPLE_JWT = {
    "TOKEN_OBTAIN_SERIALIZER": "apps.users.serializers.login.EmailTokenObtainPairSerializer",
    "AUTH_HEADER_TYPES": ("Bearer", "JWT"),
    "ACCESS_TOKEN_LIFETIME": timedelta(days=10),
    "REFRESH_TOKEN_LIFETIME": timedelta(days=7),
    "ROTATE_REFRESH_TOKENS": True,
    "BLACKLIST_AFTER_ROTATION": True,
}



AUTHENTICATION_BACKENDS = [
    'apps.users.backends.EmailBackend',  
    'django.contrib.auth.backends.ModelBackend',       
]


REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ),
    'DEFAULT_FILTER_BACKENDS': [
        'django_filters.rest_framework.DjangoFilterBackend'
    ],
    'DEFAULT_THROTTLE_CLASSES': [
        'rest_framework.throttling.AnonRateThrottle',
        'rest_framework.throttling.UserRateThrottle'
    ],
    'DEFAULT_THROTTLE_RATES': {
        'anon': '100/day', 
        'user': '5000/day',
        'custom_scope_one': '50/minute', 
        'custom_scope_two': '1000/hour',
        'password_reset_request': '5/hour',  
        'password_reset_confirm': '5/hour',
    },
    'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',  
}


SPECTACULAR_SETTINGS = {
    'TITLE': 'Your API',
    'DESCRIPTION': 'Your project API documentation',
    'VERSION': '1.0.0',
    'SERVE_INCLUDE_SCHEMA': False,
    'SWAGGER_UI_SETTINGS': {
        'deepLinking': True,
        'persistAuthorization': True,
        'displayOperationId': True,
    },
    'COMPONENT_SPLIT_REQUEST': True,
    'DEFAULT_GENERATOR_CLASS': 'drf_spectacular.generators.SchemaGenerator',
}




# Frontend URL
FRONTEND_URL='wfrexan.com'
BDT_TO_USD_RATE = 125.00

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'mail.wfrexan.com'
EMAIL_PORT = 465
EMAIL_USE_SSL = True
EMAIL_USE_TLS = False
EMAIL_HOST_USER = 'otp@wfrexan.com'
EMAIL_HOST_PASSWORD = 'iGUXQpFfVXBbyWRo122,'
DEFAULT_FROM_EMAIL = 'otp@wfrexan.com/'

GOOGLE_CLIENT_ID = config('GOOGLE_CLIENT_ID', default='')
GOOGLE_CLIENT_SECRET = config('GOOGLE_CLIENT_SECRET', default='')



BINANCE_API_KEY = config('BINANCE_API_KEY')  # No default - must be in .env
BINANCE_SECRET_KEY = config('BINANCE_SECRET_KEY')  # No default - must be in .env
BINANCE_BASE_URL = config('BINANCE_BASE_URL', default='https://api.binance.com')

# Your USDT Wallet Address
USDT_WALLET_ADDRESS = config('USDT_WALLET_ADDRESS')  # No default - must be in .env
USDT_WALLET_NETWORK = config('USDT_WALLET_NETWORK', default='BEP20')


