from __future__ import unicode_literals
from django.db import models

from configurazione.models import *
from utenti.models import *
from alias.models import *
from liste.models import *
from sicurezza.models import *
from registrar.models import *
from ftp.models import *
from mail.models import *
from database.models import *
from causali.models import *
from target.models import *


class Domini(models.Model):
  id = models.IntegerField(db_column='id',primary_key=True)
  nome = models.CharField(db_column='nome',max_length=50)
  #nome = models.CharField(db_column='nome',max_length=50)

  contatti = models.CharField(db_column='contatti',max_length=256,default='')
  proprietario = models.TextField(db_column='proprietario',default='')

  public_email = models.CharField(db_column='public_email',max_length=128)

  #home: ovvero il default destination (una sorta di alias di dominio)
  home = models.CharField(db_column='home',max_length=50)
 
  #server di storage della posta elettronica
  mail_server = models.ForeignKey('mail.MailServer',db_column='mail_server',on_delete=models.PROTECT)
  lmtp_server = models.ForeignKey('mail.LmtpServer',db_column='lmtp_server',on_delete=models.PROTECT)
  mail_proxy  = models.ForeignKey('mail.MailProxy',db_column='mail_proxy',on_delete=models.PROTECT)
  sql_remote  = models.ForeignKey('mail.SqlRemote',db_column='sql_remote',on_delete=models.PROTECT)

  # 17.1.22
  # la riga mail_server e' stata modificata nel database. l'originale era server.
  # ho anche aggiornato i vari dovecot e postfix.

  #ftp_server = models.ForeignKey('ftp.FTPgroup',db_column='ftp_server',on_delete=models.PROTECT)
  #sql_server = models.IntegerField('sql.SQLserver',db_column='sql_server',default=0)

 
  '''
  questi tre campi (max_*) direi che possono essere spostati nella sezione tipo di contratto insieme alle
  dimensioni e alle caratteristiche del contratto.
  tanto in questo contesto non servono a una mazza
  '''

  max_account = models.IntegerField(db_column='max_account',default=0)
  max_alias   = models.IntegerField(db_column='max_alias',default=0)
  max_list    = models.IntegerField(db_column='max_list',default=0)
 
  mail_quota = models.IntegerField(db_column='mail_quota',default=0)
  http_quota = models.IntegerField(db_column='http_quota',default=0)
  ftp_quota = models.IntegerField(db_column='ftp_quota',default=0)
  sql_quota = models.IntegerField(db_column='sql_quota',default=0)
  nextcloud_quota = models.IntegerField(db_column='nextcloud_quota',default=0)

  last_mail_quota = models.IntegerField(db_column='last_mail_quota',default=0)
  last_http_quota = models.IntegerField(db_column='last_http_quota',default=0)
  last_ftp_quota = models.IntegerField(db_column='last_ftp_quota',default=0)
  last_sql_quota = models.IntegerField(db_column='last_sql_quota',default=0)
  last_nextcloud_quota = models.IntegerField(db_column='last_nextcloud_quota',default=0)

  date_mail_quota = models.DateField(db_column='date_mail_quota')
  date_http_quota = models.DateField(db_column='date_http_quota')
  date_ftp_quota  = models.DateField(db_column='date_ftp_quota')
  date_sql_quota  = models.DateField(db_column='date_sql_quota')
  date_nextcloud_quota = models.DateField(db_column='date_nextcloud_quota')

  # utilizzato per stabilire quando effettuare il calcolo delle percentuali
  last_mail_quota_check = models.BooleanField(db_column='last_mail_quota_check')
  last_http_quota_check = models.BooleanField(db_column='last_http_quota_check')
  last_ftp_quota_check = models.BooleanField(db_column='last_ftp_quota_check')
  last_sql_quota_check = models.BooleanField(db_column='last_sql_quota_check')
  last_nextcloud_quota_check = models.BooleanField(db_column='last_nextcloud_quota_check')

  #totale_quota = models.IntegerField(db_column='totale_quota',default=0)
  #last_totale_quota = models.IntegerField(db_column='last_totale_quota',default=0)

  warning_mail_quota = models.IntegerField(db_column='warning_mail_quota',default=0)
  warning_mail_quota_check = models.BooleanField(db_column='warning_mail_quota_check',default=False)
  warning_mail_quota_send = models.BooleanField(db_column='warning_mail_quota_send',default=False)

  date_start_contract = models.DateField(db_column='date_start_contract')
  date_expire_contract = models.DateField(db_column='date_expire_contract')
  date_expire_registrar = models.DateField(db_column='date_expire_registrar')

  nota = models.TextField(db_column='nota')

  enabled = models.BooleanField(db_column='enabled', default=1)
  tobedeleted = models.BooleanField(db_column='tobedeleted', default=1)

  mail = models.BooleanField(db_column='mail', default=1)
  pop3 = models.BooleanField(db_column='pop3', default=0)
  imap = models.BooleanField(db_column='imap', default=1)
  smtp = models.BooleanField(db_column='smtp', default=0)

  smtp_relay = models.BooleanField(db_column='smtp_relay', default=0)
  
  webmail =   models.BooleanField(db_column='webmail', default=False)
  nextcloud = models.BooleanField(db_column='nextcloud',default=False)
  sms =       models.BooleanField(db_column='sms',default=False)
  sendafile = models.BooleanField(db_column='sendafile',default=False)
  nextcloud_pwd = models.BooleanField(db_column='nextcloud_pwd',default=True) 
  lmtp      = models.BooleanField(db_column='lmtp',default=0)
  sieve     = models.BooleanField(db_column='sieve',default=1)
  antivirus = models.BooleanField(db_column='antivirus',default=1)
  spf = models.BooleanField(db_column='spf',default=1)
  blacklist = models.BooleanField(db_column='blacklist',default=1)

  ftp = models.BooleanField(db_column='ftp',default=False)
  sql = models.BooleanField(db_column='sql',default=False)
  http = models.BooleanField(db_column='http',default=False)
  proxy = models.BooleanField(db_column='proxy',default=False)
  sendafile = models.BooleanField(db_column='sendafile',default=False)

  registrar = models.ForeignKey('registrar.Registrar',db_column='registrar',on_delete=models.PROTECT)
  
  # owner = models.ForeignKey('contatti.Contatti',db_column='owner',on_delete=models.PROTECT)

  renew_authorized = models.BooleanField(db_column='renew_authorized')
  rinnovo_automatico = models.BooleanField(db_column='rinnovo_automatico')
  renew_code = models.CharField(db_column='renew_code',max_length=36)
  payment_status = models.BooleanField(db_column='payment_status')
  payment_value = models.DecimalField(db_column='payment_value',max_digits=8,decimal_places=2)
  payment_exclusion = models.BooleanField(db_column='payment_exclusion')

  # ma il livello, non dovrebbe essere a livello gestione_utente?
  target = models.ForeignKey('target.Target',db_column='target',on_delete=models.PROTECT)

  last_causale = models.ForeignKey('causali.Causale',db_column='last_causale',on_delete=models.PROTECT)

  # consente l'editing del record.
  edit = models.BooleanField(db_column='edit',default=0)

  # per le attivita' via mqtt
  checkstatus = models.BooleanField(db_column='checkstatus',default=False)


  class Meta:
    ordering = ['id']
    db_table = 'domini'
    managed  = False

