from django.db import models


class EphemerisSnapshot(models.Model):
    """Pre-computed chart data for a single point in time.

    Element counts are stored as denormalised columns for fast DB-level range
    filtering.  Full planet/house data lives in chart_data (JSONField) for
    response serialisation.
    """

    dt = models.DateTimeField(unique=True, db_index=True)

    # Denormalised element counts — indexed for range queries
    fire = models.PositiveSmallIntegerField()
    water = models.PositiveSmallIntegerField()
    earth = models.PositiveSmallIntegerField()
    air = models.PositiveSmallIntegerField()
    time_el = models.PositiveSmallIntegerField()
    space_el = models.PositiveSmallIntegerField()

    # Full chart payload
    chart_data = models.JSONField()

    class Meta:
        ordering = ['dt']

    def elements_dict(self):
        return {
            'Fire': self.fire,
            'Water': self.water,
            'Earth': self.earth,
            'Air': self.air,
            'Time': self.time_el,
            'Space': self.space_el,
        }
