// 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"
	"net/http"

	"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"
)

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

// NewDesignSchemaApiService creates a default api service
func NewDesignSchemaApiService() openapi.DesignSchemaApiServicer {
	return &DesignSchemaApiService{}
}

// CreateDesignSchema - Update a design schema
func (s *DesignSchemaApiService) CreateDesignSchema(_ context.Context, user string, designId string,
	designSchema openapi.DesignSchema) (openapi.ImplResponse, error) {
	//TODO input validation
	zap.S().Debugf("Create design schema request received for designId: %v", designId)

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

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

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

// GetDesignSchema - Get a design schema owned by user
func (s *DesignSchemaApiService) GetDesignSchema(_ context.Context, user string, designId string) (openapi.ImplResponse, error) {
	//TODO input validation
	zap.S().Debugf("Get design schema details 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.GetDesignSchemaEndPoint, uriMap)

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

	schema := openapi.DesignSchema{}
	err = util.ByteToStruct(body, &schema)

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

// UpdateDesignSchema - Update a schema for a given design
func (s *DesignSchemaApiService) UpdateDesignSchema(_ context.Context, user string, designId string,
	designSchema openapi.DesignSchema) (openapi.ImplResponse, error) {
	zap.S().Debugf("Update design schema request received for designId: %v", designId)

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

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

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

func (s *DesignSchemaApiService) DeleteDesignSchema(_ context.Context, user string, designId string) (
	openapi.ImplResponse, error,
) {
	zap.S().Debugf("Delete design schema request received for designId: %v", designId)

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

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

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