'''
vorrei migrare tutte le voci interne all'applicazione in italiano
'''

#class Domain(Domini):
#  def __init__(self,args,**kwargs):
#    super().__init__(args,**kwargs)

#  class Meta:
#    ordering = ['id']
#    db_table = 'domini'
#    managed  = False

class DominiServizi(models.Model):
  id = models.IntegerField(db_column='id',primary_key=True)
  nome = models.CharField(db_column='nome',max_length=30)
  classe = models.CharField(db_column='classe',max_length=10)
  enabled = models.BooleanField(db_column='enabled')
  class Meta:
    ordering = ['id']
    db_table = 'domini_servizi'
    managed  = False

class DominiServiziAssegnati(models.Model):
  id = models.IntegerField(db_column='id',primary_key=True)
  servizio = models.ForeignKey('DominiServizi',db_column='servizio',on_delete=models.CASCADE)
  dominio = models.ForeignKey('Domini',db_column='dominio',on_delete=models.CASCADE)
  attivazione= models.DateField(db_column='attivazione')
  disattivazione = models.DateField(db_column='disattivazione')
  nota = models.TextField(db_column='nota')
  class Meta:
    ordering = ['id']
    db_table = 'domini_servizi_assegnati'
    managed  = False

class Storia(models.Model):
  id = models.IntegerField(db_column='id',primary_key=True)
  dominio = models.ForeignKey('Domini',db_column='dominio',on_delete=models.CASCADE)
  causale = models.ForeignKey('causali.Causale',db_column='causale',on_delete=models.CASCADE)
  utente = models.ForeignKey('utenti.User',db_column='utente',on_delete=models.CASCADE)
  data = models.IntegerField(db_column='data')
  nota = models.TextField(db_column='nota')
  class Meta:
    ordering = ['id']
    db_table = 'domini_storia'
    managed  = False
