// Copyright 2022 Cisco Systems, Inc. and its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

/*
 * Flame REST API
 *
 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 * API version: 1.0.0
 * Generated by: OpenAPI Generator (https://openapi-generator.tech)
 */

package apiserver

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

	"go.uber.org/zap"

	"github.com/cisco-open/flame/pkg/openapi"
	"github.com/cisco-open/flame/pkg/openapi/constants"
	"github.com/cisco-open/flame/pkg/restapi"
	"github.com/cisco-open/flame/pkg/util"
)

// DesignsApiService is a service that implements the logic for the DesignsApiServicer
// This service should implement the business logic for every endpoint for the DesignsApi API.
// Include any external packages or services that will be required by this service.
type DesignsApiService struct {
}

// NewDesignsApiService creates a default api service
func NewDesignsApiService() openapi.DesignsApiServicer {
	return &DesignsApiService{}
}

// CreateDesign - Create a new design template.
func (s *DesignsApiService) CreateDesign(ctx context.Context, user string, designInfo openapi.DesignInfo) (openapi.ImplResponse, error) {
	//TODO input validation
	zap.S().Debugf("New design request received for user: %s | designInfo: %v", user, designInfo)

	// create controller request
	uriMap := map[string]string{
		constants.ParamUser: user,
	}
	url := restapi.CreateURL(HostEndpoint, restapi.CreateDesignEndPoint, uriMap)

	// send post request
	code, body, err := restapi.HTTPPost(url, designInfo, "application/json")
	errResp, retErr := errorResponse(code, body, err)
	if retErr != nil {
		return errResp, retErr
	}

	return openapi.Response(http.StatusCreated, nil), nil
}

// DeleteDesign - Delete design template
func (s *DesignsApiService) DeleteDesign(ctx context.Context, user string,
	designId string, forceDelete bool) (openapi.ImplResponse, error) {
	zap.S().Debugf("Design delete request received for user: %s | DesignId: %v", user, designId)

	// create controller request
	uriMap := map[string]string{
		constants.ParamUser:        user,
		constants.ParamDesignID:    designId,
		constants.ParamForceDelete: strconv.FormatBool(forceDelete),
	}

	url := restapi.CreateURL(HostEndpoint, restapi.DeleteDesignEndPoint, uriMap)

	// send delete request
	code, body, err := restapi.HTTPDelete(url, designId, "application/json")
	errResp, retErr := errorResponse(code, body, err)
	if retErr != nil {
		return errResp, retErr
	}

	return openapi.Response(http.StatusOK, fmt.Sprintf("%s design deleted", designId)), nil
}

// GetDesign - Get design template information
func (s *DesignsApiService) GetDesign(ctx context.Context, user string, designId string) (openapi.ImplResponse, error) {
	//TODO input validation
	zap.S().Debugf("Get design template information for user: %s | designId: %s", user, designId)

	//create controller request
	uriMap := map[string]string{
		constants.ParamUser:     user,
		constants.ParamDesignID: designId,
	}
	url := restapi.CreateURL(HostEndpoint, restapi.GetDesignEndPoint, uriMap)

	//send get request
	code, body, err := restapi.HTTPGet(url)
	errResp, retErr := errorResponse(code, body, err)
	if retErr != nil {
		return errResp, retErr
	}

	design := openapi.Design{}
	err = util.ByteToStruct(body, &design)

	return openapi.Response(http.StatusOK, design), err
}

// GetDesigns - Get list of all the designs created by the user.
func (s *DesignsApiService) GetDesigns(ctx context.Context, user string, limit int32) (openapi.ImplResponse, error) {
	//TODO input validation
	zap.S().Debugf("get list of designs for user: %s | limit: %d", user, limit)

	//create controller request
	//construct URL
	uriMap := map[string]string{
		constants.ParamUser:  user,
		constants.ParamLimit: strconv.Itoa(int(limit)),
	}
	url := restapi.CreateURL(HostEndpoint, restapi.GetDesignsEndPoint, uriMap)

	//send get request
	code, body, err := restapi.HTTPGet(url)
	errResp, retErr := errorResponse(code, body, err)
	if retErr != nil {
		return errResp, retErr
	}

	designInfoList := []openapi.DesignInfo{}
	err = util.ByteToStruct(body, &designInfoList)

	return openapi.Response(http.StatusOK, designInfoList), err
}

// UpdateDesign - Update design
func (s *DesignsApiService) UpdateDesign(ctx context.Context, user string, designId string,
	designInfo openapi.DesignInfo) (openapi.ImplResponse, error) {
	zap.S().Debugf("Update design request received for designId: %v", designId)

	//create controller request
	uriMap := map[string]string{
		constants.ParamUser:     user,
		constants.ParamDesignID: designId,
	}
	url := restapi.CreateURL(HostEndpoint, restapi.UpdateDesignEndPoint, uriMap)

	//send put request
	code, body, err := restapi.HTTPPut(url, designInfo, "application/json")
	errResp, retErr := errorResponse(code, body, err)
	if retErr != nil {
		return errResp, retErr
	}

	return openapi.Response(http.StatusOK, nil), err
}
