"""
Django settings for fwd project.

Generated by 'django-admin startproject' using Django 4.1.3.

For more information on this file, see
https://docs.djangoproject.com/en/4.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""
import os
from pathlib import Path
import environ
from django.urls import reverse_lazy
from fwd_api.middleware.logging_request_response_middleware import TraceIDLogFilter

# Build paths inside the project like this: BASE_DIR / 'subdir'.

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

env = environ.Env(
    DEBUG=(bool, False)
)
DEBUG = False
environ.Env.read_env(os.path.join(BASE_DIR, '.env'))
ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", default=['*'] + ['107.120.{}.{}'.format(i, j) for i in range(256) for j in range(256)])
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env.str("SECRET_KEY", '')
CTEL_KEY = env.str("CTEL_KEY", '')
INTERNAL_SDS_KEY = env.str('SDS_SECRET_KEY', '')
DB_ENCRYPT_KEY = env.str('DB_INTERNAL_KEY', '')
BASE_URL = env.str("BASE_URL", "")
BASE_UI_URL = env.str("BASE_UI_URL", "")
AUTH_TOKEN_LIFE_TIME = env.int("AUTH_TOKEN_LIFE_TIME", 0)
IMAGE_TOKEN_LIFE_TIME = env.int("IMAGE_TOKEN_LIFE_TIME", 0)
ADMIN_USER_NAME = env.str("ADMIN_USER_NAME", "")
ADMIN_PASSWORD = env.str("ADMIN_PASSWORD", '')# SECURITY WARNING: don't run with debug turned on in production!
STANDARD_USER_NAME = env.str("STANDARD_USER_NAME", "")
STANDARD_PASSWORD = env.str("STANDARD_PASSWORD", '')# SECURITY WARNING: don't run with debug turned on in production!

# Application definition
S3_ENDPOINT = env.str("S3_ENDPOINT", "")
S3_ACCESS_KEY = env.str("S3_ACCESS_KEY", "")
S3_SECRET_KEY = env.str("S3_SECRET_KEY", "")
S3_BUCKET_NAME = env.str("S3_BUCKET_NAME", "ocr-data")
REDIS_HOST = env.str("REDIS_HOST", "result-cache")
REDIS_PORT = env.int("REDIS_PORT", 6379)


INSTALLED_APPS = [
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    'fwd_api.apps.FwdApiConfig',
    'django.contrib.admin',
    'rest_framework',
    'drf_spectacular',
    'drf_spectacular_sidecar',  # required for Django collectstatic discovery
    'corsheaders',

]

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",
    'corsheaders.middleware.CorsMiddleware',
    "whitenoise.middleware.WhiteNoiseMiddleware",
    "django.middleware.locale.LocaleMiddleware",
    "fwd_api.middleware.logging_request_response_middleware.LoggingMiddleware"
]

LOCALE_PATHS = [
    os.path.join(BASE_DIR, 'locale')
]

ROOT_URLCONF = "fwd.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",
            ],
        },
    },
]

WSGI_APPLICATION = "fwd.wsgi.application"

# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': env.str("DB_ENGINE"),
        'NAME': env.str("DB_SCHEMA"),
        'USER': env.str("DB_USER", None),
        'PASSWORD': env.str("DB_PASSWORD", None),
        'HOST': env.str("DB_HOST", None),
        'PORT': env.str("DB_PORT", None),
        'CONN_MAX_AGE': 0,
    }
}

# Password validation
# https://docs.djangoproject.com/en/4.1/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.1/topics/i18n/

LANGUAGE_CODE = "en-us"
USE_I18N = True

CELERY_ENABLE_UTC = False
CELERY_TIMEZONE = "Asia/Singapore"
TIME_ZONE = "Asia/Singapore"
USE_TZ = True

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

STATIC_URL = "static/"
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STORAGES = {
    # ...
    "staticfiles": {
        "BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
    },
}
# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

REST_FRAMEWORK = {
    # YOUR SETTINGS
    'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
    # 'DEFAULT_SCHEMA_CLASS': 'fwd_api.schema.AutoSchema',
    "DEFAULT_AUTHENTICATION_CLASSES": (
        "fwd_api.filter.AuthFilter.AuthFilter",
    ),
    'EXCEPTION_HANDLER': 'fwd_api.exception.exceptions_handler.custom_exception_handler',
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
        'rest_framework_xml.renderers.XMLRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ),
}

SPECTACULAR_SETTINGS = {
    'TITLE': 'OCR Engine for Invoices',
    'DESCRIPTION': 'AI Powered by Samsung SDS Vietnam',
    'VERSION': '1.0.0',
    'SERVE_INCLUDE_SCHEMA': True,
    # OTHER SETTINGS
    'SWAGGER_UI_DIST': 'SIDECAR',  # shorthand to use the sidecar instead
    'SWAGGER_UI_FAVICON_HREF': 'SIDECAR',
    'REDOC_DIST': 'SIDECAR',
    # OTHER SETTINGS
    "PREPROCESSING_HOOKS": ["fwd_api.api_specs.hooks.remove_apis_from_list"],
    # Custom Spectacular Settings
    "EXCLUDE_PATH": [reverse_lazy("schema")],
    "EXCLUDE_RELATIVE_PATH": ["/rsa", '/gen-token', '/app/'],
    "TAGS_SORTER": "alpha"
}

FILE_UPLOAD_HANDLERS = [
    'django.core.files.uploadhandler.TemporaryFileUploadHandler',
]
CORS_ORIGIN_ALLOW_ALL = True

MEDIA_ROOT = env.str("MEDIA_ROOT", default=r"/var/www/example.com/media/")
LOG_ROOT = env.str("LOG_ROOT", default="/app/log")
BROKER_URL = env.str("BROKER_URL", default="amqp://test:test@107.120.70.226:5672//")
CELERY_TASK_TRACK_STARTED = True
# CELERY_TASK_TIME_LIMIT = 30 * 60
CELERY_TASK_TIME_LIMIT = None

MAX_UPLOAD_SIZE_OF_A_FILE = 5 * 1024 * 1024  # 5 MB
MAX_UPLOAD_FILE_SIZE_OF_A_REQUEST = 100 * 1024 * 1024  # 100 MB
MAX_UPLOAD_FILES_IN_A_REQUEST = 2
MAX_PIXEL_IN_A_FILE = 5000
TARGET_MAX_IMAGE_SIZE = (2048, 2048)
SIZE_TO_COMPRESS = 2 * 1024 * 1024
MAX_NUMBER_OF_TEMPLATE = 3
MAX_PAGES_OF_PDF_FILE = 50

OVERVIEW_REFRESH_INTERVAL = 2
OVERVIEW_REPORT_ROOT = "overview"
OVERVIEW_REPORT_DURATION = ["30d", "7d"]

ACC_EXCLUDE_RESEASONS = ["Invalid Input", "Handwritten information", "handwritten", "invalid_image", "missing_information", "too_blurry_text", "too_small_text"]

SUBS = {
    "SEAU": "AU",
    "SESP": "SG",
    "SME": "MY",
    "SEPCO": "PH",
    "TSE": "TH",
    "SEIN": "ID",
    "ALL": "all", # all_detail
    "SEAO": "seao"
    }

FIELDS_BY_SUB = {
    "SG": {"imei": ["imei_number"],
            "invoice": ["retailername", "purchase_date"]},
    "default": {"imei": ["imei_number"],
            "invoice": ["retailername", "invoice_no", "purchase_date"]},
    }

BAD_THRESHOLD = 0.75
NEED_REVIEW = 1.0

DOC_TYPES = ["imei", "invoice"]
SUB_FOR_BILLING = ["all", "seao"]
FIELD = ["imei_number", "purchase_date", "retailername", "sold_to_party", "invoice_no"]

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
    }
}

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'trace_id': {
            '()': TraceIDLogFilter,
        },
    },
    'formatters': {
        'verbose': {
            'format': f'{{asctime}} - {{levelname}} - Trace_ID: {{trace_id}} - {{module}} - {{message}}',
            'style': '{',
        },
        'simple': {
            'format': f'{{asctime}} - {{levelname}} - Trace_ID: {{trace_id}} - {{message}}',
            'style': '{',
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
            'filters': ['trace_id'],
        },
        'file': {
            "class": 'logging.handlers.TimedRotatingFileHandler',
            'filename': f'{LOG_ROOT}/sbt_idp_BE.log',  # Specify the path and filename for the log file
            "when": "midnight",
            "interval": 1,
            'backupCount': 10,
            'formatter': 'verbose',
            'filters': ['trace_id'],
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console', 'file'],
            'level': 'INFO',
        },
        '': {
            'handlers': ['console', 'file'],
            'level': 'INFO',
        }
    },
}

REASON_SOLUTION_MAP = {"Invalid image": "Remove this image from the evaluation report",
                       "Missing information": "Remove this image from the evaluation report",
                       "Too blurry text": "Remove this image from the evaluation report",
                       "Too small text": "Remove this image from the evaluation report",
                       "Handwritten": "Remove this image from the evaluation report",
                       "Wrong feedback": "Update revised resutl and re-calculate accuracy",
                       "Ocr cannot extract": "Improve OCR",
                       }

IMEI_MAX_LENGHT = 14