package com.pandoli365.bibimbap.mapper;

import com.pandoli365.bibimbap.data.OperatorView;
import com.pandoli365.bibimbap.data.UserData;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface UsersMapper {

    @Select("""
            SELECT
                id,
                display_name AS displayName,
                canonical_email AS canonicalEmail,
                avatar_url AS avatarUrl,
                role,
                status,
                last_login_at AS lastLoginAt,
                created_at AS createdAt,
                updated_at AS updatedAt
            FROM users
            WHERE id = #{id}
              AND is_delete IS NOT TRUE
            """)
    UserData getUser(long id);

    @Insert("""
            INSERT INTO users (
                display_name,
                canonical_email,
                avatar_url,
                role,
                status,
                last_login_at
            ) VALUES (
                #{displayName},
                #{canonicalEmail},
                #{avatarUrl},
                #{role},
                #{status},
                #{lastLoginAt}
            )
            """)
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int addUser(UserData user);

    @Update("""
            UPDATE users
            SET
                display_name = #{displayName},
                canonical_email = #{canonicalEmail},
                avatar_url = #{avatarUrl},
                role = #{role},
                status = #{status},
                last_login_at = #{lastLoginAt},
                updated_at = now()
            WHERE id = #{id}
              AND is_delete IS NOT TRUE
            """)
    int updateUser(UserData user);

    @Select("""
            SELECT permissions_epoch
            FROM users
            WHERE id = #{userId}
              AND is_delete IS NOT TRUE
            """)
    long getPermissionsEpoch(@Param("userId") long userId);

    @Update("""
            UPDATE users
            SET permissions_epoch = permissions_epoch + 1,
                updated_at = now()
            WHERE id = #{userId}
              AND is_delete IS NOT TRUE
            """)
    int bumpPermissionsEpoch(@Param("userId") long userId);

    @Update("""
            UPDATE users
            SET role = #{role},
                updated_at = now()
            WHERE id = #{userId}
              AND is_delete IS NOT TRUE
            """)
    int updateRole(@Param("userId") long userId, @Param("role") String role);

    @Select("""
            SELECT id AS "userId",
                   display_name AS "displayName",
                   role
            FROM users
            WHERE role IN ('ADMIN', 'SUBADMIN')
              AND is_delete IS NOT TRUE
            ORDER BY role, id
            """)
    List<OperatorView> listOperators();
}
