# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import User
from django.db.models.fields import IntegerField
from django.db.models.lookups import In

class WhookData(models.Model):
        data=models.TextField(blank=True,null=True)
        creacion = models.DateTimeField(auto_now_add=True)
        estados=models.TextField(blank=True,null=True)
        ids=models.TextField(blank=True,null=True)


# Create your models here.
class Perfil(models.Model):
    perfil_opciones = (('ADM', 'Administración'), ('PV', 'Punto de Venta'),
                        ('SIN', 'Sindicalizado'), ('AGN', 'Agencia'))
    user = models.ForeignKey(
        User, related_name='user_perfil', on_delete=models.PROTECT)
    # rfc=models.CharField(max_length=23)
    # curp=models.CharField(max_length=28,blank=True,null=True)
    # nss=models.CharField(max_length=21,blank=True,null=True)
    # num_emp=models.IntegerField(blank=True,null=True)
    # perf_emp=models.CharField(max_length=20,blank=True,null=True,choices=perfil_opciones)
    # fecha_nac=models.DateField(blank=True,null=True)
    empresa = models.CharField(max_length=200, blank=True, null=True)
    direccion = models.CharField(max_length=200, blank=True, null=True)
    notas = models.CharField(max_length=200, blank=True, null=True)
    telefono = models.CharField(max_length=20, blank=True, null=True)
    wap_id = models.CharField(max_length=20, blank=True, null=True)

    def __unicode__(self):
        return self.telefono

    def __str__(self):
        return self.telefono
class Lista(models.Model):
    nombre = models.CharField(max_length=40)
    contactos = models.ManyToManyField(Perfil)
    activo = models.BooleanField(default=True)
    creacion = models.DateTimeField(auto_now_add=True)
    modificado = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return str(self.nombre)

    def __str__(self):
        return str(self.nombre)

class ImportFile(models.Model):
    estado_choices=((0,'Nuevo'),(1,'Campos seleccionados'),(2,'Importado'),(5,'Cancelado'))
    user=models.ForeignKey(User,on_delete=models.PROTECT)
    archivo=models.FileField(upload_to='excel_imports')
    nombre_original=models.CharField(max_length=250,default='file.xls')
    total_contactos=models.IntegerField(default=0)
    contactos_nuevos=models.IntegerField(default=0)
    contactos_actualizados=models.IntegerField(default=0)
    contactos_warning=models.IntegerField(default=0)
    contactos_error=models.IntegerField(default=0)
    to_list=models.ForeignKey(Lista,null=True,blank=True,on_delete=models.PROTECT)
    listas_multiples=models.CharField(max_length=500,default=None,null=True)
    estado=models.IntegerField(choices=estado_choices,default=0)

class ImportContact(models.Model):
    fila=models.IntegerField()
    tipos_error=((1,'ERROR'),(2,'ADVERTENCIA'))
    archivo=models.ForeignKey(ImportFile,on_delete=models.PROTECT)
    nombre=models.CharField(max_length=250)
    error=models.IntegerField(choices=tipos_error,default=1)
    comentario=models.CharField(max_length=250,default='')

class ColumnasImport(models.Model):
    archivo=models.ForeignKey(ImportFile,on_delete=models.PROTECT)
    columns_ids=((0,'NO USAR'),(1,'NOMBRES'),(2,'APELLIDOS'),(3,'CORREO'),(4,'TELEFONO'),(5,'EMPRESA'),(6,'DIRECCION'),(7,'NOTAS'),(8,'LISTAS'))
    col_number=models.IntegerField(default=0)
    column_name=models.CharField(max_length=250)
    column_map=models.IntegerField(choices=columns_ids,default=0)


    
class Plantilla(models.Model):
    tipo_choices = ((0, 'Texto'), (1, 'Media e Interactivo'))

    titulo = models.CharField(max_length=250, blank=False, null=False,unique=False)
    texto = models.TextField(null=False, blank=False)
    estado=models.CharField(max_length=25,blank=False,default="enviada")
    tipo=models.IntegerField(choices=tipo_choices,default=0)
    has_image=models.BooleanField(default=False,blank=True)
    img_file = models.FileField(upload_to='images_uploaded',null=True,blank=True)
    boton_url=models.TextField(null=True,blank=True)
    boton_url_txt=models.TextField(max_length=20,null=True,blank=True)

    boton_tel=models.TextField(null=True,blank=True)
    boton_tel_txt=models.TextField(max_length=20,null=True,blank=True)


    creado = models.DateTimeField(auto_now_add=True)
    modificado = models.DateTimeField(auto_now=True)
    borrado=models.BooleanField(default=False,blank=True)

    def __unicode__(self):
        return str(self.titulo)

    def __str__(self):
        return str(self.titulo)
    

class Campaign(models.Model):
    estado_envio = ((0, 'Creado'), (1, 'Enviando'), (2, 'Finalizado'),(3,'Programado'),(4,'Enviando (Programado)'))
    titulo = models.CharField(max_length=250, blank=True, default='')
    texto = models.TextField(null=False, blank=False)
    total = models.IntegerField(default=0)
    exitosos = models.IntegerField(default=0)
    fallidos = models.IntegerField(default=0)
    estado = models.IntegerField(choices=estado_envio, default=0)
    fecha = models.DateTimeField(auto_now_add=True)
    modificado = models.DateTimeField(auto_now=True)
    hora_programada=models.DateTimeField(null=True,blank=True, auto_now=False, auto_now_add=False)
    lista = models.ForeignKey(
        'Lista', null=True, blank=True, on_delete=models.PROTECT)
    plantilla= models.ForeignKey('Plantilla',null=True,blank=True,on_delete=models.PROTECT)
    def __unicode__(self):
        return str(self.fecha)

    def __str__(self):
        return str(self.fecha)





class Campaign_contacto(models.Model):
    estado_envio = ((0, 'Creado'), (1, 'Enviando'),
                    (2, 'Entregado'), (3, 'Leido'),(4,'Borrado'),(5,'Error'))
    campaign = models.ForeignKey(Campaign, on_delete=models.PROTECT)
    telefono = models.CharField(max_length=20)
    perfil = models.ForeignKey(
        Perfil, on_delete=models.PROTECT, null=True, blank=True)
    estado = models.IntegerField(choices=estado_envio, default=0)
    id_360=models.CharField(max_length=100,null=True,blank=True)

    creacion = models.DateTimeField(auto_now_add=True)
    modificado = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return str(self.campaign)+" T: "+str(self.telefono)

    def __str__(self):
        return str(self.campaign)+" T: "+str(self.telefono)