# Command: Create Slot
@bot.tree.command(name="create", description="Create a new slot", guild=discord.Object(id=GUILD_ID))
@app_commands.describe(
    member="The member to assign the slot to",
    duration="Duration of the slot (e.g., 7d for 7 days, 2m for 2 months)",
    category="Category of the slot (e.g., 1, 2, 3)",
    slotname="Name of the slot channel"
)
async def slash_create(interaction: discord.Interaction, member: discord.Member, duration: str, category: str, slotname: str):
    await interaction.response.defer(ephemeral=True)
    
    # Fetch Roles
    staff_role = interaction.guild.get_role(STAFF_ROLE_ID)
    premium_role = interaction.guild.get_role(PREMIUM_ROLE_ID)
    
    # Check if user is staff or creating for themselves
    is_staff = staff_role in interaction.user.roles if staff_role else False
    if not is_staff and interaction.user != member:
        await interaction.followup.send("❌ You can only create slots for yourself unless you're a staff member.")
        return

    # Validate Category
    category_id = get_category_by_name(category)
    if not category_id:
        await interaction.followup.send(f"❌ Invalid category. Please choose one of: {', '.join(CATEGORY_IDS.keys())}.")
        return
    category_obj = interaction.guild.get_channel(category_id)
    if not isinstance(category_obj, discord.CategoryChannel):
        await interaction.followup.send("❌ Specified category does not exist.")
        return

    # Parse Duration
    try:
        time_value = int(duration[:-1])
        time_unit = duration[-1].lower()
        if time_unit == 'd':
            delta = datetime.timedelta(days=time_value)
            duration_formatted = f"{time_value} day(s)"
        elif time_unit == 'm':
            delta = datetime.timedelta(days=30 * time_value)  # Approximation for months
            duration_formatted = f"{time_value} month(s)"
        else:
            raise ValueError
    except (ValueError, IndexError):
        await interaction.followup.send("❌ Invalid duration format. Use like '7d' for 7 days or '2m' for 2 months.")
        return

    # Calculate End Time
    date_of_purchase = datetime.datetime.utcnow()
    end_time = date_of_purchase + delta
    end_timestamp = int(end_time.timestamp())

    # Set Channel Permissions
    overwrites = {
        interaction.guild.default_role: discord.PermissionOverwrite(view_channel=False),
        member: discord.PermissionOverwrite(view_channel=True, send_messages=True, mention_everyone=False)
    }
    
    # If creator is staff, add staff permissions
    if is_staff and staff_role:
        overwrites[staff_role] = discord.PermissionOverwrite(view_channel=True, send_messages=True, mention_everyone=True)
    
    try:
        # Create the Slot Channel
        channel = await interaction.guild.create_text_channel(
            name=slotname,
            category=category_obj,
            overwrites=overwrites,
            reason=f"Slot created for {member.display_name}"
        )
    except Exception as e:
        await interaction.followup.send(f"❌ Failed to create channel: {e}")
        return

    # Assign Premium Role to Member
    if premium_role:
        await member.add_roles(premium_role)

    # Insert Slot Data into MongoDB
    slot_data = {
        "userid": member.id,
        "channelid": channel.id,
        "endtime": end_timestamp,  # Store as integer timestamp
        "ping_count": 0,
        "max_pings": 3,
        "allow_everyone": True,
        "category": category.lower(),
        "created_by": interaction.user.id,
        "date_of_purchase": int(date_of_purchase.timestamp()),
        "duration": duration_formatted
    }
    await slots_collection.insert_one(slot_data)

    # Create Embed for Slot Information
    embed = discord.Embed(
        title="🎰 Slot Created 🎰",
        description=(
            f"- **Slot Owner:** {member.mention}\n"
            f"- **Date of Purchase:** <t:{int(date_of_purchase.timestamp())}:F>\n"
            f"- **Duration:** {duration_formatted}\n"
            f"- **Expiry Date:** <t:{end_timestamp}:F>\n"
            f"- **Slot Name:** {slotname}\n"
            f"- **Created By:** {interaction.user.mention}"
        ),
        color=EMBED_COLOR
    )
    embed.set_thumbnail(url=interaction.guild.icon.url if interaction.guild.icon else "")
    embed.set_footer(text="Slot Management System")
    
    # Add Permissions Details Field
    permissions_description = (
        f"**Permissions:**\n"
        f"> **Maximum Pings:** {slot_data['max_pings']}\n"
        f"> **@here/@everyone Allowed:** {'Yes' if slot_data['allow_everyone'] else 'No'}"
    )
    embed.add_field(name="Permissions", value=permissions_description, inline=False)
    
    await channel.send(embed=embed)

    # Optional: Send a Welcome Message
    welcome_embed = discord.Embed(
        title="Welcome to your Slot!",
        description=(
            "Here you can manage your slot. Use the available commands to add or remove users, renew your slot, and more.\n\n"
            f"**Note:** You can use up to {slot_data['max_pings']} pings. {'@here and @everyone mentions are enabled.' if slot_data['allow_everyone'] else '@here and @everyone mentions are enabled.'}"
        ),
        color=EMBED_COLOR
    )
    welcome_embed.set_thumbnail(url="")
    await channel.send(embed=welcome_embed)

    await interaction.followup.send(f"✅ Successfully created slot {channel.mention} for {member.mention}.")


make in this do it so ican see they can use @here or @everyone or both make it idf i can make life time and how much pings i can