import os
from urllib.parse import quote_plus
from dotenv import load_dotenv

load_dotenv()

class Config:
    SECRET_KEY = os.environ.get("SECRET_KEY", "dev-secret-key")
    SECOND_OPENAI_API_KEY = os.environ.get("SECOND_OPENAI_API_KEY")
    
    user = os.environ.get("DATABASE_USER", "license_user")
    password = quote_plus(os.environ.get("DATABASE_PASSWORD", "supersecretpassword"))
    host = os.environ.get("DATABASE_HOST", "127.0.0.1")
    port = os.environ.get("DATABASE_PORT", "5432")
    db = os.environ.get("DATABASE_NAME", "license_db")

    # SQLALCHEMY_DATABASE_URI = f"mysql+pymysql://{user}:{password}@{host}:{port}/{db}?charset=utf8mb4"
    SQLALCHEMY_DATABASE_URI = (
        f"postgresql+psycopg2://{user}:{quote_plus(password)}"
        f"@{host}:{port}/{db}"
    )
    SQLALCHEMY_TRACK_MODIFICATIONS = False

    
    MAIL_SERVER = os.environ.get("MAIL_SERVER", "mail.mykaman.ir")
    MAIL_PORT = int(os.environ.get("MAIL_PORT", 465))
    MAIL_USE_SSL = os.environ.get("MAIL_USE_SSL", "True") == "True"
    MAIL_USE_TLS = os.environ.get("MAIL_USE_TLS", "False") == "True"
    MAIL_USERNAME = os.environ.get("MAIL_USERNAME", "no-reply@vipatech.mykaman.ir")
    MAIL_PASSWORD = os.environ.get("MAIL_PASSWORD", "Milad1375422")
    MAIL_DEFAULT_SENDER = (
        os.environ.get("MAIL_DEFAULT_SENDER_NAME", "دستیار هوشمند تکسورا"),
        os.environ.get("MAIL_DEFAULT_SENDER", "no-reply@vipatech.mykaman.ir")
    )


def generator_openai_models():
    from models.ai_model import ModelType #prevent circular import

    OPENAI_MODELS = {

        # =====================
        # GPT-4 / GPT-4o
        # =====================
        "gpt-4o": {
            "input_cost_per_1m": 5.00,
            "output_cost_per_1m": 15.00,
            "model_type": ModelType.TEXT_STREAM,
            "supports_temperature": True,
            "supports_web_search": True,
        },

        "gpt-4o-mini": {
            "input_cost_per_1m": 1.1,
            "output_cost_per_1m": 4.4,
            "model_type": ModelType.TEXT_STREAM,
            "supports_temperature": True,
            "supports_web_search": True,
        },

        "gpt-4o-transcribe": {
            "input_cost_per_1m": 0.008,
            "output_cost_per_1m": 0.00,
            "model_type": ModelType.VOICE_TO_TEXT,
            "supports_temperature": False,
            "supports_web_search": False,
        },

        "gpt-4o-mini-tts": {
            "input_cost_per_1m": 2.5,
            "output_cost_per_1m": 10.00,
            "model_type": ModelType.TEXT_TO_VOICE,
            "supports_temperature": False,
            "supports_web_search": False,
        },

        # =====================
        # GPT-4.1
        # =====================
        "gpt-4.1": {
            "input_cost_per_1m": 2.00,
            "output_cost_per_1m": 8.00,
            "model_type": ModelType.TEXT_STREAM,
            "supports_temperature": True,
            "supports_web_search": True,
        },

        "gpt-4.1-mini": {
            "input_cost_per_1m": 0.4,
            "output_cost_per_1m": 1.6,
            "model_type": ModelType.TEXT_STREAM,
            "supports_temperature": True,
            "supports_web_search": True,
        },

        # =====================
        # Embeddings
        # =====================
        "text-embedding-ada-002": {
            "input_cost_per_1m": 0.1,
            "output_cost_per_1m": 0.00,
            "model_type": ModelType.TEXT_EMBED,
            "supports_temperature": False,
            "supports_web_search": False,
        },

        "text-embedding-3-small": {
            "input_cost_per_1m": 0.02,
            "output_cost_per_1m": 0.00,
            "model_type": ModelType.TEXT_EMBED,
            "supports_temperature": False,
            "supports_web_search": False,
        },

        "text-embedding-3-large": {
            "input_cost_per_1m": 0.13,
            "output_cost_per_1m": 0.00,
            "model_type": ModelType.TEXT_EMBED,
            "supports_temperature": False,
            "supports_web_search": False,
        },

        # =====================
        # Audio
        # =====================
        "whisper-1": {
            "input_cost_per_1m": 0.006,
            "output_cost_per_1m": 0.00,
            "model_type": ModelType.VOICE_TO_TEXT,
            "supports_temperature": False,
            "supports_web_search": False,
        },

        "tts-1": {
            "input_cost_per_1m": 15,
            "output_cost_per_1m": 0.00,
            "model_type": ModelType.TEXT_TO_VOICE,
            "supports_temperature": False,
            "supports_web_search": False,
        },

        "tts-1-hd": {
            "input_cost_per_1m": 30,
            "output_cost_per_1m": 0.00,
            "model_type": ModelType.TEXT_TO_VOICE,
            "supports_temperature": False,
            "supports_web_search": False,
        },

        # =====================
        # GPT-5
        # =====================
        "gpt-5": {
            "input_cost_per_1m": 1.25,
            "output_cost_per_1m": 10.00,
            "model_type": ModelType.TEXT,
            "supports_temperature": False,
            "supports_web_search": True,
        },

        "gpt-5.1": {
            "input_cost_per_1m": 1.25,
            "output_cost_per_1m": 10.00,
            "model_type": ModelType.TEXT,
            "supports_temperature": False,
            "supports_web_search": True,
        },

        "gpt-5-mini": {
            "input_cost_per_1m": 0.25,
            "output_cost_per_1m": 2.00,
            "model_type": ModelType.TEXT,
            "supports_temperature": False,
            "supports_web_search": True,
        },

        "gpt-5.4-mini": {
            "input_cost_per_1m": 0.75,
            "output_cost_per_1m": 4.5,
            "model_type": ModelType.TEXT,
            "supports_temperature": False,
            "supports_web_search": True,
        },
    }
    return OPENAI_MODELS


AVERAGE_INPUT_COST_PER_1M = 1.00
AVERAGE_OUTPUT_COST_PER_1M = 3.00

UPLOAD_FILES = 'upload_files'


DEFULT_TEXT_MODEL_NAME = "gpt-5.1"
DEFULT_TEXT_STREAM_MODEL_NAME = "gpt-4o"
DEFULT_VOICE_2_TEXT_MODEL_NAME = "gpt-4o-transcribe"
DEFULT_TEXT_2_VOICE_MODEL_NAME = "gpt-4o-mini-tts"