# -*- coding: utf-8 -*-
"""
HerpMap — Survey Priority Zones (QGIS Processing script)

QGIS port of the ArcGIS Pro .pyt tool. Turns a habitat-suitability mesh into
ranked survey-priority zones in a single run:
  survey-target filter -> optional Getis-Ord Gi* hot spots -> DBSCAN clustering
  -> convex-hull zones with geodesic area, mean/max suitability and a 0-1 rank.

Getis-Ord Gi* and DBSCAN are implemented in pure Python so the script needs no
plugins. Add it via Processing Toolbox -> Scripts -> Add Script to Toolbox…

Tested target: QGIS 3.28+ (PyQGIS).
"""
import math
from qgis.PyQt.QtCore import QCoreApplication, QVariant
from qgis.core import (
    QgsProcessing, QgsProcessingAlgorithm, QgsProcessingException,
    QgsProcessingParameterFeatureSource, QgsProcessingParameterField,
    QgsProcessingParameterNumber, QgsProcessingParameterBoolean,
    QgsProcessingParameterEnum, QgsProcessingParameterExpression,
    QgsProcessingParameterFeatureSink,
    QgsFeature, QgsFields, QgsField, QgsGeometry, QgsPointXY, QgsWkbTypes,
    QgsSpatialIndex, QgsFeatureRequest, QgsExpression, QgsExpressionContext,
    QgsExpressionContextUtils, QgsCoordinateReferenceSystem,
    QgsCoordinateTransform, QgsProject, QgsDistanceArea,
)

Z_THRESH = {0: 1.645, 1: 1.960, 2: 2.576}   # 90% / 95% / 99%


