import os
import uuid
from datetime import datetime
from typing import Any

from evibes.settings.base import LANGUAGE_CODE, LANGUAGES


def get_language_from_header(accept_language: str | None = None) -> str:
    language_codes = {lang.split("-")[0]: lang for lang, _ in LANGUAGES}
    languages_dict = dict(LANGUAGES)

    if not accept_language:
        return LANGUAGE_CODE.lower()

    for lang in accept_language.split(","):
        lang_code_parts = lang.split(";")[0].strip().lower()

        if "-" in lang_code_parts:
            primary, country = lang_code_parts.split("-")
            lang_code_parts = f"{primary.lower()}-{country.upper()}"

        if lang_code_parts in languages_dict:
            return lang_code_parts

        primary_lang = lang_code_parts.split("-")[0]
        if primary_lang in language_codes:
            return language_codes[primary_lang].lower()

    return LANGUAGE_CODE.lower()


def evibes_summernote_upload_to_func(instance: Any, filename: str) -> str:
    ext = filename.split(".")[-1]
    filename = f"{uuid.uuid4()}.{ext}"
    today = datetime.now().strftime("%Y-%m-%d")
    if instance:
        return os.path.join("evibes-summernote", today, filename)
    return os.path.join("evibes-summernote", today)
