###### Hously space models ######

from django.db import models
import uuid


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

class DataSetSnapshot(models.Model):
    created_at = models.DateTimeField(default=timezone.now)
    model_name = models.CharField(max_length=100)

    total = models.IntegerField()
    title_filled = models.IntegerField()
    title_empty = models.IntegerField()
    original_filled = models.IntegerField()
    original_empty = models.IntegerField()
    images_filled = models.IntegerField()
    images_empty = models.IntegerField()

    class Meta:
        ordering = ['-created_at']






# extractly/models_inactive.py  (albo dodaj do istniejącego models.py)
from django.db import models
from django.conf import settings
from extractly.models import AdsManual
class AdsInactiveSync(models.Model):
    """
    Ewidencja ostatniego wysłania stanu 'inactive' do chmury.
    Pozwala wysłać tylko zmiany (delta po dacie).
    """
    ad = models.OneToOneField(
        AdsManual,
        on_delete=models.CASCADE,
        related_name="inactive_sync",
    )
        # kiedy ostatnio faktycznie wysłaliśmy do chmury:
    last_sent_at = models.DateTimeField(null=True, blank=True)

    # JAKĄ wartość inactive_date wysłaliśmy:
    last_inactive_value = models.DateTimeField(null=True, blank=True)
    
    last_inactive_sent_at = models.DateTimeField(null=True, blank=True)
    last_payload_hash = models.CharField(max_length=64, null=True, blank=True)
    attempts = models.PositiveIntegerField(default=0)
    last_error = models.TextField(null=True, blank=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = "ads_inactive_sync"
        indexes = [
            models.Index(fields=["last_inactive_sent_at"]),
        ]

    def __str__(self):
        return f"InactiveSync(ad_id={self.ad_id}, sent={self.last_inactive_sent_at})"





# monitoring/models.py
from django.db import models

class ScraperStatus(models.Model):
    name = models.CharField(max_length=100, unique=True)
    last_run = models.DateTimeField(auto_now=True)
    success = models.BooleanField(default=False)
    message = models.TextField(blank=True, null=True)

    def __str__(self):
        return f"{self.name} ({'OK' if self.success else 'FAIL'})"




class ScraperConfig(models.Model):
    name = models.CharField(max_length=100, unique=True)
    value = models.CharField(max_length=255)

    def __str__(self):
        return f"{self.name} = {self.value}"



class PhotoConsistencyLog(models.Model):
    model_name = models.CharField(max_length=100)
    checked = models.PositiveIntegerField()
    updated = models.PositiveIntegerField()
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"{self.model_name} | {self.checked} checked, {self.updated} updated at {self.created_at}"



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

class ScraperWorkerLock(models.Model):
    name = models.CharField(max_length=100, unique=True)
    started_at = models.DateTimeField(default=timezone.now)
    pinged_at = models.DateTimeField(default=timezone.now)

    def is_alive(self, timeout_minutes=5):
        from django.utils import timezone
        return self.pinged_at >= timezone.now() - timezone.timedelta(minutes=timeout_minutes)
