"""Central configuration for the BEAVER code project.

Single source of truth for constants, layer-name lists, citation sources, and
filesystem paths that were previously duplicated across the three main modules.

All path constants are derived from this file's own location on disk so the
application works without modification on any machine, in any directory, under
any username.  Every path can still be overridden via environment variable for
development or CI purposes:

    BEAVER_PROJECT_PATH       overrides QGIS_PROJECT_PATH
    BEAVER_ANALYTICS_PATH     overrides ANALYTICS_CSV_PATH
    BEAVER_FLAGS_DIR          overrides FLAGS_DIR
    BEAVER_LOGOS_DIR          overrides LOGOS_DIR
    BEAVER_ICONS_DIR          overrides ICONS_DIR
    BEAVER_OUTPUT_DIR         overrides OUTPUT_DIR
"""

import os

# This file lives in the ``plugin`` subpackage alongside the bundled project,
# data and assets. Paths are derived from this location so the app is fully
# portable (any machine, any install directory).
_HERE = os.path.dirname(os.path.abspath(__file__))


def _writable_base_dir():
    """Per-user, writable directory for outputs and analytics.

    A published plugin's install directory can be read-only, so anything the
    app writes at runtime (built-map cache, analytics CSV) must live in the
    user's own space. Prefer the QGIS profile dir; fall back to the home
    directory when QGIS isn't initialised (should not happen in practice).
    """
    override = os.environ.get("BEAVER_DATA_DIR")
    if override:
        base = override
    else:
        base = None
        try:
            from qgis.core import QgsApplication
            settings = QgsApplication.qgisSettingsDirPath()
            if settings:
                base = os.path.join(settings, "beaver_processor")
        except Exception:
            base = None
        if not base:
            base = os.path.join(os.path.expanduser("~"), ".beaver_processor")
    try:
        os.makedirs(base, exist_ok=True)
    except Exception:
        pass
    return base


_DATA_DIR = _writable_base_dir()


# ---- Filesystem paths --------------------------------------------------------

QGIS_PROJECT_PATH = os.environ.get(
    "BEAVER_PROJECT_PATH",
    os.path.join(_HERE, "beaver_master_project.qgz"),
)

ANALYTICS_CSV_PATH = os.environ.get(
    "BEAVER_ANALYTICS_PATH",
    os.path.join(_DATA_DIR, "Analytics.csv"),
)

# Country flag PNG images used by the "Insert flag" overlay menu.
FLAGS_DIR = os.environ.get(
    "BEAVER_FLAGS_DIR",
    os.path.join(_HERE, "assets", "flags"),
)

# Organisational logo PNG images used by the "Insert flag" overlay menu.
LOGOS_DIR = os.environ.get(
    "BEAVER_LOGOS_DIR",
    os.path.join(_HERE, "assets", "logos"),
)

# Application icons, splash image, liability graphic, and other UI assets.
ICONS_DIR = os.environ.get(
    "BEAVER_ICONS_DIR",
    os.path.join(_HERE, "assets", "icons"),
)

# Directory where the rolling cache of built (not exported) map PNGs is written.
OUTPUT_DIR = os.environ.get(
    "BEAVER_OUTPUT_DIR",
    os.path.join(_DATA_DIR, "built_maps"),
)


# ---- Preloaded vector layers (in declaration order) --------------------------
# These names match the layer names in the bundled project. The export pipeline
# iterates this list when applying user-driven visibility, style, label and
# size choices to each layer.

# Focus layers exist solely to project/frame/pan the map — they are never
# drawn. Tile basemaps are discovered dynamically (see
# methods.discover_basemap_names), not listed here.
PRELOADED_LAYER_NAMES = [
    "Focus_province",
    "Focus_country",
]


# ---- Basemap raster layers ---------------------------------------------------
# Basemaps are DISCOVERED at runtime from the bundled project — any web/tile
# raster layer (XYZ/WMS/WMTS) in beaver_master_project.qgz is offered as a
# toggle (see methods.discover_basemap_names). Users add their own by saving
# tile layers into that project. The names below are just the shipped defaults,
# kept for reference/citation lookup.
DEFAULT_BASEMAP_NAMES = {
    "OpenStreetMap",
    "OpenTopoMap",
    "CartoDb_Positron",
    "CartoDb_Dark_Matter",
}


# ---- Citation text for the "Data sources" metadata line ----------------------
# Keyed by the preloaded layer's name.
# When a preloaded layer is included on a map, its citation is added to the
# bottom-of-map "Data sources" note. Edit the strings here to update citations
# project-wide.

PRELOADED_LAYER_SOURCES = {
    "Focus_province":              "Natural Earth (2025).",
    "Focus_country":               "Natural Earth (2025).",
    "Secondary_focus_country":     "Natural Earth (2025).",
    "Countries_and_lakes":         "Natural Earth (2025).",
    "Marine_labels":               "Natural Earth (2025).",
    "OpenStreetMap":               "© OpenStreetMap contributors (ODbL).",
    "OpenTopoMap":                 "© OpenStreetMap contributors, SRTM; style © OpenTopoMap (CC-BY-SA).",
    "CartoDb_Positron":            "© OpenStreetMap contributors, © CARTO.",
    "CartoDb_Dark_Matter":         "© OpenStreetMap contributors, © CARTO.",
}
