import os
from pathlib import Path

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


# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-4ju2n@$f9d0c=h)_g0lbb%k9&@rf(xa$d$g$&5ri$uf)*gev^4'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = [
    'hyperkenya.posapp.co.ke',  # Your domain
]

# Logging settings for production
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '{levelname} {asctime} {module} {message}',
            'style': '{',
        },
    },
    'handlers': {
        'file': {
            'level': 'ERROR',
            'class': 'logging.FileHandler',
            'filename': os.path.join(BASE_DIR, 'logs/django.log'),
            'formatter': 'verbose',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'ERROR',
            'propagate': True,
        },
    },
}


# Application definition
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'inventory',
    'sales',
    'customers',
    'reports',
    'multi_shop',
    'dashboard',
    'suppliers',
    'payments',
    'authapp',
    'orders',
    'settings',
    'hr',
    'expenses',
    'shop',
    'customer_orders',
]

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',
]


ROOT_URLCONF = 'django_project.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                # Add your custom processors here
                'settings.context_processors.theme_context',  # Assuming 'settings' is your app name
                'settings.context_processors.shop_details',   # Assuming 'settings' is your app name
            ],
        },
    },
]

WSGI_APPLICATION = 'django_project.wsgi.application'


# Database configuration for PostgreSQL
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'hyperkenya',
        'USER': 'hyperkenya',
        'PASSWORD': 'Innov@t254',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

# Password validation
# https://docs.djangoproject.com/en/5.0/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.0/topics/i18n/
LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


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

STATIC_URL = 'static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
    ]
STATIC_ROOT = BASE_DIR / "staticfiles"


# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

LOGIN_URL = 'auth/login/'  # This assumes you have set a URL pattern for login
LOGIN_REDIRECT_URL = 'dashboard'  # Redirect after successful login
LOGOUT_REDIRECT_URL = 'login'  # Redirect after logout

# Set time zone to Nairobi local time
TIME_ZONE = 'Africa/Nairobi'

USE_TZ = True  # Keep this as True to use time zone support

CSRF_TRUSTED_ORIGINS = ['http:hyperkenya.posapp.co.ke']
CSRF_COOKIE_SECURE = False

# Session settings for automatic logout after 30 minutes of inactivity
SESSION_COOKIE_AGE = 30 * 60  # 30 minutes in seconds
SESSION_EXPIRE_AT_BROWSER_CLOSE = True  # Session expires when the browser is closed
SESSION_SAVE_EVERY_REQUEST = True  # Refresh session timeout with every request

# Settings for media files
MEDIA_URL = '/media/'  # This is the URL that will serve media files
MEDIA_ROOT = BASE_DIR / 'media'  # This is where media files will be stored

