/*
 * This file is part of sdbapi
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

package handlers

import (
	"api/apierror"
	"api/db"
	"api/models"
	"net/http"

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

func PatchGameState(c *gin.Context) {
	id := c.Param("id")

	// check if the game exists
	var game models.Game
	record := db.DbConn.Where("id = ?", id).First(&game)
	if record.Error != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": apierror.GameNotFound})
		c.Abort()
		return
	}

	// get the user who requested the patching, and verify that they are the game host
	presumedHost, _ := db.GetUserByEmail(c.GetString("email"))
	if game.HostID != presumedHost.ID {
		c.JSON(http.StatusUnauthorized, gin.H{"error": apierror.NotAuthorized})
		c.Abort()
		return
	}

	var patchedGame models.Game
	if err := c.ShouldBindJSON(&patchedGame); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": apierror.InvalidInput})
		c.Abort()
		return
	}

	// store failed state
	failed := false
	// a long set of verification
	if game.State == models.GAMESTATE_FORMING {
		// allow INPROGRESS and CANCELLED
		if patchedGame.State != models.GAMESTATE_INPROGRESS && patchedGame.State != models.GAMESTATE_CANCELLED {
			failed = true
		}
		// do we have a guest?
		if game.GuestID == "" {
			failed = true
		}
	} else if game.State == models.GAMESTATE_INPROGRESS {
		// allow only FINISHED
		if patchedGame.State != models.GAMESTATE_FINISHED {
			failed = true
		}
	} else if game.State == models.GAMESTATE_FINISHED {
		failed = true
	} else if game.State == models.GAMESTATE_CANCELLED {
		failed = true
	}

	if failed {
		c.JSON(http.StatusInternalServerError, gin.H{"error": apierror.GameStatePatchFailed})
		c.Abort()
		return
	}

	// everything is fine so far, so go through with it
	updatedRecord := db.DbConn.Model(&game).Update("state", patchedGame.State)
	if updatedRecord.Error != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": apierror.GameStatePatchFailed})
		c.Abort()
		return
	}

	// don't have anything to return
	c.JSON(http.StatusOK, gin.H{})
}