from django.db import models
from django.contrib.gis.db.models.fields import PointField
from django.contrib.gis.db.models.functions import Distance

class Marker(models.Model):
  timestamp = models.DateTimeField(unique=True)
  location = PointField(dim=2)
  alt = models.FloatField("Altitude")
  hdop = models.IntegerField(null=True, blank=True)
  speed = models.IntegerField(null=True, blank=True)

class Trip(models.Model):
  startTime = models.DateTimeField()
  endTime = models.DateTimeField()
  name = models.CharField(max_length=255)
  description = models.TextField(null=True, blank=True)

class CensoredLocation(models.Model):
  location = PointField(dim=2)
  radius = models.IntegerField(default=800)
  name = models.CharField(max_length=255)

  def delete_markers(self, markers = None) -> int:
    # matches = Marker.objects.filter(location__distance_lt=(
    #   self.location,
    #   Distance(m=self.radius)
    # ))

    matches = Marker.objects.annotate(distance=Distance('location', self.location)
       ).filter(distance__lte=self.radius)
    count = len(matches)
    matches.delete()
    return count