-- Migration: Add custom_commands tables
-- Date: 2025-10-02
-- Description: Creates tables for Discord bot custom command functionality

-- Create custom_command_creators table
CREATE TABLE IF NOT EXISTS custom_command_creators (
    id SERIAL PRIMARY KEY,
    discord_id BIGINT UNIQUE NOT NULL,
    username VARCHAR(32) NOT NULL,
    display_name VARCHAR(32),
    created_at TIMESTAMP NOT NULL,
    total_commands INTEGER DEFAULT 0,
    active_commands INTEGER DEFAULT 0
);

CREATE INDEX IF NOT EXISTS idx_custom_command_creators_discord_id
ON custom_command_creators(discord_id);

-- Create custom_commands table
CREATE TABLE IF NOT EXISTS custom_commands (
    id SERIAL PRIMARY KEY,
    name VARCHAR(32) UNIQUE NOT NULL,
    content TEXT NOT NULL,
    creator_id INTEGER NOT NULL,
    created_at TIMESTAMP NOT NULL,
    updated_at TIMESTAMP,
    last_used TIMESTAMP,
    use_count INTEGER DEFAULT 0,
    warning_sent BOOLEAN DEFAULT FALSE,
    is_active BOOLEAN DEFAULT TRUE,
    tags TEXT,
    FOREIGN KEY (creator_id) REFERENCES custom_command_creators (id)
);

CREATE INDEX IF NOT EXISTS idx_custom_commands_name
ON custom_commands(name);

CREATE INDEX IF NOT EXISTS idx_custom_commands_creator_id
ON custom_commands(creator_id);

CREATE INDEX IF NOT EXISTS idx_custom_commands_is_active
ON custom_commands(is_active);

CREATE INDEX IF NOT EXISTS idx_custom_commands_last_used
ON custom_commands(last_used);
