package com.pandoli365.bibimbap.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserPermissionsMapper {

    @Select("""
            SELECT permission_key
            FROM user_permissions
            WHERE user_id = #{userId}
            """)
    List<String> listKeys(long userId);

    @Select("""
            SELECT EXISTS(
                SELECT 1
                FROM user_permissions
                WHERE user_id = #{userId}
                  AND permission_key = #{permissionKey}
            )
            """)
    boolean exists(@Param("userId") long userId, @Param("permissionKey") String permissionKey);

    @Insert("""
            INSERT INTO user_permissions (user_id, permission_key, granted_by)
            VALUES (#{userId}, #{permissionKey}, #{grantedBy})
            """)
    int insert(@Param("userId") long userId, @Param("permissionKey") String permissionKey, @Param("grantedBy") long grantedBy);

    @Delete("""
            DELETE FROM user_permissions
            WHERE user_id = #{userId}
              AND permission_key = #{permissionKey}
            """)
    int delete(@Param("userId") long userId, @Param("permissionKey") String permissionKey);

    @Delete("""
            DELETE FROM user_permissions
            WHERE user_id = #{userId}
            """)
    int deleteAllByUser(long userId);
}
