from django.db import models
from django.utils import timezone


class Stewardship(models.Model):
    STATUS_AWAITING_LOCATION = "awaiting_location"
    STATUS_AWAITING_TARGET = "awaiting_target"
    STATUS_ACTIVE = "active"
    STATUS_EXPIRED = "expired"

    STATUS_CHOICES = [
        (STATUS_AWAITING_LOCATION, "Awaiting location"),
        (STATUS_AWAITING_TARGET, "Awaiting target bot"),
        (STATUS_ACTIVE, "Active"),
        (STATUS_EXPIRED, "Expired"),
    ]

    platform = models.CharField(max_length=20, default="telegram")
    user_id = models.CharField(max_length=64)
    bot_name = models.CharField(max_length=80)

    status = models.CharField(max_length=32, choices=STATUS_CHOICES, default=STATUS_AWAITING_LOCATION)
    geohash = models.CharField(max_length=12, blank=True, null=True)
    lat = models.FloatField(null=True, blank=True)
    lon = models.FloatField(null=True, blank=True)
    target_bot = models.CharField(max_length=80, blank=True, null=True)

    energy = models.PositiveIntegerField(default=0)
    expires_at = models.DateTimeField(null=True, blank=True)
    pending_until = models.DateTimeField(null=True, blank=True)

    created_at = models.DateTimeField(default=timezone.now, editable=False)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        unique_together = ("platform", "user_id")
        indexes = [
            models.Index(fields=["platform", "user_id"]),
            models.Index(fields=["geohash", "status", "expires_at"]),
        ]

    def __str__(self) -> str:
        cell = self.geohash or "unset"
        return f"{self.platform}:{self.user_id} @ {cell} ({self.status})"


class StewardBlessing(models.Model):
    steward = models.ForeignKey(Stewardship, on_delete=models.CASCADE, related_name="blessings")
    geohash = models.CharField(max_length=12)
    target_bot = models.CharField(max_length=80)
    multiplier = models.FloatField(default=2.0)
    starts_at = models.DateTimeField(default=timezone.now)
    ends_at = models.DateTimeField()
    created_at = models.DateTimeField(default=timezone.now, editable=False)

    class Meta:
        indexes = [
            models.Index(fields=["geohash", "ends_at"]),
            models.Index(fields=["target_bot", "ends_at"]),
        ]

    def __str__(self) -> str:
        return f"Blessing {self.geohash} -> {self.target_bot}"


class StewardSignal(models.Model):
    steward = models.ForeignKey(Stewardship, on_delete=models.CASCADE, related_name="signals")
    geohash = models.CharField(max_length=12)
    target_bot = models.CharField(max_length=80)
    kind = models.CharField(max_length=40, default="signal")
    note = models.TextField(blank=True, default="")
    created_at = models.DateTimeField(default=timezone.now, editable=False)

    class Meta:
        indexes = [
            models.Index(fields=["geohash", "created_at"]),
            models.Index(fields=["target_bot", "created_at"]),
        ]

    def __str__(self) -> str:
        return f"Signal {self.geohash} -> {self.target_bot} ({self.kind})"
