from snek.system.service import BaseService
import jinja2 

class ChannelMessageService(BaseService):
    mapper_name = "channel_message"

    async def create(self, channel_uid, user_uid, message):
        model = await self.new()
        
        model["channel_uid"] = channel_uid
        model["user_uid"] = user_uid
        model["message"] = message
                
        context = {
            
        }
        
        
        record = model.record 
        context.update(record)
        user = await self.app.services.user.get(uid=user_uid)
        context.update(dict(
            user_uid=user['uid'],
            username=user['username'],
            user_nick=user['nick'],
            color=user['color']
        ))
        try:
            template = self.app.jinja2_env.get_template("message.html")
            model["html"] = template.render(**context)
        except Exception as ex:
            print(ex,flush=True)
        
        if await self.save(model):
            return model
        raise Exception(f"Failed to create channel message: {model.errors}.")
    
    async def to_extended_dict(self, message):
        user = await self.services.user.get(uid=message["user_uid"])
        if not user:
            return {}
        return {
            "uid": message["uid"],
            "color": user['color'],
            "user_uid": message["user_uid"],
            "channel_uid": message["channel_uid"],
            "user_nick": user['nick'],
            "message": message["message"],
            "created_at": message["created_at"],
            "html": message['html'],
            "username": user['username'] 
        }

    async def offset(self, channel_uid, page=0, timestamp = None, page_size=30):
        results = []
        offset = page * page_size 
        try:
            if timestamp:
                async for model in self.query("SELECT * FROM channel_message WHERE channel_uid=:channel_uid AND created_at < :timestamp ORDER BY created_at DESC LIMIT :page_size OFFSET :offset",dict(channel_uid=channel_uid, page_size=page_size, offset=offset, timestamp=timestamp)):
                    results.append(model)
            elif page > 0:
                async for model in self.query("SELECT * FROM channel_message WHERE channel_uid=:channel_uid WHERE created_at < :timestamp ORDER BY created_at DESC LIMIT :page_size",dict(channel_uid=channel_uid, page_size=page_size, offset=offset,timestamp=timestamp )):
                    results.append(model)
            else: 
                async for model in self.query("SELECT * FROM channel_message WHERE channel_uid=:channel_uid ORDER BY created_at DESC LIMIT :page_size OFFSET :offset",dict(channel_uid=channel_uid, page_size=page_size, offset=offset)):
                    results.append(model)

        except: 
            pass
        results.sort(key=lambda x: x['created_at'])
        return results 
