import os
from pathlib import Path
from dotenv import load_dotenv

# ===============================
# BASE DIRECTORY
# ===============================
BASE_DIR = Path(__file__).resolve().parent.parent.parent

# ===============================
# LOAD ENV
# ===============================
ENV_PATH = BASE_DIR / ".env"
load_dotenv(dotenv_path=ENV_PATH)

# ===============================
# UPLOAD CONFIG
# ===============================
UPLOAD_DIR = BASE_DIR / "uploads"
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)

ALLOWED_EXTENSIONS = {".mp3", ".mp4", ".wav", ".m4a", ".ogg", ".webm", ".flac"}
MAX_FILE_SIZE_MB = int(os.getenv("MAX_FILE_SIZE_MB", 100))
MAX_FILE_SIZE_BYTES = MAX_FILE_SIZE_MB * 1024 * 1024


def is_allowed_file(filename: str) -> bool:
    return Path(filename).suffix.lower() in ALLOWED_EXTENSIONS


# ===============================
# WHISPER CONFIG
# ===============================
AVAILABLE_MODELS = ["tiny", "base", "small", "medium", "large"]

WHISPER_DEFAULT_MODEL = os.getenv("WHISPER_MODEL", "base")
WHISPER_LANGUAGE = os.getenv("WHISPER_LANGUAGE", "id")

if WHISPER_DEFAULT_MODEL not in AVAILABLE_MODELS:
    raise ValueError(f"WHISPER_MODEL '{WHISPER_DEFAULT_MODEL}' tidak valid")


# ===============================
# OPENAI / KOBOILLM CONFIG
# ===============================
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "").strip()
OPENAI_TRANSCRIBE_MODEL = "openai/whisper-1"  # format koboillm

if os.getenv("TRANSCRIBE_ENGINE", "local") == "api" and not OPENAI_API_KEY:
    raise ValueError("OPENAI_API_KEY belum diset untuk mode API")


# ===============================
# ENGINE OPTIONS
# ===============================
ENGINE_LOCAL = "local"
ENGINE_API = "api"
TRANSCRIBE_ENGINE = os.getenv("TRANSCRIBE_ENGINE", ENGINE_LOCAL)


# ===============================
# MIDTRANS CONFIG
# ===============================
MIDTRANS_SERVER_KEY = os.getenv("MIDTRANS_SERVER_KEY", "").strip()
MIDTRANS_CLIENT_KEY = os.getenv("MIDTRANS_CLIENT_KEY", "").strip()
MIDTRANS_IS_PRODUCTION = os.getenv("MIDTRANS_IS_PRODUCTION", "false").lower() == "true"

if not MIDTRANS_SERVER_KEY:
    raise RuntimeError("MIDTRANS_SERVER_KEY belum diset di .env")

if not MIDTRANS_CLIENT_KEY:
    raise RuntimeError("MIDTRANS_CLIENT_KEY belum diset di .env")


# ===============================
# SECURITY
# ===============================
SECRET_KEY = os.getenv("SECRET_KEY", "").strip()
ENCRYPTION_KEY = os.getenv("ENCRYPTION_KEY", "").strip()

if not SECRET_KEY:
    raise RuntimeError("SECRET_KEY belum diset")

if len(SECRET_KEY) < 32:
    raise ValueError("SECRET_KEY minimal 32 karakter")


# ===============================
# SIGNUP CONFIG
# ===============================
ALLOW_PUBLIC_SIGNUP: bool = os.getenv("ALLOW_PUBLIC_SIGNUP", "true").lower() == "true"


# ===============================
# GOOGLE OAUTH2
# ===============================
GOOGLE_CLIENT_ID     = os.getenv("GOOGLE_CLIENT_ID", "")
GOOGLE_CLIENT_SECRET = os.getenv("GOOGLE_CLIENT_SECRET", "")
GOOGLE_REDIRECT_URI  = os.getenv(
    "GOOGLE_REDIRECT_URI",
    "http://localhost:8000/auth/google/callback",
)


# ===============================
# GITHUB OAUTH2
# ===============================
GITHUB_CLIENT_ID     = os.getenv("GITHUB_CLIENT_ID", "")
GITHUB_CLIENT_SECRET = os.getenv("GITHUB_CLIENT_SECRET", "")
GITHUB_REDIRECT_URI  = os.getenv(
    "GITHUB_REDIRECT_URI",
    "http://localhost:8000/auth/github/callback",
)