from django.db import models
from django.contrib.auth.models import User
import os
from model_utils import Choices
from universidad.models import laboratorio, asignatura, area, material

# Create your models here.

class lab(models.Model):
    USO = Choices('Practica', 'Clase', 'Libre')
    usuario = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    id_lab = models.ForeignKey(laboratorio, on_delete=models.CASCADE, null=True, blank=True, verbose_name='Laboratorio')
    id_asig = models.ForeignKey(asignatura, on_delete=models.CASCADE, null=True, blank=True, verbose_name='Asignatura')
    id_area = models.ForeignKey(area, on_delete=models.CASCADE, null=True, blank=True, verbose_name='Area')
    id_mat = models.ForeignKey(material, on_delete=models.CASCADE, null=True, blank=True, verbose_name='Material')
    uso = models.CharField(choices=USO, default=USO.Clase, max_length=10, verbose_name='Tipo de Uso')
    foto = models.ImageField(upload_to='foto/', blank=True)
    entrada = models.DateField(auto_now_add=True, verbose_name='Entrada')
    salida = models.DateField(auto_now=True, verbose_name='Salida')
    fecha_alta = models.DateField(auto_now_add=True, verbose_name='Fecha alta')
    fecha_actualizacion = models.DateField(auto_now=True, verbose_name='Fecha de Actualización')
        
    def delete(self, *args, **kwargs):
        if os.path.isfile(self.foto.path):
            os.remove(self.foto.path)
        super(User, self).delete(*args, **kwargs)
    
    def __str__(self):
        return self.entrada
    
    class Meta:
        verbose_name = 'Laboratorio'
        verbose_name_plural = 'Laboratorios'
        ordering = ['-entrada']


class labComputo(models.Model):
    USO = Choices('Practica', 'Clase', 'Libre')
    usuario = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    id_lab = models.ForeignKey(laboratorio, on_delete=models.CASCADE, null=True, blank=True, verbose_name='Laboratorio')
    id_asig = models.ForeignKey(asignatura, on_delete=models.CASCADE, null=True, blank=True, verbose_name='Asignatura')
    id_area = models.ForeignKey(area, on_delete=models.CASCADE, null=True, blank=True, verbose_name='Area')
    id_mat = models.ForeignKey(material, on_delete=models.CASCADE, null=True, blank=True, verbose_name='Material')
    uso = models.CharField(choices=USO, default=USO.Clase, max_length=10, verbose_name='Tipo de Uso')
    foto = models.ImageField(upload_to='foto/', blank=True)
    entrada = models.DateTimeField(auto_now_add=True, verbose_name='Entrada')
    salida = models.DateTimeField(auto_now=True, verbose_name='Salida')
    fecha_alta = models.DateTimeField(auto_now_add=True, verbose_name='Fecha alta')
    fecha_actualizacion = models.DateTimeField(auto_now=True, verbose_name='Fecha de Actualización')
        
    def delete(self, *args, **kwargs):
        if os.path.isfile(self.foto.path):
            os.remove(self.foto.path)
        super(User, self).delete(*args, **kwargs)
    
    def __str__(self):
        return self.entrada
    
    class Meta:
        verbose_name = 'Laboratorio'
        verbose_name_plural = 'Laboratorios'
        ordering = ['-id']