# -*- coding: utf-8 -*-
from core.utils.db import db
from core.utils.packages.session import BackupOnDeleteMixin
from sqlalchemy.orm import backref
from sqlalchemy import BigInteger
from datetime import datetime, timezone 
from sqlalchemy import desc, func, String, case, literal_column  
from sqlalchemy.ext.hybrid import hybrid_property 
from sqlalchemy.sql.expression import null 

class Propertytype(db.Model, BackupOnDeleteMixin):
    __tablename__ = 'propertytype'
    id = db.Column(db.Integer, primary_key=True)
    nameEN = db.Column(db.String(255))
    nameES = db.Column(db.String(255))
    parent_id = db.Column(db.Integer, db.ForeignKey('propertytype.id', ondelete='SET NULL'), nullable=True)
    parent = db.relationship('Propertytype', remote_side=[id], backref=db.backref('parent_childs'))
    createdby_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    modifiedby_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    createdby = db.relationship('User', backref='created_propertytype', foreign_keys=[createdby_id])
    modifiedby = db.relationship('User', backref='modified_propertytype', foreign_keys=[modifiedby_id])
    created_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc))
    updated_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))


    @hybrid_property
    def full_repr(self):
        result = f'''{self.nameEN if self.nameEN is not None else ''}'''
        return result.strip()

    @full_repr.expression
    def full_repr(cls):
        return (
            literal_column("") + literal_column("''")  + func.coalesce(cls.nameEN, literal_column("''"))  + literal_column("''")
        )

    def __repr__(self) -> str:
        return self.full_repr 
    __default_ordering__ = [desc('nameEN')]

    __table_args__ = {
        "mysql_charset": "utf8mb4",
        "mysql_collate": "utf8mb4_unicode_ci",
    }


def get_fields():
    fields = {'nameEN': {'id': 161, 'type': 'String', 'name': 'nameEN', 'maxlength': None, 'connected_table': None, 'label': 'Nombre (EN)', 'input': 'text', 'sort': None, 'required': False, 'hidden': False, 'publicBlob': False, 'default_value': None, 'select_options': None, 'extraclass': None, 'allowHtml': False, 'hasManyValues': False, 'readOnly': False, 'calculate_file': None, 'calculate_function': None, 'helper': None}, 'nameES': {'id': 162, 'type': 'String', 'name': 'nameES', 'maxlength': None, 'connected_table': None, 'label': 'Nombre (ES)', 'input': 'text', 'sort': None, 'required': False, 'hidden': False, 'publicBlob': False, 'default_value': None, 'select_options': None, 'extraclass': None, 'allowHtml': False, 'hasManyValues': False, 'readOnly': False, 'calculate_file': None, 'calculate_function': None, 'helper': None}, 'parent': {'id': 163, 'type': 'selfParent', 'name': 'parent', 'maxlength': None, 'connected_table': 12, 'label': 'Parent', 'input': 'connected_table', 'sort': None, 'required': False, 'hidden': False, 'publicBlob': False, 'default_value': None, 'select_options': None, 'extraclass': None, 'allowHtml': False, 'hasManyValues': False, 'readOnly': False, 'calculate_file': None, 'calculate_function': None, 'helper': None}}
    return fields


def get_permissions():
    permissions = {}
    return permissions
