package listing

import (
	"context"
)

func (s *Service) Create(ctx context.Context, ownerID uint64, req CreateRequest) (*ListingDTO, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	if !req.AgreedVirtualAssetSale || !req.AgreedSellerAgreement {
		return nil, ErrAgreementRequired
	}
	rules, err := s.publishRules(ctx)
	if err != nil {
		return nil, err
	}
	if err := validateRequest(req, rules); err != nil {
		return nil, err
	}
	if err := validateRequiredOnlineTime(req); err != nil {
		return nil, err
	}
	reviewRequired, err := s.reviewRequired(ctx)
	if err != nil {
		return nil, err
	}
	return s.repo.Create(ctx, ownerID, req, reviewRequired)
}

func (s *Service) Update(ctx context.Context, ownerID uint64, id uint64, req UpdateRequest) (*ListingDTO, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	rules, err := s.publishRules(ctx)
	if err != nil {
		return nil, err
	}
	if err := validateRequest(req, rules); err != nil {
		return nil, err
	}
	if err := validateRequiredOnlineTime(req); err != nil {
		return nil, err
	}
	reviewRequired, err := s.reviewRequired(ctx)
	if err != nil {
		return nil, err
	}
	return s.repo.Update(ctx, ownerID, id, req, reviewRequired)
}

func (s *Service) SubmitReview(ctx context.Context, ownerID uint64, id uint64) (*ListingDTO, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	reviewRequired, err := s.reviewRequired(ctx)
	if err != nil {
		return nil, err
	}
	return s.repo.SubmitReview(ctx, ownerID, id, reviewRequired)
}

func (s *Service) Offline(ctx context.Context, ownerID uint64, id uint64) (*ListingDTO, error) {
	if s.repo == nil {
		return nil, ErrDependencyUnavailable
	}
	return s.repo.Offline(ctx, ownerID, id)
}
