// 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 controller

import (
	"context"
	"net/http"

	"go.uber.org/zap"

	"github.com/cisco-open/flame/cmd/controller/app/database"
	"github.com/cisco-open/flame/pkg/openapi"
)

// DesignSchemaApiService is a service that implents 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 {
	dbService database.DBService
}

// NewDesignSchemaApiService creates a default api service
func NewDesignSchemaApiService(dbService database.DBService) openapi.DesignSchemaApiServicer {
	return &DesignSchemaApiService{
		dbService: dbService,
	}
}

// CreateDesignSchema - Update a design schema
func (s *DesignSchemaApiService) CreateDesignSchema(_ context.Context, user string, designId string,
	designSchema openapi.DesignSchema) (openapi.ImplResponse, error) {
	err := s.dbService.CreateDesignSchema(user, designId, designSchema)
	if err != nil {
		return openapi.Response(http.StatusInternalServerError, nil), err
	}

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

// GetDesignSchema - Get a design schema owned by user
func (s *DesignSchemaApiService) GetDesignSchema(_ context.Context, user string, designId string) (openapi.ImplResponse, error) {
	info, err := s.dbService.GetDesignSchema(user, designId)
	if err != nil {
		return openapi.Response(http.StatusInternalServerError, nil), err
	}
	return openapi.Response(http.StatusOK, info), nil
}

// UpdateDesignSchema - Update a schema for a given design
func (s *DesignSchemaApiService) UpdateDesignSchema(_ context.Context, user string, designId string,
	designSchema openapi.DesignSchema) (openapi.ImplResponse, error) {
	err := s.dbService.UpdateDesignSchema(user, designId, designSchema)
	if err != nil {
		return openapi.Response(http.StatusInternalServerError, nil), err
	}

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

func (s *DesignSchemaApiService) DeleteDesignSchema(_ context.Context, user string, designId string) (
	openapi.ImplResponse, error,
) {
	zap.S().Debugf("Received DeleteDesignSchema Delete request: user: %s | designId: %s ", user, designId)

	err := s.dbService.DeleteDesignSchema(user, designId)
	if err != nil {
		return openapi.Response(http.StatusInternalServerError, nil), err
	}
	return openapi.Response(http.StatusOK, nil), nil
}
