package com.dji.sample.manage.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.dji.sample.manage.model.dto.LoginLogDTO;
import com.dji.sample.manage.model.dto.UserDTO;
import com.dji.sample.manage.model.dto.UserListDTO;
import com.dji.sample.manage.model.dto.UserLoginDTO;
import com.dji.sample.manage.model.entity.UserEntity;
import com.dji.sample.manage.model.param.ChangePasswordParam;
import com.dji.sample.manage.model.param.searchParam.LoginLogSearchParam;
import com.dji.sample.manage.model.param.searchParam.UserSearchParam;
import com.dji.sdk.common.HttpResultResponse;
import com.dji.sdk.common.PaginationData;

import java.util.Optional;

public interface IUserService extends IService<UserEntity> {

    /**
     * Query user's details based on username.
     * @param username
     * @param workspaceId
     * @return
     */
    HttpResultResponse getUserByUsername(String username, String workspaceId);



    /**
     * get user login log
     *
     * @param userId 用户ID
     * @param roleType 角色类型
     *
     * @return 结果
     */
    PaginationData<LoginLogDTO> getUserLoginLog(LoginLogSearchParam param, String userId, Integer roleType, int page, int pageSize);


    /**
     * Verify the username and password to log in.
     * @param username
     * @param password
     * @param flag
     * @return
     */
    HttpResultResponse userLogin(String username, String password, Integer flag);

    /**
     * Verify the username and password to log in.
     * @param loginDTO
     * @return
     */
    HttpResultResponse userLogin(UserLoginDTO loginDTO);

    /**
     * Create a user object containing a new token.
     * @param token
     * @return
     */
    Optional<UserDTO> refreshToken(String token);

    /**
     * Create a user object containing a new token.
     * @param token
     * @return
     */
    Optional<UserDTO> changeOrgRefreshToken(String token, String orgId);

    /**
     * Query information about all users in a workspace.
     * @param workspaceId   uuid
     * @return
     */
    PaginationData<UserListDTO> getUsersByWorkspaceId(long page, long pageSize, String workspaceId);

    /**
     * Query information about all users.
     * @param workspaceId   uuid
     * @return
     */
    PaginationData<UserListDTO> getUsers(UserSearchParam param, String workspaceId, long page, long pageSize);

    Boolean updateUser(String workspaceId, String userId, UserListDTO user);

    Boolean deleteUser(String workspaceId, String userId);

    Boolean addUser(String workspaceId, UserEntity user);

    /**
     * Query user's details based on userId
     * @param username
     * @param workspaceId
     * @return
     */
    HttpResultResponse getUserByUserId(String username, String workspaceId);

    UserEntity addWorkspaceAdminUser(UserEntity user);

    UserEntity addOrgAdminUser(UserEntity user);

    /**
     * Change the current user's own password.
     * Enforces: old-password verification, strength rules, history check.
     *
     * @param userId current user id (from token)
     * @param param  ChangePasswordParam containing oldPassword / newPassword / confirmPassword
     * @return result
     */
    HttpResultResponse<Object> changePassword(String userId, ChangePasswordParam param);

    /**
     * Admin resets another user's password.
     * Enforces: strength rules, history check.
     *
     * @param targetUserId user whose password will be reset
     * @param param        ChangePasswordParam containing newPassword
     * @return result
     */
    HttpResultResponse<Object> resetPassword(String targetUserId, ChangePasswordParam param);

    /**
     * Admin changes a specific user's password directly.
     * Requires admin role. Enforces: strength rules, history check.
     *
     * @param targetUserId user whose password will be changed
     * @param param        ChangePasswordParam containing newPassword
     * @return result
     */
    HttpResultResponse<Object> changePasswordByAdmin(String targetUserId, ChangePasswordParam param);

    /**
     * Check whether the given user's password has expired.
     *
     * @param userId user id
     * @return true if password is expired
     */
    boolean isPasswordExpired(String userId);

}
