package handler

import (
	"errors"
	"io"
	"net/http"

	"juhe-factory/api/internal/service"

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

func (h *Creative) ListScriptAnalyses(c *gin.Context) {
	items, err := h.service.ListScriptAnalyses(currentWebUser(c).ID)
	if err != nil {
		h.respondError(c, err)
		return
	}
	c.JSON(http.StatusOK, gin.H{"data": items})
}

func (h *Creative) CreateScriptAnalysis(c *gin.Context) {
	item, err := h.service.CreateScriptAnalysis(currentWebUser(c).ID)
	if err != nil {
		h.respondError(c, err)
		return
	}
	c.JSON(http.StatusCreated, gin.H{"data": item})
}

func (h *Creative) ListScriptAnalysisImportProjects(c *gin.Context) {
	items, err := h.service.ListScriptAnalysisImportProjects(currentWebUser(c).ID)
	if err != nil {
		h.respondError(c, err)
		return
	}
	c.JSON(http.StatusOK, gin.H{"data": items})
}

func (h *Creative) ImportScriptAnalysisProject(c *gin.Context) {
	id, err := service.ParseUUID(c.Param("script_id"), "剧本")
	if err != nil {
		h.bad(c, err)
		return
	}
	var body struct {
		ProjectID uuid.UUID `json:"project_id"`
	}
	if err := c.ShouldBindJSON(&body); err != nil || body.ProjectID == uuid.Nil {
		h.bad(c, errors.New("请选择要导入的剧本反推项目"))
		return
	}
	item, err := h.service.ImportScriptAnalysisProject(currentWebUser(c).ID, id, body.ProjectID)
	if err != nil {
		h.respondError(c, err)
		return
	}
	c.JSON(http.StatusOK, gin.H{"data": item})
}

func (h *Creative) ImportScriptAnalysisFile(c *gin.Context) {
	id, err := service.ParseUUID(c.Param("script_id"), "剧本")
	if err != nil {
		h.bad(c, err)
		return
	}
	c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 4<<20)
	if err := c.Request.ParseMultipartForm(4 << 20); err != nil {
		h.bad(c, errors.New("导入文件无效"))
		return
	}
	if c.Request.MultipartForm != nil {
		defer c.Request.MultipartForm.RemoveAll()
	}
	file, header, err := c.Request.FormFile("file")
	if err != nil {
		h.bad(c, errors.New("请选择剧本文件"))
		return
	}
	defer file.Close()
	content, err := io.ReadAll(io.LimitReader(file, 3*1024*1024+1))
	if err != nil {
		h.bad(c, errors.New("读取剧本文件失败"))
		return
	}
	item, err := h.service.ImportScriptAnalysisFile(currentWebUser(c).ID, id, header.Filename, content)
	if err != nil {
		h.respondError(c, err)
		return
	}
	c.JSON(http.StatusOK, gin.H{"data": item})
}

func (h *Creative) GetScriptAnalysis(c *gin.Context) {
	id, err := service.ParseUUID(c.Param("script_id"), "剧本")
	if err != nil {
		h.bad(c, err)
		return
	}
	item, err := h.service.GetScriptAnalysis(currentWebUser(c).ID, id)
	if err != nil {
		h.respondError(c, err)
		return
	}
	c.JSON(http.StatusOK, gin.H{"data": item})
}

func (h *Creative) UpdateScriptAnalysis(c *gin.Context) {
	id, err := service.ParseUUID(c.Param("script_id"), "剧本")
	if err != nil {
		h.bad(c, err)
		return
	}
	var input service.ScriptAnalysisInput
	if err := c.ShouldBindJSON(&input); err != nil {
		h.bad(c, err)
		return
	}
	item, err := h.service.UpdateScriptAnalysis(currentWebUser(c).ID, id, input)
	if err != nil {
		h.respondError(c, err)
		return
	}
	c.JSON(http.StatusOK, gin.H{"data": item})
}

func (h *Creative) DeleteScriptAnalysis(c *gin.Context) {
	id, err := service.ParseUUID(c.Param("script_id"), "剧本")
	if err != nil {
		h.bad(c, err)
		return
	}
	if err := h.service.DeleteScriptAnalysis(currentWebUser(c).ID, id); err != nil {
		h.respondError(c, err)
		return
	}
	c.Status(http.StatusNoContent)
}

func (h *Creative) QueueScriptAnalysis(c *gin.Context) {
	id, err := service.ParseUUID(c.Param("script_id"), "剧本")
	if err != nil {
		h.bad(c, err)
		return
	}
	task, err := h.service.QueueScriptAnalysis(currentWebUser(c).ID, id)
	if err != nil {
		h.respondError(c, err)
		return
	}
	c.JSON(http.StatusAccepted, gin.H{"data": task})
}

func (h *Creative) CancelScriptAnalysis(c *gin.Context) {
	id, err := service.ParseUUID(c.Param("script_id"), "剧本")
	if err != nil {
		h.bad(c, err)
		return
	}
	if err := h.service.CancelScriptAnalysis(currentWebUser(c).ID, id); err != nil {
		h.respondError(c, err)
		return
	}
	c.Status(http.StatusNoContent)
}
