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

from application.app import db, ma
from .base import PrimaryModel 
from marshmallow import Schema, fields, INCLUDE, EXCLUDE

class LoginModel(PrimaryModel):
    __tablename__ = 'evm_login'

    user = db.Column(db.Integer, nullable = False)
    login_at = db.Column(db.String(200), nullable = False)
    user_agent = db.Column(db.String(200), nullable = False)
    ip = db.Column(db.String(128), nullable = False)
    geo_location = db.Column(db.String(200), nullable = False)
    operator = db.Column(db.String(50), nullable = False)

    # __table_args__ = (
    #     db.Index('idx_xxx', 'xxx', mysql_using='btree'),
    # )

    def __init__(self, user, login_at, user_agent, ip, geo_location, operator):
        self.user = user
        self.login_at = login_at
        self.user_agent = user_agent
        self.ip = ip
        self.geo_location = geo_location
        self.operator = operator

    def __repr__(self):
        return '<LoginModel %r>' % (self.user)

    def to_json(self):
        return {
            'login_at': self.login_at,
            'user_agent': self.user_agent,
            'ip': self.ip,
            'geo_location': self.geo_location,
            'operator': self.operator,
        }


class PostLoginSchema(ma.SQLAlchemySchema):
    class Meta:
        # unknown = INCLUDE # 未知字段默认包含
        unknown = EXCLUDE # 未知字段默认排除
        model = LoginModel

    account = fields.String(required=True, length=64)
    password = fields.String(required=True, length=64)

postLoginSchema = PostLoginSchema()

class GetListLoginSchema(ma.SQLAlchemySchema):
    class Meta:
        # unknown = INCLUDE # 未知字段默认包含
        unknown = EXCLUDE # 未知字段默认排除
        model = LoginModel

    page = fields.Integer(required=False)
    pageSize = fields.Integer(required=False)
    user = ma.auto_field()
    login_at = ma.auto_field()
    user_agent = ma.auto_field()
    ip = ma.auto_field()
    geo_location = ma.auto_field()
    operator = ma.auto_field()

getListLoginSchema = GetListLoginSchema()
getListLoginsSchema = GetListLoginSchema(many=True)

class GetLoginSchema(ma.SQLAlchemySchema):
    class Meta:
        # unknown = INCLUDE # 未知字段默认包含
        unknown = EXCLUDE # 未知字段默认排除
        model = LoginModel

    user = ma.auto_field()
    login_at = ma.auto_field()
    user_agent = ma.auto_field()
    ip = ma.auto_field()
    geo_location = ma.auto_field()
    operator = ma.auto_field()

getLoginSchema = GetLoginSchema()
