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

	"go.uber.org/zap"

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

// DesignCodeApiService is a service that implents the logic for the DesignCodeApiServicer
// This service should implement the business logic for every endpoint for the DesignCodeApi API.
// Include any external packages or services that will be required by this service.
type DesignCodeApiService struct {
	dbService database.DBService
}

// NewDesignCodeApiService creates a default api service
func NewDesignCodeApiService(dbService database.DBService) openapi.DesignCodeApiServicer {
	return &DesignCodeApiService{
		dbService: dbService,
	}
}

// CreateDesignCode - Upload a new design code
func (s *DesignCodeApiService) CreateDesignCode(ctx context.Context, user string, designId string,
	fileName string, fileData *os.File) (openapi.ImplResponse, error) {
	zap.S().Debugf("Received CreateDesignCode POST request: %s | %s | %s", user, designId, fileName)

	// Don't forget to close the temp file
	defer fileData.Close()

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

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

// DeleteDesignCode - Delete a zipped design code file owned by user
func (s *DesignCodeApiService) DeleteDesignCode(ctx context.Context, user string, designId string) (openapi.ImplResponse, error) {
	zap.S().Debugf("Received DeleteDesignCode Delete request: %s | %s", user, designId)

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

// GetDesignCode - Get a zipped design code file owned by user
func (s *DesignCodeApiService) GetDesignCode(ctx context.Context, user string, designId string) (openapi.ImplResponse, error) {
	zap.S().Debugf("Received GetDesignCode Get request: %s | %s", user, designId)

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

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

// GetDesignCodeRevision - Get a revision number of design code
func (s *DesignCodeApiService) GetDesignCodeRevision(ctx context.Context, user string, designId string) (openapi.ImplResponse, error) {
	info, err := s.dbService.GetDesignCodeRevision(user, designId)
	if err != nil {
		return openapi.Response(http.StatusInternalServerError, nil), err
	}
	return openapi.Response(http.StatusOK, info), nil
}

// UpdateDesignCode - Update a design code
func (s *DesignCodeApiService) UpdateDesignCode(ctx context.Context, user string, designId string,
	fileName string, fileData *os.File) (openapi.ImplResponse, error) {
	// TODO - update UpdateDesignCode with the required logic for this service method.
	// Add api_design_codes_service.go to the .openapi-generator-ignore to avoid overwriting this service
	// implementation when updating open api generation.

	//TODO: Uncomment the next line to return response Response(200, {}) or use other options such as http.Ok ...
	//return Response(200, nil),nil

	//TODO: Uncomment the next line to return response Response(0, Error{}) or use other options such as http.Ok ...
	//return Response(0, Error{}), nil

	return openapi.Response(http.StatusNotImplemented, nil), errors.New("UpdateDesignCode method not implemented")
}
