
import os
import datetime
from flask_user import UserMixin
from slapos.certificate_authority.web.start_web import db
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, validators

CERT_STATUS_VALIDATED = 'validated'
CERT_STATUS_REVOKED = 'invalidated'
CERT_STATUS_REJECTED = 'rejected'
CERT_STATUS_PENDING = 'pending'

class User(db.Model, UserMixin):
  id = db.Column(db.Integer, primary_key=True)

  # User authentication information
  username = db.Column(db.String(50), nullable=False, unique=True)
  password = db.Column(db.String(255), nullable=False, server_default='')

  # User email information
  email = db.Column(db.String(255), nullable=False, unique=True)
  confirmed_at = db.Column(db.DateTime())

  # User information
  active = db.Column('is_active', db.Boolean(), nullable=False, server_default='0')
  first_name = db.Column(db.String(100), nullable=False, server_default='')
  last_name = db.Column(db.String(100), nullable=False, server_default='')


class Certificate(db.Model):
  """
    This table is used to store some informations about certificate
  """
  __tablename__ = 'certificate'
  id = db.Column(db.Integer, primary_key=True)
  name = db.Column(db.String(80), unique=True)
  serial = db.Column(db.String(50), unique=True)
  filename = db.Column(db.String(80), unique=True)
  common_name = db.Column(db.String(50), unique=False)
  expire_after = db.Column(db.DateTime)
  start_before = db.Column(db.DateTime)
  # status = validated or revoked
  status = db.Column(db.String(20), unique=False, server_default=CERT_STATUS_VALIDATED)
  comment = db.Text(db.String())
  # When renew, this can say what was the previous certificate
  old_serial = db.Column(db.String(50), unique=False, server_default='')

  def __repr__(self):
    return '<CertificateMap %r>' % (self.serial)

class Revocation(db.Model):
  """
    This table store certificate revocation request from users
  """
  __tablename__ = 'revoke_request'
  id = db.Column(db.Integer, primary_key=True)
  serial = db.Column(db.String(50), unique=False)
  filename = db.Column(db.String(80), unique=False)
  common_name = db.Column(db.String(50), unique=False)
  reason = db.Column(db.String(200), unique=False)
  request_date = db.Column(db.DateTime)
  status = db.Column(db.String(20), unique=False, server_default=CERT_STATUS_PENDING)

  def __repr__(self):
      return '<CertificateMap %r>' % (self.serial)


class DisabledStringField(StringField):
  def __call__(self, *args, **kwargs):
    kwargs.setdefault('disabled', True)
    return super(ReadonlyStringField, self).__call__(*args, **kwargs)

# Define the User profile form
class UserProfileForm(FlaskForm):
  username = StringField('Username')
  first_name = StringField('First name', validators=[
      validators.DataRequired('First name is required')])
  last_name = StringField('Last name', validators=[
      validators.DataRequired('Last name is required')])
  email = StringField('Email', validators=[
      validators.DataRequired('Email is required')])
  submit = SubmitField('Save')

