from sqlalchemy import Boolean, Column, Integer, String, DateTime,ForeignKey
from sqlalchemy.ext.declarative import as_declarative
from datetime import datetime

@as_declarative()
class Base:
    id = Column(Integer, primary_key=True, index=True)
    create_time = Column(DateTime, default=datetime.now)
    update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now)

class User(Base):
    __tablename__ = "user"

    email = Column(String(50), unique=True, index=True, nullable=False)
    first_name = Column(String(100))
    last_name = Column(String(100))
    hashed_password = Column(String(200), nullable=False)
    is_active = Column(Boolean, default=True)
    is_superuser = Column(Boolean, default=False)

class AppSetting(Base):
    __tablename__ = "appsetting"

    appid = Column(String(100), index=True, nullable=False)
    setting = Column(String(1000))   

class Kintone(Base):
    __tablename__ = "kintone"

    type = Column(Integer, index=True, nullable=False)
    name = Column(String(100), nullable=False)   
    desc = Column(String)
    content = Column(String)

class Action(Base):
    __tablename__ = "action"

    name = Column(String(100), index=True, nullable=False)
    title = Column(String(200))   
    subtitle = Column(String(500))   
    outputpoints = Column(String)
    property =  Column(String)

class Flow(Base):
    __tablename__ = "flow"

    flowid = Column(String(100), index=True, nullable=False)
    appid = Column(String(100), index=True, nullable=False)
    eventid = Column(String(100), index=True, nullable=False)
    domainid = Column(Integer,ForeignKey("domain.id"))
    name = Column(String(200))   
    content = Column(String)  

class Tenant(Base):
    __tablename__ = "tenant"

    tenantid =  Column(String(100), index=True, nullable=False)
    name = Column(String(200)) 
    licence  = Column(String(200)) 
    startdate = Column(DateTime)
    enddate = Column(DateTime) 

class Domain(Base):
    __tablename__ = "domain"

    tenantid =  Column(String(100), index=True, nullable=False)
    name = Column(String(100), nullable=False)   
    url = Column(String(200), nullable=False)
    kintoneuser = Column(String(100), nullable=False)
    kintonepwd = Column(String(100), nullable=False)


class UserDomain(Base):
    __tablename__ = "userdomain"

    userid = Column(Integer,ForeignKey("user.id"))
    domainid = Column(Integer,ForeignKey("domain.id"))
    active = Column(Boolean, default=False)

class Event(Base):
    __tablename__ = "event"

    category = Column(String(100), nullable=False)
    type = Column(String(100), nullable=False)
    eventid= Column(String(100), nullable=False)
    function = Column(String(500), nullable=False)   
    mobile = Column(Boolean, default=False)
    eventgroup = Column(Boolean, default=False)

class EventAction(Base):
    __tablename__ = "eventaction"

    eventid = Column(Integer,ForeignKey("event.id"))
    actionid = Column(Integer,ForeignKey("action.id"))


class ErrorLog(Base):
       __tablename__ = "errorlog"

       title = Column(String(50))
       location = Column(String(500))
       content = Column(String(5000))

class KintoneFormat(Base):
      __tablename__ = "kintoneformat"

      name = Column(String(50))
      startrow =Column(Integer)
      startcolumn =Column(Integer)
      typecolumn =Column(Integer)
      codecolumn =Column(Integer)
      field = Column(String(5000))
      trueformat = Column(String(10))