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

package service

import (
	"context"
	"errors"
	"log"
	"net/http"

	"github.com/moustachioed/dash/backend/dashapi"
	"github.com/moustachioed/dash/backend/mapping"
)

// PlanApiService is a service that implements the logic for the PlanApiServicer
// This service should implement the business logic for every endpoint for the PlanApi API.
// Include any external packages or services that will be required by this service.
type PlanApiService struct {
	ds     DataStore
	mapper mapping.Mapper
}

// NewPlanApiService creates a default api service
func NewPlanApiService(ds DataStore, mapper mapping.Mapper) dashapi.PlanApiServicer {
	service := PlanApiService{
		ds:     ds,
		mapper: mapper,
	}

	return &service
}

// GetPlanDayForDate -
func (s *PlanApiService) GetPlanDayForDate(ctx context.Context, date string) (dashapi.ImplResponse, error) {
	d, err := s.mapper.StringToDate(date)
	if err != nil {
		log.Fatal(err)
	}

	dbEntry, _ := s.ds.GetPlanDayForDate(d)
	plan := s.mapper.PlanDayDsToApi(dbEntry)
	return dashapi.Response(200, plan), nil
}

// GetPlanMonthForDate -
func (s *PlanApiService) GetPlanMonthForDate(ctx context.Context, date string) (dashapi.ImplResponse, error) {
	// TODO - update GetPlanMonthForDate with the required logic for this service method.
	// Add api_plan_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, PlanMonth{}) or use other options such as http.Ok ...
	//return Response(200, PlanMonth{}), nil

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

// GetPlanWeekForDate -
func (s *PlanApiService) GetPlanWeekForDate(ctx context.Context, date string) (dashapi.ImplResponse, error) {
	d, err := s.mapper.StringToDate(date)
	if err != nil {
		log.Fatal(err)
	}

	dbEntry, _ := s.ds.GetPlanWeekForDate(d)
	plan := s.mapper.PlanWeekDsToApi(dbEntry)
	return dashapi.Response(200, plan), nil
}

// SavePlanForDay -
func (s *PlanApiService) SavePlanForDay(ctx context.Context, planDay dashapi.PlanDay) (dashapi.ImplResponse, error) {
	plan := s.mapper.PlanDayApiToDs(planDay)
	s.ds.WritePlanDay(plan)

	return dashapi.Response(200, nil), nil
}

// SavePlanForMonth -
func (s *PlanApiService) SavePlanForMonth(ctx context.Context, planMonth dashapi.PlanMonth) (dashapi.ImplResponse, error) {
	// TODO - update SavePlanForMonth with the required logic for this service method.
	// Add api_plan_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

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

// SavePlanForWeek -
func (s *PlanApiService) SavePlanForWeek(ctx context.Context, planWeek dashapi.PlanWeek) (dashapi.ImplResponse, error) {
	plan := s.mapper.PlanWeekApiToDs(planWeek)
	s.ds.WritePlanWeek(plan)

	return dashapi.Response(200, nil), nil
}
