import discord
from discord.ext import commands
from discord import app_commands

from config_manager import ConfigManager
import cogs.views as views
from enum import Enum

import storybot_exceptions
import re

# Listens for DMs to add to the story
class privacy_cog(commands.GroupCog, group_name='privacy'):
    discord_generic = None
    
    def __init__(self, bot: discord.Client):
        self.bot = bot
        self.config_manager : ConfigManager = bot.config_manager
        self.file_manager = bot.file_manager
        self.dm_listener = bot.cogs["dm_listener"]
            
    @app_commands.checks.cooldown(1, 60*60*24) # Can only happen once a day per user
    @app_commands.command(name="request_my_data")
    async def privacy_request_data(self, interaction: discord.Interaction):
        """Returns all the data this bot has stored about you. Can only be run once every 24 hours.

        Args:
            interaction (discord.Interaction): the default interaction
        """
        await interaction.response.defer(ephemeral=True)
        
        await self.discord_generic.reply_to_message(reply_to=interaction, content=await self.file_manager.gdpr_request(interaction.user.id), view = views.LinkToSupportServer(), ephemeral=True)
        
                
    @app_commands.checks.cooldown(1, 60*60*24) # Can only happen once a day per user
    @app_commands.command(name="delete_my_data")
    async def privacy_delete_data(self, interaction: discord.Interaction):
        """Deletes all your data stored by StoryBot. Can only be run once every 24 hours.

        Args:
            interaction (discord.Interaction): the default interaction
        """
        
        if not await self.discord_generic.confirm(respond_to=interaction, embed = await self.discord_generic.create_embed(content="Are you sure you want to want to delete all of your data? You will be removed as an author in every server you are in!\n\nNote: your bans will *not* be deleted!", title="Warning", color=discord.Color.brand_red())): return
        
        if not await self.discord_generic.confirm(respond_to=interaction, embed = await self.discord_generic.create_embed(content="Are you ABSOLUTELY SURE? **THIS PROCESS CANNOT BE REVERSED!!!**", title="⚠️⚠️ WARNING!! ⚠️⚠️", color=discord.Color.brand_red())): return
        
        user_active_servers = (await self.file_manager.get_user_active_guilds(interaction.user.id)) + (await self.file_manager.get_user_paused_guilds(interaction.user.id))
        
        for guild_id in user_active_servers:
            await self.dm_listener.remove_user(guild_id, user_id=interaction.user.id, throw_error_if_not_an_author=False, use_skip_logic=True)
        
        await self.file_manager.gdpr_delete(user_id=interaction.user.id)
        
        await self.discord_generic.reply_to_message(reply_to=interaction, content="All of your data (except bans) has now been deleted! We're sorry to see you go, but we hope you enjoyed using StoryBot.\n\nNote: interacting with StoryBot again in the future may cause data to be collected again.\n\nIf you want your server's data deleted, just kick the bot. When the bot is kicked from a server, it'll automatically delete all data (and stories) it stores about that server.", ephemeral=True)
    

async def setup(bot):
    await bot.add_cog(privacy_cog(bot))

