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

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

class PackageModel(PrimaryModel):
    __tablename__ = 'evm_package'

    app = db.Column(db.Integer, nullable = False)
    app_version = db.Column(db.String(200), nullable = False)
    package_info = db.Column(db.String(20), nullable = False)
    file_path = db.Column(db.String(200), nullable = False)
    source = db.Column(db.Integer, nullable = False)
    user_agent = db.Column(db.String(200), nullable = False)
    download_url = 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, app, app_version, package_info, file_path, source, user_agent, download_url, ip, geo_location, operator):
        self.app = app
        self.app_version = app_version
        self.package_info = package_info
        self.file_path = file_path
        self.source = source
        self.user_agent = user_agent
        self.download_url = download_url
        self.ip = ip
        self.geo_location = geo_location
        self.operator = operator

    def __repr__(self):
        return '<PackageModel %r>' % (self.app)

    def to_json(self):
        return {
            'app_version': self.app_version,
            'package_info': self.package_info,
            'file_path': self.file_path,
            'source': self.source,
            'user_agent': self.user_agent,
            'download_url': self.download_url,
            'ip': self.ip,
            'geo_location': self.geo_location,
            'operator': self.operator,
        }


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

    page = fields.Integer(required=False)
    pageSize = fields.Integer(required=False)
    app = ma.auto_field()
    app_version = ma.auto_field()
    package_info = ma.auto_field()
    file_path = ma.auto_field()
    source = ma.auto_field()
    user_agent = ma.auto_field()
    download_url = ma.auto_field()
    ip = ma.auto_field()
    geo_location = ma.auto_field()
    operator = ma.auto_field()

getListPackageSchema = GetListPackageSchema()
getListPackagesSchema = GetListPackageSchema(many=True)

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

    app = ma.auto_field()
    app_version = ma.auto_field()
    package_info = ma.auto_field()
    file_path = ma.auto_field()
    source = ma.auto_field()
    user_agent = ma.auto_field()
    download_url = ma.auto_field()
    ip = ma.auto_field()
    geo_location = ma.auto_field()
    operator = ma.auto_field()

getPackageSchema = GetPackageSchema()
