from django.db import models
from user_account.models import UserAccount
import os


class Malware(models.Model):
    submissiondate = models.DateTimeField()
    hash = models.CharField(max_length=64)
    vmp = models.CharField(max_length=64)
    # VirusTotal
    firstseen = models.DateTimeField(blank=True, null=True)
    detectionrate = models.DecimalField(max_digits=3, decimal_places=2, blank=True, null=True)
    virustotalurl = models.CharField(max_length=200, blank=True, null=True)
    md5 = models.CharField(max_length=100, blank=True, null=True)
    sha1 = models.CharField(max_length=100, blank=True, null=True)
    sha256 = models.CharField(max_length=100, blank=True, null=True)

    create_hooklog_state = models.BooleanField(default=False)

    def __str__(self):
        return self.hash


def update_filename(instance, filename):
    path = "uploads/"
    format = instance.hash
    return os.path.join(path, format)


class Upload(models.Model):
    name = models.CharField(max_length=64)
    size = models.BigIntegerField()
    hash = models.CharField(max_length=64)
    submissiondate = models.DateTimeField()
    user_ip = models.GenericIPAddressField(null=True)
    user_account = models.ForeignKey(UserAccount, null=True)
    file = models.FileField(upload_to=update_filename, blank=True, null=True)

    def __str__(self):
        return self.name


class VirusTotal(models.Model):
    malware = models.ForeignKey(Malware)
    created = models.DateTimeField(auto_now_add=True)
    virus_total = models.TextField(blank=True, null=True)
    virus_total_file = models.FileField(upload_to='virus_total/', blank=True, null=True)

    def __str__(self):
        return self.malware.hash


class VirusTotalInfo(models.Model):
    vendor = models.CharField(max_length=100)
    version = models.CharField(max_length=50, blank=True, null=True)
    detected = models.BooleanField()
    label = models.CharField(max_length=100, blank=True, null=True)
    update_date = models.DateField(blank=True, null=True)
    malware = models.ForeignKey(Malware)
    md5 = models.CharField(max_length=50)
    virus_total = models.ForeignKey(VirusTotal, blank=True, null=True)


class Hooklog(models.Model):
    hooklog = models.TextField(blank=True, null=True)
    hooklog_file = models.FileField(upload_to='hooklog/', blank=True, null=True)
    malware = models.ForeignKey(Malware)
    created = models.DateTimeField(auto_now_add=True)
