"""Incursions app settings with sane defaults and helpers."""

# Standard Library
from urllib.parse import urlparse, urlunparse

# Django
from django.conf import settings


def get_site_url() -> str:
    """Derive base site URL from ESI SSO callback without regex fragility.

    Falls back directly to settings.SITE_URL when parsing fails.
    """
    fallback = getattr(settings, 'SITE_URL', 'http://localhost:8000')
    callback = getattr(settings, 'ESI_SSO_CALLBACK_URL', fallback)

    if not callback:
        return fallback

    try:
        parsed = urlparse(callback)
        if not parsed.scheme or not parsed.netloc:
            return fallback
        # strip any path segment after first component
        clean = parsed._replace(path='')._replace(params='', query='', fragment='')
        return urlunparse(clean)
    except Exception:
        return fallback


INCURSIONS_AUTO_HIGHSEC_FOCUS = getattr(settings, 'INCURSIONS_AUTO_HIGHSEC_FOCUS', True)

INCURSIONS_SCOPES_BASE = ["publicData", "esi-skills.read_skills.v1", "esi-clones.read_implants.v1"]

INCURSIONS_SCOPES_FC = [
    "esi-fleets.read_fleet.v1",
    "esi-fleets.write_fleet.v1",
    "esi-ui.open_window.v1",
    "esi-search.search_structures.v1",
]


INCURSIONS_MINIMUM_FLEET_SIZE = getattr(settings, 'INCURSIONS_MINIMUM_FLEET_SIZE', 5)


def insert_sse_subdomain(site_url: str) -> str:
    parsed = urlparse(site_url)           # e.g. scheme='http', netloc='localhost:8000', path='', etc.
    parts = parsed.netloc.split(':', 1)   # split hostname from port
    host = parts[0]
    port = parts[1] if len(parts) > 1 else None

    # Prepend 'sse.' to the hostname
    new_host = f"sse.{host}"
    new_netloc = f"{new_host}:{port}" if port else new_host

    # Rebuild the full URL
    new_url = urlunparse((
        parsed.scheme,
        new_netloc,
        parsed.path,
        parsed.params,
        parsed.query,
        parsed.fragment
    ))
    return new_url


SSE_SITE_URL = getattr(settings, 'SSE_SITE_URL', insert_sse_subdomain(settings.SITE_URL))

SSE_SECRET = getattr(settings, 'SSE_SECRET', "0000000000000000000000000000000000000000000000000000000000000000")

EVECATEGORY_SHIP = 6
EVECATEGORY_MODULE = 7
EVECATEGORY_CHARGE = 8
EVECATEGORY_DRONE = 18
EVECATEGORY_SKILLS = 16
EVECATEGORY_IMPLANTS = 20
EVECATEGORY_DEPLOYABLE = 22

# Convenience iterable for tests
EVECATEGORY_ALL = (
    EVECATEGORY_SHIP,
    EVECATEGORY_MODULE,
    EVECATEGORY_CHARGE,
    EVECATEGORY_DRONE,
    EVECATEGORY_SKILLS,
    EVECATEGORY_IMPLANTS,
    EVECATEGORY_DEPLOYABLE,
)

ALWAYS_CARGO_CATEGORIES = [EVECATEGORY_IMPLANTS, EVECATEGORY_CHARGE, EVECATEGORY_DEPLOYABLE]
