package com.ttstd.signaling.store;

import com.ttstd.signaling.model.AdminAccount;
import com.ttstd.signaling.repository.AdminAccountRepository;
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 JpaAdminStore implements AdminStore {

    private final AdminAccountRepository repository;

    public JpaAdminStore(AdminAccountRepository repository) {
        this.repository = repository;
    }

    @Override
    public AdminAccount save(AdminAccount admin) {
        return repository.save(admin);
    }

    @Override
    public Optional<AdminAccount> findById(String adminId) {
        return repository.findById(adminId);
    }

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

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

    @Override
    public boolean existsById(String adminId) {
        return repository.existsById(adminId);
    }

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

    @Override
    public long count() {
        return repository.count();
    }

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