from django.conf import settings
from django.db import models


class LocationPolicy(models.Model):
    """Per organization: where may people check in / out from."""

    organization = models.OneToOneField("organizations.Organization", on_delete=models.CASCADE, related_name="location_policy")
    enabled = models.BooleanField(default=False)
    use_location = models.BooleanField(default=True)  # GPS / browser location inside a work place
    use_network = models.BooleanField(default=True)  # request comes from an allowed office network
    max_accuracy_m = models.PositiveIntegerField(default=200)  # worse accuracy than this cannot prove presence
    apply_to_checkout = models.BooleanField(default=True)


class WorkPlace(models.Model):
    organization = models.ForeignKey("organizations.Organization", on_delete=models.CASCADE, related_name="work_places")
    name = models.CharField(max_length=120)
    latitude = models.FloatField()
    longitude = models.FloatField()
    radius_m = models.PositiveIntegerField(default=150)
    is_active = models.BooleanField(default=True)

    class Meta:
        ordering = ["id"]


class AllowedNetwork(models.Model):
    organization = models.ForeignKey("organizations.Organization", on_delete=models.CASCADE, related_name="allowed_networks")
    label = models.CharField(max_length=120)
    cidr = models.CharField(max_length=64)  # a single address or a network, e.g. 5.6.7.8 or 5.6.7.0/24

    class Meta:
        ordering = ["id"]


class LocationExemption(models.Model):
    """The person may check in from anywhere (remote work, field staff). No dates = always."""

    organization = models.ForeignKey("organizations.Organization", on_delete=models.CASCADE, related_name="location_exemptions")
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="+")
    start_date = models.DateField(null=True, blank=True)
    end_date = models.DateField(null=True, blank=True)
    reason = models.CharField(max_length=200, blank=True, default="")
    created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, related_name="+")
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ["-id"]


class PunchLog(models.Model):
    """Evidence of every check-in / check-out made under a location policy."""

    KINDS = [("in", "in"), ("out", "out")]
    STATUSES = [("approved", "approved"), ("pending", "pending"), ("rejected", "rejected")]

    organization = models.ForeignKey("organizations.Organization", on_delete=models.CASCADE, related_name="punch_logs")
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="punch_logs")
    kind = models.CharField(max_length=3, choices=KINDS)
    date = models.DateField()
    time = models.TimeField()
    latitude = models.FloatField(null=True, blank=True)
    longitude = models.FloatField(null=True, blank=True)
    accuracy_m = models.FloatField(null=True, blank=True)
    ip_address = models.CharField(max_length=64, blank=True, default="")
    distance_m = models.FloatField(null=True, blank=True)  # to the nearest work place
    place_name = models.CharField(max_length=120, blank=True, default="")
    verdict = models.CharField(max_length=20)  # see services.Verdict
    status = models.CharField(max_length=10, choices=STATUSES, default="approved")
    reviewed_by = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL, related_name="+")
    reviewed_at = models.DateTimeField(null=True, blank=True)
    review_note = models.CharField(max_length=500, blank=True, default="")
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ["-created_at", "-id"]
        indexes = [models.Index(fields=["organization", "status"])]
