# -*- coding: utf-8 -*-

from sqlalchemy.sql.expression import func

from nitrate import db
from nitrate.auth.models import User


class CaseStatus(db.Model):
    __tablename__ = 'nitrate_case_statuss'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(200), nullable=False)
    description = db.Column(db.Text, nullable=False, default='')

    created_on = db.Column(db.DateTime, nullable=False)
    updated_on = db.Column(db.DateTime, nullable=False)


class CaseCategory(db.Model):
    __tablename__ = 'nitrate_case_categories'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(200), nullable=False, unique=True)
    description = db.Column(db.Text, nullable=False)

    created_on = db.Column(db.DateTime, nullable=False)
    updated_on = db.Column(db.DateTime, nullable=False)


class Case(db.Model):
    __tablename__ = 'nitrate_cases'

    id = db.Column(db.Integer, primary_key=True)
    summary = db.Column(db.String(200), nullable=False)
    is_automated = db.Column(db.Boolean, nullable=False, default=False)
    script = db.Column(db.Text, nullable=False)
    arguments = db.Column(db.String(200), nullable=False, default='')
    extra_link = db.Column(db.String(255))
    alias = db.Column(db.String(200), nullable=False)
    estimated_time = db.Column(db.String(100), nullable=False)
    notes = db.Column(db.Text, nullable=False, default='')

    status_id = db.Column(db.Integer, db.ForeignKey('nitrate_case_statuss.id'),
                          nullable=False)
    status = db.relationship('CaseStatus',
                              backref=db.backref('cases', lazy='dynamic'))

    category_id = db.Column(db.Integer,
                            db.ForeignKey('nitrate_case_categories.id'),
                            nullable=False)
    category = db.relationship('CaseCategory',
                              backref=db.backref('cases', lazy='dynamic'))

    author_id = db.Column(db.Integer, db.ForeignKey('nitrate_users.id'),
                          nullable=False)
    author = db.relationship('User',
                              backref=db.backref('my_cases', lazy='dynamic'))

    default_tester_id = db.Column(db.Integer,
                                  db.ForeignKey('nitrate_users.id'),
                                  nullable=False)
    default_tester = db.relationship('User',
                                     backref=db.backref('cases_to_test',
                                                        lazy='dynamic'))

    reviewer_id = db.Column(db.Integer, db.ForeignKey('nitrate_users.id'),
                            nullable=False)
    reviewer = db.relationship('User',
                               backref=db.backref('cases_to_review',
                                                  lazy='dynamic'))

    created_on = db.Column(db.DateTime, nullable=False)
    updated_on = db.Column(db.DateTime, nullable=False)


class CaseText(db.Model):
    __tablename__ = 'nitrate_case_texts'

    id = db.Column(db.Integer, primary_key=True)
    text_version = db.Column(db.Integer, nullable=False)

    case_id = db.Column(db.Integer, db.ForeignKey(Case.id), nullable=False)
    case = db.relationship('Case', backref=db.backref('texts', lazy='dynamic'))

    author_id = db.Column(db.Integer, db.ForeignKey(User.id), nullable=False)
    author = db.relationship('User', backref=db.backref('my_case_texts',
                                                        lazy='dynamic'))

    action = db.Column(db.Text, nullable=False, default='')
    effect = db.Column(db.Text, nullable=False, default='')
    setup = db.Column(db.Text, nullable=False, default='')
    breakdown = db.Column(db.Text, nullable=False, default='')

    action_checksum = db.Column(db.String(32))
    effect_checksum = db.Column(db.String(32))
    setup_checksum = db.Column(db.String(32))
    breakdown_checksum = db.Column(db.String(32))

    created_on = db.Column(db.DateTime, nullable=False)
    updated_on = db.Column(db.DateTime, nullable=False)
