from pathlib import Path
import os

# ================= BASE =================
BASE_DIR = Path(__file__).resolve().parent.parent


# ================= SECURITY =================
SECRET_KEY = 'django-insecure-os^!w8vxeb032r^r7*pyqzwsilfcnl^&=0z-by+y&3o%-u6+q1'

DEBUG = True


ALLOWED_HOSTS = [
    "starteamgroup.co.ke",
    "www.starteamgroup.co.ke"
]
# ================= APPLICATIONS =================
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',

    # Local apps
    'accounts',
    'properties',
    'tenants',
    'payments.apps.PaymentsConfig',
    'maintenance',
    'reports',
    'core',
]


# ================= MIDDLEWARE =================
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]


# ================= URL CONFIG =================
ROOT_URLCONF = 'config.urls'


# ================= TEMPLATES =================
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',

        # 🔥 CRITICAL FIX (YOUR SYSTEM NEEDS THIS)
        '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',
            ],
        },
    },
]

#===============REDIS CONFIG =======================
CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
    }
}

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


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


# ================= AUTH PASSWORD VALIDATION =================
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',
    },
]


# ================= AUTH SYSTEM =================
AUTH_USER_MODEL = 'accounts.User'

LOGIN_URL = "/accounts/login/"
LOGIN_REDIRECT_URL = "/"
LOGOUT_REDIRECT_URL = "/accounts/login/"


# ================= INTERNATIONALIZATION =================
LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Africa/Nairobi'

USE_I18N = True
USE_TZ = True


# ================= STATIC FILES =================
STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),  # your CSS/JS
]

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')  # for collectstatic


# ================= MEDIA FILES =================
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# =================MPESA CREDENTIALS ==============
MPESA_ENV = "sandbox"  # change to "production" later

MPESA_CONSUMER_KEY = "YOUR_KEY"
MPESA_CONSUMER_SECRET = "YOUR_SECRET"

MPESA_SHORTCODE = "174379"  # sandbox default
MPESA_PASSKEY = "YOUR_PASSKEY"

HOSTINGER_SMS_API_KEY = "your_api_key_here"

MPESA_CALLBACK_URL = "https://yourdomain.com/payments/mpesa-callback/" 

# ================= DEFAULT PRIMARY KEY =================
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

CSRF_TRUSTED_ORIGINS = [
    "https://starteamgroup.co.ke",
    "https://www.starteamgroup.co.ke"
]