package com.pandoli365.bibimbap.mapper;

import com.pandoli365.bibimbap.data.PermissionData;
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 PermissionsMapper {

    @Insert("""
            INSERT INTO permissions (permission_key, display_name, is_active)
            VALUES (#{permissionKey}, #{displayName}, true)
            ON CONFLICT (permission_key) DO UPDATE
            SET display_name = EXCLUDED.display_name,
                is_active = true
            """)
    int upsert(@Param("permissionKey") String permissionKey, @Param("displayName") String displayName);

    @Select("""
            SELECT
                permission_key AS "permissionKey",
                display_name AS "displayName",
                is_active AS "isActive"
            FROM permissions
            WHERE is_active = true
            ORDER BY id
            """)
    List<PermissionData> listActive();

    @Select("""
            SELECT permission_key
            FROM permissions
            WHERE is_active = true
            """)
    List<String> listActiveKeys();
}
