import os
import sys
from pathlib import Path

def get_package_dir():
    """Returns the directory where the package is installed."""
    # This returns the directory containing this file (core/)
    # Then we go up one level to get rt_commission_dashboard/
    return Path(__file__).parent.parent

def get_app_dir():
    """Returns the root directory of the application for user data."""
    # Check for env var override first
    env_dir = os.getenv('RT_COMMISSION_DATA_DIR')
    if env_dir:
        expanded_dir = os.path.expandvars(env_dir)
        # Return parent directory (without /data suffix)
        return Path(expanded_dir).parent

    # Determine if running as a script or frozen exe
    if getattr(sys, 'frozen', False):
        return Path(sys.executable).parent
    else:
        # Use user's home directory for writable storage
        return Path.home() / '.rt-commission-dashboard'

def get_data_dir():
    """Returns the directory where data should be stored."""
    # Check for env var override
    env_dir = os.getenv('RT_COMMISSION_DATA_DIR')
    if env_dir:
        # Expand environment variables in the path
        expanded_dir = os.path.expandvars(env_dir)
        path = Path(expanded_dir)
        path.mkdir(parents=True, exist_ok=True)
        return path

    # Default to user's home directory for writable storage
    path = get_app_dir() / 'data'
    path.mkdir(parents=True, exist_ok=True)
    return path

def get_db_path():
    """Returns the path to the SQLite database."""
    from rt_commission_dashboard.core.config import config
    return get_data_dir() / config.get_database_filename()

def get_config_path():
    """Returns the path to the config file."""
    return get_app_dir() / 'config' / 'settings.yaml'

def get_locales_path():
    """Returns the path to the locales directory inside the package."""
    # Locales are part of the installed package
    return get_package_dir() / 'locales'
