package com.red.circle.other.app.service.signin;

import com.red.circle.other.app.command.signin.SignCheckInExe;
import com.red.circle.other.app.command.signin.SignInStatusQueryExe;
import com.red.circle.other.app.command.signin.SupplementCheckInExe;
import com.red.circle.other.app.dto.clientobject.signin.CheckInResultCO;
import com.red.circle.other.app.dto.clientobject.signin.SignInStatusCO;
import com.red.circle.other.app.dto.cmd.signin.CheckInCmd;
import com.red.circle.other.app.dto.cmd.signin.SupplementCmd;
import com.red.circle.other.app.util.DistributedLockUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class SignInServiceImpl implements SignInService {

    private final SignInStatusQueryExe statusQueryExe;
    private final SignCheckInExe signCheckInExe;
    private final SupplementCheckInExe supplementCheckInExe;
    private final DistributedLockUtil distributedLockUtil;
    private static final String SIGN_IN_LOCK_KEY = "sign_in:lock:";
    private static final String SUPPLEMENT_LOCK_KEY = "sign_in:supplement:lock:";

    @Override
    public SignInStatusCO getStatus(Long userId) {
        return statusQueryExe.execute(userId);
    }

    @Override
    public CheckInResultCO checkIn(CheckInCmd cmd) {
        return distributedLockUtil.executeWithLock(SIGN_IN_LOCK_KEY + cmd.getReqUserId(),
                () -> signCheckInExe.execute(cmd));
    }

    @Override
    public CheckInResultCO supplement(SupplementCmd cmd) {
        return distributedLockUtil.executeWithLock(SUPPLEMENT_LOCK_KEY + cmd.getReqUserId(),
                () -> supplementCheckInExe.execute(cmd));
    }
}
