import discord
import json 

from datetime import datetime
from discord.ext import commands 
from utils.aiohttp_wrap import aio_get_json


class Stonks(commands.Cog):
    URL = "https://finnhub.io/api/v1/quote"
    TTL = 60 * 15
    def __init__(self, bot):
        self.bot = bot
        self.session = bot.aio_session
        self.redis_client = bot.redis_client
        # self.headers = {'X-Finnhub-Token': bot.api_keys["stonks"]}
        with open('data/apikeys.json') as f:
            self.api_key = json.load(f)["stonks"]
        self.headers = {'X-Finnhub-Token': self.api_key}
    
    @commands.command(name="stonk", aliases=["stonks", "stock", "stocks"])
    async def stonks(self, ctx: commands.Context, *, symbol: str):
        symbol = symbol.upper()
        params = {"symbol": symbol}

        redis_key = f"stonks:{symbol}"
        if await self.redis_client.exists(redis_key):
            resp = json.loads(await self.redis_client.get(redis_key))
        else:
            resp = await aio_get_json(self.session, self.URL, headers=self.headers, params=params)

            if resp is None:
                return await ctx.error("API Error", description="There was an issue with the stocks API, try again later")
            
            if resp['t'] == 0:
                return await ctx.error("Stock error", description=f"Couldn't find any stock information for `{symbol}`")
            await self.redis_client.set(redis_key, json.dumps(resp), ex=self.TTL)
        
        em = discord.Embed(color=discord.Color.blurple())
        em.set_author(name=symbol, icon_url="https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/240/twitter/259/chart-increasing_1f4c8.png")
        em.add_field(name="Current Price", value=f"${resp['c']:.2f}")
        em.add_field(name="Previous Close", value=f"${resp['pc']:.2f}")
        em.add_field(name="% Change Today", value=f"{(resp['c'] - resp['pc'])/resp['pc']:.2%}")

        em.set_footer()
        em.timestamp = datetime.fromtimestamp(resp['t'])

        await ctx.send(embed=em)


def setup(bot):
    bot.add_cog(Stonks(bot))

@@ -84,17 +84,58 @@
 commands
- 
+%0Afrom discord.utils import escape_markdown
 %0Afrom ut
@@ -245,16 +245,77 @@
 /quote%22%0A
+    PROFILE_URL = %22https://finnhub.io/api/v1/stock/profile2%22%0A
     TTL 
@@ -828,16 +828,162 @@
 : str):%0A
+        if len(symbol) %3E 5:%0A            return await ctx.error(%22Stock error%22, description=f%22Invalid ticker symbol: %60%7Bescape_markdown(symbol)%7D%60%22)%0A%0A
         
@@ -1637,16 +1637,201 @@
 bol%7D%60%22)%0A
+            %0A            company_profile = await aio_get_json(self.session, self.PROFILE_URL, params=params, headers=self.headers)%0A            resp%5B%22company_profile%22%5D = company_profile%0A
         
@@ -1998,22 +1998,63 @@
 or(name=
+f%22%7Bresp%5B'company_profile'%5D%5B'name'%5D%7D (%7B
 symbol
+%7D)%22
 , icon_u
@@ -2060,118 +2060,78 @@
 url=
-%22https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/240/twitter/259/chart-increasing_1f4c8.png%22
+resp%5B%22company_profile%22%5D%5B%22logo%22%5D, url=resp%5B%22company_profile%22%5D%5B%22weburl%22%5D
 )%0A  
@@ -2187,24 +2187,25 @@
 $%7Bresp%5B'c'%5D:
+,
 .2f%7D%22)%0A     
@@ -2264,16 +2264,17 @@
 p%5B'pc'%5D:
+,
 .2f%7D%22)%0A 

add more company info to stonks