package cn.nianxx.thhotel.platform.identity.repository;

import cn.nianxx.thhotel.platform.identity.domain.PlatformUserEntity;
import cn.nianxx.thhotel.platform.identity.domain.PlatformUserSessionEntity;
import cn.nianxx.thhotel.platform.identity.mapper.PlatformUserMapper;
import cn.nianxx.thhotel.platform.identity.mapper.PlatformUserSessionMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import java.util.Optional;
import org.springframework.stereotype.Repository;

/**
 * 基于 MyBatis-Plus 的平台用户与 session 仓储实现。
 */
@Repository
public class MybatisPlatformIdentityRepository implements PlatformIdentityRepository {

    private final PlatformUserMapper userMapper;
    private final PlatformUserSessionMapper sessionMapper;

    public MybatisPlatformIdentityRepository(
            PlatformUserMapper userMapper,
            PlatformUserSessionMapper sessionMapper) {
        this.userMapper = userMapper;
        this.sessionMapper = sessionMapper;
    }

    @Override
    public Optional<PlatformUserEntity> findUserByUsername(String username) {
        return Optional.ofNullable(userMapper.selectOne(Wrappers.<PlatformUserEntity>lambdaQuery()
                .eq(PlatformUserEntity::getUsername, username)
                .last("LIMIT 1")));
    }

    @Override
    public Optional<PlatformUserEntity> findUserById(Long userId) {
        if (userId == null) {
            return Optional.empty();
        }
        return Optional.ofNullable(userMapper.selectById(userId));
    }

    @Override
    public long countSuperAdminUsers() {
        return userMapper.selectCount(Wrappers.<PlatformUserEntity>lambdaQuery()
                .eq(PlatformUserEntity::getSuperAdmin, true));
    }

    @Override
    public void insertUser(PlatformUserEntity user) {
        userMapper.insert(user);
    }

    @Override
    public void updateUser(PlatformUserEntity user) {
        userMapper.updateById(user);
    }

    @Override
    public void insertSession(PlatformUserSessionEntity session) {
        sessionMapper.insert(session);
    }

    @Override
    public Optional<PlatformUserSessionEntity> findSessionByTokenHash(String tokenHash) {
        return Optional.ofNullable(sessionMapper.selectOne(Wrappers.<PlatformUserSessionEntity>lambdaQuery()
                .eq(PlatformUserSessionEntity::getTokenHash, tokenHash)
                .last("LIMIT 1")));
    }

    @Override
    public void updateSession(PlatformUserSessionEntity session) {
        sessionMapper.updateById(session);
    }
}
