import meta
from sqlalchemy import types, or_
from sqlalchemy.schema import Column, ForeignKey
from sqlalchemy.orm import relationship
from pin import ModeratorPin


class Conference(meta.Base):
    """The conferences"""
    __tablename__ = 'conferences'
    
    id = Column(types.String(36), primary_key=True)
    created = Column(types.DateTime, nullable=False)
    name = Column(types.Unicode(255), nullable=False)
    profile = Column(types.Unicode(255), nullable=False)
    ended = Column(types.DateTime)
    actions = relationship("ConferenceAction", backref='Conference', cascade='all, delete, delete-orphan')
    owner = Column(types.String(8), ForeignKey(ModeratorPin.pin), nullable=False)
    recording = Column(types.Unicode(255), nullable=True)
    
    def __init__(self, id, created, conf_name, profile):
        """Creates a new conference"""
        self.id = id
        self.created = created
        self.name = conf_name
        self.profile = profile

class ConferenceAction(meta.Base):
    """Log all actions that happen on a certain conference"""
    __tablename__ = 'conference_actions'
    
    id = Column(types.Integer, primary_key=True)
    action = Column(types.Unicode(255), nullable=False)
    caller_id_name = Column(types.Unicode(255))
    caller_id_number = Column(types.Unicode(255))
    conference_size = Column(types.Integer)
    conference = Column(types.String(36), ForeignKey(Conference.id), nullable=False)
    member_type = Column(types.Unicode(255))
    timestamp = Column(types.DateTime, nullable=False)
    
    def __init__(self, action, caller_id_name, caller_id_number, conference_size, member_type, timestamp, conference):
        """Create a new action"""
        self.action = action
        self.caller_id_name = caller_id_name
        self.caller_id_number = caller_id_number
        self.conference_size = conference_size
        self.member_type = member_type
        self.timestamp = timestamp
        self.conference = conference
    