from chloroform import db

class Choice(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    text = db.Column(db.Text)
    question_id = db.Column(db.Integer, db.ForeignKey('question.id'))
    choice_template_id = db.Column(db.Integer, db.ForeignKey('choice_template.id'))


    def __init__(self, text):
        self.text = text



class ChoiceTemplate(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    template = db.Column(db.Text)
    version = db.Column(db.Integer, default=0)
    question_template_id = db.Column(db.Integer, db.ForeignKey('question_template.id'))
    choices = db.relationship('Choice', backref='choice_template')

    def __init__(self, template):
        self.template = template



class Client(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Text, unique=True)
    forms = db.relationship('Form', backref='client')

    def __init__(self, name):
        self.name = name




class Form(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.Text)
    version = db.Column(db.Integer, default = 0)
    client_id = db.Column(db.Integer, db.ForeignKey('client.id'))
    question_group_id = db.Column(db.Integer, db.ForeignKey('question_group.id'))
    retail_chain_id = db.Column(db.Integer, db.ForeignKey('retail_chain.id'))


    def __init__(self, title):
        self.title = title




class Madlib(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    word = db.Column(db.Text)
    word_type = db.Column(db.Text)
    version = db.Column(db.Integer, default = 0)
    client_id = db.Column(db.Integer, db.ForeignKey('client.id'))
    question_id = db.Column(db.Integer, db.ForeignKey('question.id'))

    def __init__(self, word, word_type):
        self.word = word
        self.word_type = word_type

    


class QuestionGroup(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    version = db.Column(db.Integer, default = 0)
    parent_id = db.Column(db.Integer, db.ForeignKey('question_group.id'))
    question_group_template_id = db.Column(db.Integer, db.ForeignKey('question_group_template.id'))
    forms = db.relationship('Form', backref='question_group')

    def __init__(self):
        pass



class QuestionGroupTemplate(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    template = db.Column(db.Text)
    version = db.Column(db.Integer, default = 0)

    def __init__(self, template):
        self.template = template



class Question(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    text = db.Column(db.Text)
    question_group_id = db.Column(db.Integer, db.ForeignKey('question_group.id'))
    choices = db.relationship('Choice', backref='question')

    def __init__(self, text):
        self.text = text



class QuestionTemplate(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    template = db.Column(db.Text)
    version = db.Column(db.Integer, default = 0)
    question_group_template_id = db.Column(db.Integer, db.ForeignKey('question_group_template.id'))
    choice_templates = db.relationship('ChoiceTemplate')

    def __init__(self, template):
        self.template = template



class RetailChain(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Text)
    forms = db.relationship('Form', backref='retail_chain')


    def __init__(self, name):
        self.name = name



@@ -720,25 +720,24 @@
  template%0A%0A%0A
-%0A
 class Client
@@ -1257,28 +1257,28 @@
 '))%0A    
-retail_chain
+form_context
 _id = db
@@ -1312,28 +1312,28 @@
 ignKey('
-retail_chain
+form_context
 .id'))%0A%0A
@@ -1389,26 +1389,24 @@
 e = title%0A%0A%0A
-%0A%0A
 class Madlib
@@ -1649,78 +1649,8 @@
 d'))
-%0A    question_id = db.Column(db.Integer, db.ForeignKey('question.id'))
 %0A%0A  
@@ -1753,13 +1753,9 @@
 pe%0A%0A
-    
 %0A
+
 %0A%0Acl
@@ -2392,32 +2392,359 @@
 ate = template%0A%0A
+%0Aclass QuestionMadlib(db.Model):%0A    question_id = db.Column(db.Integer, db.ForeignKey('question.id'),primary_key = True)%0A    madlib_id = db.Column(db.Integer, db.ForeignKey('madlib.id'), primary_key = True)%0A    key = db.Column(db.Text)%0A    madlib = db.relationship('Madlib')%0A%0A    def __init__(self,key):%0A        self.key = key
 %0A%0Aclass Question
@@ -2975,16 +2975,64 @@
 estion')
+%0A    madlibs = db.relationship('QuestionMadlib')
 %0A%0A    de
@@ -3078,25 +3078,24 @@
 xt = text%0A%0A%0A
-%0A
 class Questi
@@ -3486,19 +3486,19 @@
 ass 
-RetailChain
+FormContext
 (db.
@@ -3633,20 +3633,20 @@
 ef='
-retail_chain
+form_context
 ')%0A%0A

fix madlib relationship; genericize retail_client