from app import db 
from datetime import datetime as dt, timezone as tz

# Database 1 Models
class User(db.Model):
    __tablename__ = 'users'
    
    user_id = db.Column(db.Integer,primary_key=True, autoincrement=True)
    user_name = db.Column(db.String(64), unique=True, nullable=False)
    user_password = db.Column(db.String(255), nullable=False)
    user_displayname = db.Column(db.String(128))
    user_age = db.Column(db.Integer)
    created_at = db.Column(db.DateTime, default=dt.now(tz.utc))
    updated_at = db.Column(db.DateTime, default=dt.now(tz.utc), onupdate=dt.now(tz.utc))

class Chat(db.Model):
    __tablename__ = 'chats'
    
    chat_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    chat_name = db.Column(db.String(128))
    creation_date = db.Column(db.DateTime, default=dt.now(tz.utc))
    is_group = db.Column(db.Boolean, default=False)

class ChatMember(db.Model):
    __tablename__ = 'chat_members'
    
    chat_id = db.Column(db.Integer, db.ForeignKey('chats.chat_id', ondelete='CASCADE'), primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.user_id', ondelete='CASCADE'), primary_key=True)
    user_role = db.Column(db.String(32), default='member')  # roles: owner, member
    added_at = db.Column(db.DateTime, default=dt.now(tz.utc))

class Message(db.Model):
    __tablename__ = 'messages'
    
    message_id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    chat_id = db.Column(db.Integer, db.ForeignKey('chats.chat_id', ondelete='CASCADE'), nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'), nullable=False)
    key_id = db.Column(db.Integer, nullable=False)
    enc_content = db.Column(db.Text, nullable=False)
    sent_at = db.Column(db.DateTime, default=dt.now(tz.utc))
    signature = db.Column(db.String(128))
    is_deleted = db.Column(db.Boolean, default=False)

# Database 2 Models (Keys database)
class UserKey(db.Model):
    __bind_key__ = 'keys_db'
    __tablename__ = 'user_keys'
    
    user_name = db.Column(db.String(64), unique=True, nullable=False, primary_key=True)
    user_id = db.Column(db.Integer, unique=True, nullable=False)
    protected_enc_privatekey = db.Column(db.Text, unique=True, nullable=False)
    enc_publickey = db.Column(db.Text, unique=True, nullable=False)
    protected_sign_privatekey = db.Column(db.Text, unique=True, nullable=False)
    sign_publickey = db.Column(db.Text, unique=True, nullable=False)
    created_at = db.Column(db.DateTime, default=dt.now(tz.utc))
    updated_at = db.Column(db.DateTime, default=dt.now(tz.utc), onupdate=dt.now(tz.utc))

class ChatKey(db.Model):
    __bind_key__ = 'keys_db'
    __tablename__ = 'chat_keys'
    
    user_name = db.Column(db.String(64), db.ForeignKey('user_keys.user_name'), nullable=False, primary_key=True)
    user_id = db.Column(db.Integer, nullable=False, primary_key=True)
    chat_id = db.Column(db.Integer, nullable=False, primary_key=True)
    key_id = db.Column(db.Integer, nullable=False, primary_key=True)
    encrypted_symetrickey = db.Column(db.Text, unique=True, nullable=False)
    created_at = db.Column(db.DateTime, default=dt.now(tz.utc))
    updated_at = db.Column(db.DateTime, default=dt.now(tz.utc), onupdate=dt.now(tz.utc))
