"""Django settings for aiservice project.

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

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

For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.0/ref/settings/
"""

from __future__ import annotations

import os
from pathlib import Path

import dj_database_url
import dotenv
import sentry_sdk

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


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
# SECURITY WARNING: don't run with debug turned on in production!
if os.environ.get("ENVIRONMENT") == "PRODUCTION":
    SECRET_KEY: str | None = os.environ.get("SECRET_KEY")
    os.environ["DATABASE_URL"] = os.environ.get("POSTGRESQLCONNSTR_DATABASE_URL")
    DEBUG: bool = False
else:
    env_vars: dict[str, str | None] = dotenv.dotenv_values()
    SECRET_KEY = env_vars.get("SECRET_KEY", os.environ.get("SECRET_KEY"))
    DEBUG = True

assert "DATABASE_URL" in os.environ, "DATABASE_URL environment variable not set"
ALLOWED_HOSTS: list[str] = [
    "localhost",
    "codeflash.ai",
    "app.codeflash.ai",
    "codeflash-aiservice.azurewebsites.net",
    "codeflash-aiservice-dev.azurewebsites.net",
]


# Application definition

INSTALLED_APPS: list[str] = [
    "authapp.apps.AuthAppConfig",
    "testgen.apps.TestgenConfig",
    "optimizer.apps.OptimizerConfig",
    "django.contrib.contenttypes",
    "django.contrib.staticfiles",
    "log_features.apps.LoggingConfig",
    "aiservice.apps.AiServiceConfig",  # Add the app containing the management commands
]

MIDDLEWARE: list[str] = [
    "django.middleware.security.SecurityMiddleware",
    "django.middleware.common.CommonMiddleware",
    "aiservice.middleware.healthcheck.HealthCheckMiddleware",
]

ROOT_URLCONF: str = "aiservice.urls"

TEMPLATES: list[str] = []

WSGI_APPLICATION: str = "aiservice.wsgi.application"


# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
# Requires DATABASE_URL environment variable to be set

assert "DATABASE_URL" in os.environ, "DATABASE_URL environment variable not set"
DATABASES = {"default": dj_database_url.config(conn_max_age=600, conn_health_checks=True)}


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

AUTH_PASSWORD_VALIDATORS: list[dict[str, str]] = [
    {"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: str = "en-us"

TIME_ZONE: str = "UTC"

USE_I18N: bool = True

USE_TZ: bool = True


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

STATIC_URL: str = "static/"

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

DEFAULT_AUTO_FIELD: str = "django.db.models.BigAutoField"

# Sentry
if os.environ.get("ENVIRONMENT", default="") == "PRODUCTION":
    sentry_sdk.init(
        dsn="https://8a857cbf974ca889a46c1b39173db44b@o4506833230561280.ingest.sentry.io/4506833234493440",
        # Set traces_sample_rate to 1.0 to capture 100%
        # of transactions for performance monitoring.
        traces_sample_rate=1.0,
        # Set profiles_sample_rate to 1.0 to profile 100%
        # of sampled transactions.
        # We recommend adjusting this value in production.
        profiles_sample_rate=1.0,
    )
