package nostr

import (
	"fmt"
	"time"

	"github.com/nbd-wtf/go-nostr"
)

const (
	// NIP-71: Video Events
	KindVideoHorizontal = 21
	KindVideoVertical   = 22
)

// CreateNIP71VideoEvent creates a NIP-71 video event with WebTorrent extensions
func (p *Publisher) CreateNIP71VideoEvent(data TorrentEventData) (*nostr.Event, error) {
	// Determine if vertical video based on dimensions
	kind := KindVideoHorizontal
	if data.VideoHeight > data.VideoWidth {
		kind = KindVideoVertical
	}

	event := &nostr.Event{
		Kind:      kind,
		CreatedAt: nostr.Now(),
		Content:   data.Description,
		Tags:      nostr.Tags{},
	}

	// Standard NIP-71 tags
	if data.Title != "" {
		event.Tags = event.Tags.AppendUnique(nostr.Tag{"title", data.Title})
	}
	
	event.Tags = event.Tags.AppendUnique(nostr.Tag{"published_at", fmt.Sprintf("%d", time.Now().Unix())})
	
	if data.Description != "" {
		event.Tags = event.Tags.AppendUnique(nostr.Tag{"alt", data.Description})
	}

	// Duration in seconds
	if data.Duration > 0 {
		event.Tags = event.Tags.AppendUnique(nostr.Tag{"duration", fmt.Sprintf("%d", data.Duration)})
	}

	// Add HLS streaming URL
	if data.HLSPlaylistURL != "" {
		event.Tags = event.Tags.AppendUnique(nostr.Tag{
			"imeta",
			fmt.Sprintf("dim %dx%d", data.VideoWidth, data.VideoHeight),
			fmt.Sprintf("url %s", data.HLSPlaylistURL),
			"m application/x-mpegURL",
		})
	}

	// Add direct download URL
	if data.StreamURL != "" {
		event.Tags = event.Tags.AppendUnique(nostr.Tag{
			"imeta",
			fmt.Sprintf("dim %dx%d", data.VideoWidth, data.VideoHeight),
			fmt.Sprintf("url %s", data.StreamURL),
			fmt.Sprintf("m %s", data.MimeType),
			fmt.Sprintf("size %d", data.FileSize),
		})
	}

	// Add WebTorrent magnet link
	if data.MagnetLink != "" {
		event.Tags = event.Tags.AppendUnique(nostr.Tag{"magnet", data.MagnetLink})
		
		// WebTorrent extensions
		event.Tags = event.Tags.AppendUnique(nostr.Tag{"webtorrent", data.MagnetLink})
		event.Tags = event.Tags.AppendUnique(nostr.Tag{"streaming_method", "webtorrent"})
		event.Tags = event.Tags.AppendUnique(nostr.Tag{"piece_order", "sequential"})
	}

	// Add WebSocket trackers for WebTorrent
	wsTrackers := []string{
		"wss://tracker.btorrent.xyz",
		"wss://tracker.openwebtorrent.com", 
		"wss://tracker.webtorrent.dev",
	}
	for _, tracker := range wsTrackers {
		event.Tags = event.Tags.AppendUnique(nostr.Tag{"ws_tracker", tracker})
	}

	// Add WebSeed fallback
	if data.WebSeedURL != "" {
		event.Tags = event.Tags.AppendUnique(nostr.Tag{"webseed", data.WebSeedURL})
	}

	// Cross-reference with NIP-35 torrent
	if data.InfoHash != "" {
		event.Tags = event.Tags.AppendUnique(nostr.Tag{"i", data.InfoHash})
	}

	// Blossom reference
	if data.BlossomHash != "" {
		event.Tags = event.Tags.AppendUnique(nostr.Tag{"blossom", data.BlossomHash})
	}

	// Content categorization
	event.Tags = event.Tags.AppendUnique(nostr.Tag{"t", "video"})
	event.Tags = event.Tags.AppendUnique(nostr.Tag{"t", "p2p"})
	event.Tags = event.Tags.AppendUnique(nostr.Tag{"t", "webtorrent"})
	event.Tags = event.Tags.AppendUnique(nostr.Tag{"t", "streaming"})

	// Quality tags
	if data.VideoHeight >= 2160 {
		event.Tags = event.Tags.AppendUnique(nostr.Tag{"t", "4k"})
	} else if data.VideoHeight >= 1080 {
		event.Tags = event.Tags.AppendUnique(nostr.Tag{"t", "hd"})
	} else if data.VideoHeight >= 720 {
		event.Tags = event.Tags.AppendUnique(nostr.Tag{"t", "hd"})
	}

	// Sign the event
	err := event.Sign(p.privateKey)
	if err != nil {
		return nil, fmt.Errorf("error signing NIP-71 event: %w", err)
	}

	return event, nil
}