package com.ttstd.signaling.store;

import com.ttstd.signaling.model.UserAccount;
import com.ttstd.signaling.repository.UserAccountRepository;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

/** JPA 实现：用户账号持久化到 MySQL。仅在 storage.mode=jpa（默认）时生效。 */
@Component
@Profile("!memory")
public class JpaUserStore implements UserStore {

    private final UserAccountRepository repository;

    public JpaUserStore(UserAccountRepository repository) {
        this.repository = repository;
    }

    @Override
    public Optional<UserAccount> findByUserId(String userId) {
        return repository.findById(userId);
    }

    @Override
    public Optional<UserAccount> findByUsername(String username) {
        return repository.findByUsername(username);
    }

    @Override
    public boolean existsByUsername(String username) {
        return repository.existsByUsername(username);
    }

    @Override
    public UserAccount save(UserAccount account) {
        return repository.save(account);
    }

    @Override
    public List<UserAccount> listAll() {
        return repository.findAll();
    }

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