# -*- coding: utf-8 -*-
# SPDX-License-Identifier: Apache-2.0
"""Project metadata."""
import os
from importlib import metadata

import toml

try:
    # Try getting metadata from the installed package first (standard for PyPI/pip)
    __version__ = metadata.version("iechelle")
    # For newer python/importlib versions, metadata() returns a dict-like object
    meta = metadata.metadata("iechelle")
    __name_soft__ = meta.get("Name", "iechelle")
    __title__ = meta.get("Name", "iechelle")
    __description__ = meta.get("Summary", "")
    __url__ = meta.get("Home-page", "")
    __author__ = meta.get("Author", "")
    __author_email__ = meta.get("Author-email", "")
    __license__ = meta.get("License", "")
except Exception:
    # Fallback to pyproject.toml only for local development/un-installed source
    try:
        project_root = os.path.dirname(os.path.dirname(__file__))
        pyproject_path = os.path.join(project_root, "pyproject.toml")

        with open(pyproject_path, "r") as file:
            pyproject_content = toml.load(file)

        project_tool = pyproject_content.get("tool", {})
        project_metadata = project_tool.get("poetry", {})

        __name_soft__ = project_metadata.get("name", "unknown")
        __version__ = project_metadata.get("version", "0.0.0")
        __title__ = project_metadata.get("name", "unknown")
        __description__ = project_metadata.get("description", "")
        __url__ = project_metadata.get("homepage", "")
        __author__ = project_metadata.get("authors", [{}])[0]
        __author_email__ = project_metadata.get("authors", [{}])[0]
        __license__ = project_metadata.get("license", "")
    except Exception:
        # Last resort fallback values
        __name_soft__ = "iechelle"
        __version__ = "0.0.0"
        __title__ = "iechelle"
        __description__ = ""
        __url__ = ""
        __author__ = ""
        __author_email__ = ""
        __license__ = ""

__copyright__ = "2024, CEA (Dinil Bose Palakkatharappil)"