class SurveyPriorityZones(QgsProcessingAlgorithm):
    INPUT = 'INPUT'
    HSI = 'HSI'
    MIN_HSI = 'MIN_HSI'
    NO_RECORDS = 'NO_RECORDS'
    UNSURVEYED = 'UNSURVEYED'
    EXPANDING = 'EXPANDING'
    EXTRA = 'EXTRA'
    USE_GI = 'USE_GI'
    GI_CONF = 'GI_CONF'
    MIN_CLUSTER = 'MIN_CLUSTER'
    SEARCH_DIST = 'SEARCH_DIST'
    OUTPUT = 'OUTPUT'

    def tr(self, s):
        return QCoreApplication.translate('SurveyPriorityZones', s)

    def createInstance(self):
        return SurveyPriorityZones()

    def name(self):
        return 'surveypriorityzones'

    def displayName(self):
        return self.tr('Survey Priority Zones')

    def group(self):
        return self.tr('HerpMap HSM Tools')

    def groupId(self):
        return 'herpmaphsm'

    def shortHelpString(self):
        return self.tr(
            'Turns a habitat-suitability mesh into ranked survey-priority zones: '
            'filters to high-suitability survey targets, optionally keeps only the '
            'statistically significant Getis-Ord Gi* hot spots, clusters them with '
            'DBSCAN, and outputs convex-hull zones with geodesic area, mean/max '
            'suitability and a 0-1 priority rank.')

    def initAlgorithm(self, config=None):
        self.addParameter(QgsProcessingParameterFeatureSource(
            self.INPUT, self.tr('Habitat suitability mesh (polygons)'),
            [QgsProcessing.TypeVectorPolygon]))
        self.addParameter(QgsProcessingParameterField(
            self.HSI, self.tr('Suitability field'), parentLayerParameterName=self.INPUT,
            defaultValue='hsi', type=QgsProcessingParameterField.Numeric))
        self.addParameter(QgsProcessingParameterNumber(
            self.MIN_HSI, self.tr('Minimum suitability (survey-target cutoff)'),
            type=QgsProcessingParameterNumber.Double, defaultValue=0.9))
        self.addParameter(QgsProcessingParameterBoolean(
            self.NO_RECORDS, self.tr('Only cells with no records (pres_n = 0)'), defaultValue=True))
        self.addParameter(QgsProcessingParameterBoolean(
            self.UNSURVEYED, self.tr('Only unsurveyed cells (effort_n = 0)'), defaultValue=False))
        self.addParameter(QgsProcessingParameterBoolean(
            self.EXPANDING, self.tr("Only expanding habitat (change = 'gain')"), defaultValue=False))
        self.addParameter(QgsProcessingParameterExpression(
            self.EXTRA, self.tr('Additional filter (optional expression)'),
            parentLayerParameterName=self.INPUT, optional=True))
        self.addParameter(QgsProcessingParameterBoolean(
            self.USE_GI, self.tr('Require statistically significant hot spots (Getis-Ord Gi*)'),
            defaultValue=True))
        self.addParameter(QgsProcessingParameterEnum(
            self.GI_CONF, self.tr('Hot-spot confidence'),
            options=['90%', '95%', '99%'], defaultValue=1))
        self.addParameter(QgsProcessingParameterNumber(
            self.MIN_CLUSTER, self.tr('DBSCAN - minimum cells per cluster'),
            type=QgsProcessingParameterNumber.Integer, defaultValue=5, minValue=2))
        self.addParameter(QgsProcessingParameterNumber(
            self.SEARCH_DIST, self.tr('DBSCAN - search distance in metres (0 = auto)'),
            type=QgsProcessingParameterNumber.Double, defaultValue=0.0, minValue=0.0))
        self.addParameter(QgsProcessingParameterFeatureSink(
            self.OUTPUT, self.tr('Survey priority zones')))

    # ---- helpers ----------------------------------------------------------
    @staticmethod
    def _field_index(fields, *names):
        lower = {fields.at(i).name().lower(): i for i in range(fields.count())}
        for n in names:
            if n.lower() in lower:
                return lower[n.lower()]
        return -1

    def processAlgorithm(self, parameters, context, feedback):
        src = self.parameterAsSource(parameters, self.INPUT, context)
        if src is None:
            raise QgsProcessingException(self.tr('No input mesh.'))
        hsi_name = self.parameterAsString(parameters, self.HSI, context)
        min_hsi = self.parameterAsDouble(parameters, self.MIN_HSI, context)
        no_records = self.parameterAsBool(parameters, self.NO_RECORDS, context)
        unsurveyed = self.parameterAsBool(parameters, self.UNSURVEYED, context)
        expanding = self.parameterAsBool(parameters, self.EXPANDING, context)
        extra_expr_str = self.parameterAsExpression(parameters, self.EXTRA, context)
        use_gi = self.parameterAsBool(parameters, self.USE_GI, context)
        gi_conf = self.parameterAsEnum(parameters, self.GI_CONF, context)
        min_cluster = self.parameterAsInt(parameters, self.MIN_CLUSTER, context)
        search_dist = self.parameterAsDouble(parameters, self.SEARCH_DIST, context)

        fields = src.fields()
        i_hsi = self._field_index(fields, hsi_name)
        if i_hsi < 0:
            raise QgsProcessingException(self.tr('Suitability field "%s" not found.') % hsi_name)
        i_pres = self._field_index(fields, 'pres_n')
        i_eff = self._field_index(fields, 'effort_n')
        i_chg = self._field_index(fields, 'change')

        src_crs = src.sourceCrs()
        wgs = QgsCoordinateReferenceSystem('EPSG:4326')
        to_wgs = QgsCoordinateTransform(src_crs, wgs, QgsProject.instance())

        # --- read all cells: geometry, hsi, centroid (source CRS + lon/lat) ---
        cells = []   # dict per cell
        extra_expr = None
        ectx = None
        if extra_expr_str:
            extra_expr = QgsExpression(extra_expr_str)
            ectx = QgsExpressionContext()
            ectx.appendScopes(QgsExpressionContextUtils.globalProjectLayerScopes(None))

        for f in src.getFeatures():
            g = f.geometry()
            if g is None or g.isEmpty():
                continue
            try:
                hsi = float(f[i_hsi])
            except (TypeError, ValueError):
                continue
            c = g.centroid().asPoint()
            lonlat = to_wgs.transform(QgsPointXY(c))
            cells.append({
                'f': f, 'g': g, 'hsi': hsi, 'c': QgsPointXY(c),
                'lon': lonlat.x(), 'lat': lonlat.y(),
            })
        n = len(cells)
        if n == 0:
            raise QgsProcessingException(self.tr('No usable cells (check the suitability field).'))
        feedback.pushInfo('Read %d mesh cells.' % n)

        # --- Getis-Ord Gi* over all cells (optional) -------------------------
        sig = [True] * n   # significant-hot-spot flag per cell
        if use_gi:
            feedback.pushInfo('Computing Getis-Ord Gi* hot spots…')
            index = QgsSpatialIndex()
            for idx, cell in enumerate(cells):
                feat = QgsFeature(idx)
                feat.setGeometry(cell['g'])
                index.insertFeature(feat)
            x = [c['hsi'] for c in cells]
            xbar = sum(x) / n
            s = math.sqrt(max(0.0, sum(v * v for v in x) / n - xbar * xbar))
            if s == 0:
                feedback.pushWarning('All suitability values are equal; skipping Gi*.')
            else:
                thresh = Z_THRESH.get(gi_conf, 1.960)
                denom_const = (n - 1)
                for i in range(n):
                    if feedback.isCanceled():
                        return {}
                    gi = cells[i]['g']
                    cand = index.intersects(gi.boundingBox())
                    neigh = [i]
                    for j in cand:
                        if j == i:
                            continue
                        if gi.intersects(cells[j]['g']):   # shares an edge or corner
                            neigh.append(j)
                    w = len(neigh)                         # binary weights incl. self
                    sum_wx = sum(x[j] for j in neigh)
                    num = sum_wx - xbar * w
                    den = s * math.sqrt(max(1e-12, (w * (n - w)) / denom_const))
                    z = num / den if den > 0 else 0.0
                    sig[i] = (z >= thresh)                 # high-value (hot) spot
                feedback.pushInfo('%d significant hot-spot cells (%s).'
                                  % (sum(1 for v in sig if v), ['90%', '95%', '99%'][gi_conf]))

        # --- survey-target filter -------------------------------------------
        targets = []
        for i, cell in enumerate(cells):
            f = cell['f']
            if cell['hsi'] < min_hsi:
                continue
            if no_records and i_pres >= 0 and _num(f[i_pres]) != 0:
                continue
            if unsurveyed and i_eff >= 0 and _num(f[i_eff]) != 0:
                continue
            if expanding and i_chg >= 0 and str(f[i_chg]).lower() != 'gain':
                continue
            if extra_expr is not None:
                ectx.setFeature(f)
                if not bool(extra_expr.evaluate(ectx)):
                    continue
            if use_gi and not sig[i]:
                continue
            targets.append(cell)
        feedback.pushInfo('%d survey-target cells after filtering.' % len(targets))
        if len(targets) < min_cluster:
            raise QgsProcessingException(
                self.tr('Only %d survey-target cells — fewer than the minimum cluster size. '
                        'Loosen the filters or lower the minimum.') % len(targets))

        # --- metric coordinates for DBSCAN ----------------------------------
        if src_crs.isGeographic():
            cx = sum(c['lon'] for c in targets) / len(targets)
            cy = sum(c['lat'] for c in targets) / len(targets)
            zone = int((cx + 180.0) / 6.0) + 1
            epsg = (32600 if cy >= 0 else 32700) + zone
            metric_crs = QgsCoordinateReferenceSystem('EPSG:%d' % epsg)
            to_metric = QgsCoordinateTransform(src_crs, metric_crs, QgsProject.instance())
            for c in targets:
                p = to_metric.transform(c['c'])
                c['mx'], c['my'] = p.x(), p.y()
        else:
            for c in targets:
                c['mx'], c['my'] = c['c'].x(), c['c'].y()

        # --- auto search distance from geodesic cell size -------------------
        da = QgsDistanceArea()
        da.setEllipsoid('WGS84')
        da.setSourceCrs(src_crs, QgsProject.instance().transformContext())
        if search_dist <= 0:
            areas = []
            for c in targets[:300]:
                a = da.measureArea(c['g'])
                if a and a > 0:
                    areas.append(a)
            if areas:
                areas.sort()
                med = areas[len(areas) // 2]
                search_dist = max(100.0, 2.5 * math.sqrt(med))
            else:
                search_dist = 1000.0
            feedback.pushInfo('DBSCAN search distance: %.0f m (auto).' % search_dist)
        else:
            feedback.pushInfo('DBSCAN search distance: %.0f m.' % search_dist)

        # --- DBSCAN (pure Python, grid-indexed) -----------------------------
        labels = _dbscan([(c['mx'], c['my']) for c in targets], search_dist, min_cluster)
        clusters = {}
        for c, lab in zip(targets, labels):
            if lab < 0:
                continue
            clusters.setdefault(lab, []).append(c)
        feedback.pushInfo('%d clusters found (noise dropped).' % len(clusters))
        if not clusters:
            raise QgsProcessingException(
                self.tr('No clusters formed. Try a larger search distance or a smaller minimum.'))

        # --- build zones: convex hull + stats -------------------------------
        out_fields = QgsFields()
        out_fields.append(QgsField('cluster_id', QVariant.Int))
        out_fields.append(QgsField('n_cells', QVariant.Int))
        out_fields.append(QgsField('mean_hsi', QVariant.Double))
        out_fields.append(QgsField('max_hsi', QVariant.Double))
        out_fields.append(QgsField('area_km2', QVariant.Double))
        out_fields.append(QgsField('priority', QVariant.Double))
        out_fields.append(QgsField('rank', QVariant.Int))

        (sink, dest_id) = self.parameterAsSink(
            parameters, self.OUTPUT, context, out_fields, QgsWkbTypes.Polygon, src_crs)

        zones = []
        for cid, members in clusters.items():
            pts = [m['c'] for m in members]                      # source-CRS centroids
            hull = QgsGeometry.fromMultiPointXY(pts).convexHull()
            if hull is None or hull.isEmpty():
                continue
            mean_hsi = sum(m['hsi'] for m in members) / len(members)
            max_hsi = max(m['hsi'] for m in members)
            area_km2 = da.measureArea(hull) / 1e6 if not hull.isEmpty() else 0.0
            zones.append({'cid': cid, 'n': len(members), 'mean': mean_hsi,
                          'max': max_hsi, 'area': area_km2, 'hull': hull})

        # --- priority (normalised) + rank ------------------------------------
        if zones:
            mn = min(z['mean'] for z in zones)
            mx = max(z['mean'] for z in zones)
            amx = max(z['area'] for z in zones) or 1.0
            rng = (mx - mn) or 1.0
            for z in zones:
                z['priority'] = round(0.7 * (z['mean'] - mn) / rng + 0.3 * (z['area'] / amx), 4)
            zones.sort(key=lambda z: z['priority'], reverse=True)
            for rank, z in enumerate(zones, start=1):
                z['rank'] = rank

        for z in zones:
            feat = QgsFeature(out_fields)
            feat.setGeometry(z['hull'])
            feat.setAttributes([int(z['cid']), int(z['n']), float(round(z['mean'], 4)),
                                float(round(z['max'], 4)), float(round(z['area'], 4)),
                                float(z['priority']), int(z['rank'])])
            sink.addFeature(feat)

        feedback.pushInfo('Done: %d survey-priority zones written.' % len(zones))
        return {self.OUTPUT: dest_id}


def _num(v):
    try:
        return float(v)
    except (TypeError, ValueError):
        return None


def _dbscan(points, eps, min_pts):
    """Grid-indexed DBSCAN. points: list of (x, y) in metres. Returns a label per
    point: cluster id (>=0) or -1 for noise."""
    n = len(points)
    labels = [-2] * n                       # -2 = unvisited, -1 = noise
    cell = eps if eps > 0 else 1.0
    grid = {}
    for i, (x, y) in enumerate(points):
        grid.setdefault((int(x // cell), int(y // cell)), []).append(i)

    def region(i):
        x, y = points[i]
        gx, gy = int(x // cell), int(y // cell)
        out = []
        for ax in (gx - 1, gx, gx + 1):
            for ay in (gy - 1, gy, gy + 1):
                for j in grid.get((ax, ay), ()):
                    dx = points[j][0] - x
                    dy = points[j][1] - y
                    if dx * dx + dy * dy <= eps * eps:
                        out.append(j)
        return out

    cid = -1
    for i in range(n):
        if labels[i] != -2:
            continue
        neigh = region(i)
        if len(neigh) < min_pts:
            labels[i] = -1
            continue
        cid += 1
        labels[i] = cid
        seeds = [j for j in neigh if j != i]
        k = 0
        while k < len(seeds):
            j = seeds[k]
            k += 1
            if labels[j] == -1:
                labels[j] = cid
            if labels[j] != -2:
                continue
            labels[j] = cid
            jn = region(j)
            if len(jn) >= min_pts:
                seeds.extend(jn)
    return labels
