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

// JournalApiService is a service that implements the logic for the JournalApiServicer
type JournalApiService struct {
	ds     DataStore
	mapper mapping.Mapper
}

// NewJournalApiService creates a default api service
func NewJournalApiService(ds DataStore, mapper mapping.Mapper) dashapi.JournalApiServicer {
	service := JournalApiService{
		ds:     ds,
		mapper: mapper,
	}

	return &service
}

// DeleteJournalEntryForDate -
func (s *JournalApiService) DeleteJournalEntryForDate(ctx context.Context, date string) (dashapi.ImplResponse, error) {
	// TODO - update DeleteJournalEntryForDate with the required logic for this service method.
	// Add api_journal_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("DeleteJournalEntryForDate method not implemented")
}

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

	dbEntry, _ := s.ds.GetJournalEntryForDate(d)
	journal := s.mapper.JournalDsToApi(dbEntry)
	return dashapi.Response(200, journal), nil
}

// WriteJournalEntry -
func (s *JournalApiService) WriteJournalEntry(ctx context.Context, journalEntry dashapi.JournalEntry) (dashapi.ImplResponse, error) {
	journal := s.mapper.JournalApiToDs(journalEntry)
	s.ds.WriteJournalEntry(journal)

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