package com.ttstd.signaling.store;

import com.ttstd.signaling.model.PairingCode;
import com.ttstd.signaling.repository.PairingCodeRepository;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.util.Optional;

/** JPA 实现，配对码持久化到 MySQL（storage.mode=jpa，默认）。 */
@Component
@Profile("!memory")
public class JpaPairingStore implements PairingStore {

    private final PairingCodeRepository repository;

    public JpaPairingStore(PairingCodeRepository repository) {
        this.repository = repository;
    }

    @Override
    public PairingCode save(PairingCode code) {
        return repository.save(code);
    }

    @Override
    public Optional<PairingCode> findByCodeHash(String codeHash) {
        return repository.findByCodeHash(codeHash);
    }

    @Override
    public void delete(String codeHash) {
        repository.deleteById(codeHash);
    }
}
