package com.izouma.dingdong.service;

import com.izouma.dingdong.domain.Coupon;
import com.izouma.dingdong.repo.CouponRepo;
import com.izouma.dingdong.service.merchant.MerchantService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;


@Service
@AllArgsConstructor
public class CouponService {

    private CouponRepo      couponRepo;
    private MerchantService merchantService;

    public List<Coupon> my(Long userId, Boolean isAll) {
        Long merchantId = merchantService.findMerchantId(userId);
        List<Coupon> coupons = couponRepo.findAllByMerchantIdAndEnabledTrue(merchantId);
        if (isAll) {
            return coupons.stream()
                    .filter(c -> !c.getStartDate().isBefore(LocalDate.now()))
                    .collect(Collectors.toList());
        }
        return coupons.stream().filter(c -> !c.getEndDate().isBefore(LocalDate.now())).collect(Collectors.toList());
    }

}
