/*
 * Project: Video Sharing Platform
 * Year: 2026
 * Developed by: Molla Samser (Founder of RSK World)
 * Designer & Tester: Rima Khatun
 * Website: https://rskworld.in
 * 
 * Video management handlers, upload logic, and metadata handling.
 */

package main

import (
	"fmt"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

var videos = []Video{
	{ID: 1, Title: "Building High-Performance Media Systems with Go", Creator: "Molla Samser", URL: "/static/vid1.mp4"},
	{ID: 2, Title: "Live: Advanced Video Processing Techniques", Creator: "Rima Khatun", URL: "/static/vid2.mp4"},
}

func GetVideos(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"status": "success",
		"data":   videos,
	})
}

func UploadVideo(c *gin.Context) {
	title := c.PostForm("title")
	creator := c.PostForm("creator")

	// Simulated file upload
	newVideo := Video{
		ID:      len(videos) + 1,
		Title:   title,
		Creator: creator,
		URL:     fmt.Sprintf("/static/vid%d.mp4", len(videos)+1),
	}

	videos = append(videos, newVideo)

	// Trigger processing in background
	go func() {
		ProcessVideo(newVideo.URL)
	}()

	c.JSON(http.StatusAccepted, gin.H{
		"message": "Video uploaded and processing started",
		"video":   newVideo,
	})
}

func StreamVideo(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	for _, v := range videos {
		if v.ID == id {
			c.JSON(http.StatusOK, gin.H{
				"id": v.ID,
				"stream_url": v.URL,
				"format": "HLS",
			})
			return
		}
	}
	c.JSON(http.StatusNotFound, gin.H{"error": "Video not found"})
}
