import redis
import json
import discord
import os

REDIS = redis.Redis(host="localhost", port=6379, decode_responses=True)

DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")

client = discord.Client(intents=discord.Intents.default())


CHANNEL_MAP = {
    "storm-emergencies": 1522099044669984850,
    "tornado": 1522099061459521656,
    "severe-storms": 1522099083706105947,
    "flood": 1522099101930360862,
    "advisories": 1522099154804019220,
    "other-weather-alerts": 1522099168020271155,
}


async def publish_discord(alert, channel_name: str):
    """
    Sends alert to correct Discord channel.
    """

    channel_id = CHANNEL_MAP.get(channel_name, CHANNEL_MAP["other-weather-alerts"])
    channel = client.get_channel(channel_id)

    if channel is None:
        return

    embed = discord.Embed(
        title=f"🌩 {alert.event}",
        description=alert.headline or "No description",
        color=0x3498db,
    )

    embed.add_field(name="Severity", value=alert.tier, inline=True)
    embed.add_field(name="Category", value=alert.category, inline=True)

    await channel.send(embed=embed)
