package systemconfig

import (
	"context"
	"encoding/json"
)

func (s *Service) PublishOptions(ctx context.Context) (*PublishOptionsDTO, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	options, err := s.publishOptions(ctx)
	if err != nil {
		return nil, err
	}
	return &options, nil
}

func (s *Service) SalePriceConfig(ctx context.Context) (*PublishSalePriceConfig, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	config, err := s.salePriceConfig(ctx)
	if err != nil {
		return nil, err
	}
	return &config, nil
}

func (s *Service) OrderAgreements(ctx context.Context) (*OrderAgreementsDTO, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	agreements, err := s.orderAgreements(ctx)
	if err != nil {
		return nil, err
	}
	return &agreements, nil
}

func (s *Service) ListingPublishAgreements(ctx context.Context) (*ListingPublishAgreementsDTO, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	agreements, err := s.listingPublishAgreements(ctx)
	if err != nil {
		return nil, err
	}
	return &agreements, nil
}

func (s *Service) PostRentalNotice(ctx context.Context) (*PostRentalNoticeDTO, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	notice, err := s.postRentalNotice(ctx)
	if err != nil {
		return nil, err
	}
	return &notice, nil
}

func (s *Service) publishOptions(ctx context.Context) (PublishOptionsDTO, error) {
	value, err := s.repo.FindValue(ctx, publishOptionsConfigKey)
	if err != nil {
		return PublishOptionsDTO{}, err
	}
	options := DefaultPublishOptions()
	if err := json.Unmarshal([]byte(value), &options); err != nil {
		options = DefaultPublishOptions()
	}
	normalizePublishOptions(&options)
	return options, nil
}

func (s *Service) salePriceConfig(ctx context.Context) (PublishSalePriceConfig, error) {
	value, err := s.repo.FindValue(ctx, salePriceConfigKey)
	if err != nil {
		return PublishSalePriceConfig{}, err
	}
	config := DefaultSalePriceConfig()
	if err := json.Unmarshal([]byte(value), &config); err != nil {
		config = DefaultSalePriceConfig()
	}
	normalizeSalePriceConfig(&config)
	return config, nil
}

func (s *Service) orderAgreements(ctx context.Context) (OrderAgreementsDTO, error) {
	value, err := s.repo.FindValue(ctx, orderAgreementsConfigKey)
	if err != nil {
		return OrderAgreementsDTO{}, err
	}
	agreements := DefaultOrderAgreements()
	if err := json.Unmarshal([]byte(value), &agreements); err != nil {
		agreements = DefaultOrderAgreements()
	}
	normalizeOrderAgreements(&agreements)
	return agreements, nil
}

func (s *Service) listingPublishAgreements(ctx context.Context) (ListingPublishAgreementsDTO, error) {
	value, err := s.repo.FindValue(ctx, listingPublishAgreementsConfigKey)
	if err != nil {
		return ListingPublishAgreementsDTO{}, err
	}
	agreements := DefaultListingPublishAgreements()
	if err := json.Unmarshal([]byte(value), &agreements); err != nil {
		agreements = DefaultListingPublishAgreements()
	}
	normalizeListingPublishAgreements(&agreements)
	return agreements, nil
}

func (s *Service) postRentalNotice(ctx context.Context) (PostRentalNoticeDTO, error) {
	value, err := s.repo.FindValue(ctx, postRentalNoticeConfigKey)
	if err != nil {
		return PostRentalNoticeDTO{}, err
	}
	notice := DefaultPostRentalNotice()
	if err := json.Unmarshal([]byte(value), &notice); err != nil {
		notice = DefaultPostRentalNotice()
	}
	normalizePostRentalNotice(&notice)
	return notice, nil
}
