from django.db import models
import json

class Scan(models.Model):
    SCAN_TYPES = (
        ('phone', 'Phone Number'),
        ('email', 'Email Address'),
        ('name', 'Person Name'),
        ('username', 'Username'),
        ('ip', 'IP Address'),
    )

    target_type = models.CharField(max_length=20, choices=SCAN_TYPES)
    target_value = models.CharField(max_length=255)
    results_json = models.TextField(default='{}')
    risk_score = models.IntegerField(default=0) # 0 to 100
    created_at = models.DateTimeField(auto_now_add=True)

    def get_results(self):
        try:
            return json.loads(self.results_json)
        except:
            return {}

    def __str__(self):
        return f"{self.target_type}: {self.target_value} ({self.created_at})"

    class Meta:
        ordering = ['-created_at']