package location

import (
	"github.com/gofiber/fiber/v2"
	"matrix-common/pkg/logger"
	"matrix-middle-service/internel/controller/record/location/dto"
	"matrix-middle-service/pkg/database"
	"matrix-middle-service/pkg/database/model"
	response "matrix-middle-service/pkg/resp"
	"matrix-middle-service/pkg/resp/errorx"
	utils "matrix-middle-service/pkg/utils/json"
	"matrix-middle-service/pkg/utils/sonyflake"
	"matrix-middle-service/pkg/utils/time"
)

func SetupLocation(r fiber.Router) {
	api := r.Group("/location")
	postLocation(api)
	getLocation(api)
}

type Data struct {
	CallbackTime time.Time `json:"callbackTime"`
	LocationTime time.Time `json:"locationTime"`
	LocationType int       `json:"locationType,omitempty"`
	Latitude     float64   `json:"latitude,omitempty"`
	Longitude    float64   `json:"longitude,omitempty"`
	Accuracy     float64   `json:"accuracy,omitempty"`
	Altitude     float64   `json:"altitude,omitempty"`
	Bearing      float64   `json:"bearing,omitempty"`
	Speed        float64   `json:"speed,omitempty"`
	Country      string    `json:"country,omitempty"`
	Province     string    `json:"province,omitempty"`
	City         string    `json:"city,omitempty"`
	District     string    `json:"district,omitempty"`
	Street       string    `json:"street,omitempty"`
	StreetNumber string    `json:"streetNumber,omitempty"`
	CityCode     string    `json:"cityCode,omitempty"`
	AdCode       string    `json:"adCode,omitempty"`
	Address      string    `json:"address,omitempty"`
	Description  string    `json:"description,omitempty"`
}

// postLocation
// @Summary      上报定位信息
// @Description  上报定位信息
// @Tags         Record
// @Accept       json
// @Produce      json
// @Param		vo body model.LocationRecord true "定位信息"
// @Success      200  {object}  response.Response{data=model.LocationRecord}
// @Failure	     default {object}  errorx.CodeErrorResponse
// @Router       /record/location/post [post]
func postLocation(api fiber.Router) {
	api.Post("/post", func(ctx *fiber.Ctx) error {
		data := &model.LocationRecord{}
		err := ctx.BodyParser(data)
		if err != nil {
			logger.Log().Error(err)
			return ctx.JSON(errorx.ParseError(err))
		}

		data.Id = sonyflake.NextStrId()
		logger.Log().Info(utils.Json(data))
		return database.Exec(func(db database.DataBase) error {
			err := db.RecordLocation(data)
			if err = errorx.ParseError(err); err != nil {
				return ctx.JSON(err)
			}
			return ctx.JSON(response.NewResponse(data))
		})
	})
}

// getLocation
// @Summary      查询定位信息
// @Description  查询定位信息
// @Tags         Record
// @Accept       json
// @Produce      json
// @Param		vo body dto.GetDto true "查询查询定位信息"
// @Success      200  {object}  response.Response{data=[]model.LocationRecord}
// @Failure	     default {object}  errorx.CodeErrorResponse
// @Router       /record/location/get [post]
func getLocation(api fiber.Router) {
	api.Post("/get", func(ctx *fiber.Ctx) error {
		data := &dto.GetDto{}
		err := ctx.BodyParser(data)
		if err != nil {
			logger.Log().Error(err)
			return ctx.JSON(errorx.ParseError(err))
		}

		return database.Exec(func(db database.DataBase) error {
			list, err := db.GetLocationRecord(data)
			if err = errorx.ParseError(err); err != nil {
				return ctx.JSON(err)
			}
			return ctx.JSON(response.NewResponse(list))
		})
	})
}
