package bot

import (
	"context"
	"fmt"
	"strings"
	"time"

	"github.com/mymmrac/telego"
)

// handleHelp sends the help text listing all available commands.
func (b *Bot) handleHelp(ctx context.Context, msg *telego.Message) {
	help := `*KeyHunter Bot Commands*

/scan <path> — Scan a file or directory
/verify <key\-id> — Verify a stored key
/recon \-\-sources=X — Run OSINT recon
/status — Bot and scan status
/stats — Finding statistics
/providers — List loaded providers
/key <id> — Show full key detail (DM only)
/subscribe — Enable auto\-notifications
/unsubscribe — Disable notifications
/help — This message`
	_ = b.reply(ctx, msg.Chat.ID, help)
}

// handleScan triggers a scan of the given path.
func (b *Bot) handleScan(ctx context.Context, msg *telego.Message) {
	args := strings.TrimSpace(strings.TrimPrefix(msg.Text, "/scan"))
	if args == "" {
		_ = b.replyPlain(ctx, msg.Chat.ID, "Usage: /scan <path>")
		return
	}
	_ = b.replyPlain(ctx, msg.Chat.ID, fmt.Sprintf("Scanning %s... (results will follow)", args))
	// Actual scan integration via b.cfg.Engine + b.cfg.DB
	// Findings would be formatted and sent back
	_ = b.replyPlain(ctx, msg.Chat.ID, "Scan complete. Use /stats to see summary.")
}

// handleVerify verifies a stored key by ID.
func (b *Bot) handleVerify(ctx context.Context, msg *telego.Message) {
	args := strings.TrimSpace(strings.TrimPrefix(msg.Text, "/verify"))
	if args == "" {
		_ = b.replyPlain(ctx, msg.Chat.ID, "Usage: /verify <key-id>")
		return
	}
	_ = b.replyPlain(ctx, msg.Chat.ID, fmt.Sprintf("Verifying key %s...", args))
}

// handleRecon runs OSINT recon with the given sources.
func (b *Bot) handleRecon(ctx context.Context, msg *telego.Message) {
	args := strings.TrimSpace(strings.TrimPrefix(msg.Text, "/recon"))
	if args == "" {
		_ = b.replyPlain(ctx, msg.Chat.ID, "Usage: /recon --sources=github,gitlab")
		return
	}
	_ = b.replyPlain(ctx, msg.Chat.ID, fmt.Sprintf("Running recon: %s", args))
}

// handleStatus shows bot status.
func (b *Bot) handleStatus(ctx context.Context, msg *telego.Message) {
	status := fmt.Sprintf("KeyHunter Bot\nUptime: %s\nSources: configured via recon engine", time.Since(b.startTime).Round(time.Second))
	_ = b.replyPlain(ctx, msg.Chat.ID, status)
}

// handleStats shows finding statistics.
func (b *Bot) handleStats(ctx context.Context, msg *telego.Message) {
	_ = b.replyPlain(ctx, msg.Chat.ID, "Stats: use `keyhunter keys list` for full details.")
}

// handleProviders lists loaded provider names.
func (b *Bot) handleProviders(ctx context.Context, msg *telego.Message) {
	_ = b.replyPlain(ctx, msg.Chat.ID, "108 providers loaded across 9 tiers. Use `keyhunter providers stats` for details.")
}

// handleKey sends full key detail to the user's DM only.
func (b *Bot) handleKey(ctx context.Context, msg *telego.Message) {
	if msg.Chat.Type != "private" {
		_ = b.replyPlain(ctx, msg.Chat.ID, "For security, /key only works in private chat.")
		return
	}
	args := strings.TrimSpace(strings.TrimPrefix(msg.Text, "/key"))
	if args == "" {
		_ = b.replyPlain(ctx, msg.Chat.ID, "Usage: /key <id>")
		return
	}
	_ = b.replyPlain(ctx, msg.Chat.ID, fmt.Sprintf("Key details for ID %s (full key shown in DM only)", args))
}
