
import os
from pathlib import Path
from datetime import timedelta

BASE_DIR = Path(__file__).resolve().parent.parent


SECRET_KEY = 'django-insecure-3%@o0k=b%^-wc59&zfy8jx9zphlof-rllc5*yowl2fsw54maz^'

DEBUG = True

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'corsheaders',
    'rest_framework_simplejwt.token_blacklist',
    'social_django',
    'django.contrib.sites',

    'api.apps.ApiConfig',
    'common',
    'employee',
    'expenses',
    'financial_year',
    'masters',
    'privilege',
    'reports',
    'store',
    'company',
    'user_auth',
    'dashboard',
    'supplier',
    'inventory',
    'stock',
    'sales',
    'customer',
    'barcodes',
    'material',
    'scheme',
    'voucher',
    'claim_collect_upgrade',
    'slab',
    'wholesale',
    'ledger',
    'core_ledger',
]

MIDDLEWARE = [
    'common.middleware.CustomErrorMiddleware',
    'common.middleware.MaintenanceModeMiddleware', 
    'common.middleware.ForceErrorMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'common.middleware.SessionTimeoutMiddleware',  # put AFTER auth middleware
    'financial_year.middleware.FinancialYearMiddleware',  # FY lock enforcement
    # 'common.middleware.WholesaleCustomerAccessMiddleware',  # Restrict wholesale customer access
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'boyspos_gst.urls'


TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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',
                'common.context_processors.transfer_pending_counts',
                'common.context_processors.balance_marquee_processor',
                'common.context_processors.wholesale_internal_count',
                'common.context_processors.sales_request_pending_counts',
                'common.context_processors.financial_year_context',
            ],
        },
    },
]

WSGI_APPLICATION = 'boyspos_gst.wsgi.application'

CORS_ALLOWED_ORIGINS = [
    'http://localhost:8000',  
    'http://127.0.0.1:8000',
    'http://192.168.1.41:8000',
]



REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework_simplejwt.authentication.JWTAuthentication',

    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
}





SIMPLE_JWT = {
    'ACCESS_TOKEN_LIFETIME': timedelta(days=7),
    'REFRESH_TOKEN_LIFETIME': timedelta(days=7),
    'BLACKLIST_AFTER_ROTATION': True,
    'UPDATE_LAST_LOGIN': False,
    'AUTH_TOKEN_CLASSES': ('rest_framework_simplejwt.tokens.AccessToken',),
    'TOKEN_BLACKLIST_CACHE_ENABLED': True,  # Enable caching for token blacklist checks
}


CSRF_COOKIE_SECURE = False  # Ensure this is False if you're testing on localhost
CSRF_TRUSTED_ORIGINS = [
    'http://localhost:8000',
    'http://127.0.0.1:8000',

]


# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

# DATABASES = {
    # 'default': {
        # 'ENGINE': 'django.db.backends.mysql',
        # 'NAME': 'boysmobiles',
        # 'USER':'root',
        # 'PASSWORD':'',
        # 'HOST':'127.0.0.1',
        # 'PORT':'3306',
        # 'OPTIONS':{
            # 'init_command':"SET sql_mode='STRICT_TRANS_TABLES'"
    # }
# }
# }

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'biglitz_gst',
        'USER':'biglitz_boys_mobiles',
        'PASSWORD':'Boys_Mobiles@321',
        'HOST':'103.118.17.85',
        'PORT':'3306',
        'OPTIONS':{
            'init_command':"SET sql_mode='STRICT_TRANS_TABLES'"
    }
}
}

# Password validation
# https://docs.djangoproject.com/en/4.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/4.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Kolkata'  

USE_I18N = True

USE_L10N = True

USE_TZ = True  

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/

# https://docs.djangoproject.com/en/4.2/howto/static-files/
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = "/media/"
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]


# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

SESSION_ENGINE = 'django.contrib.sessions.backends.db'  # or use cache-based sessions

SESSION_COOKIE_AGE = 604800

LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = '/dashboard/'
LOGOUT_REDIRECT_URL = '/login/'

SESSION_SAVE_EVERY_REQUEST = True

SESSION_EXPIRE_AT_BROWSER_CLOSE = True



MAINTENANCE_MODE = False  
LIVE_SERVER = False