package com.pandoli365.bibimbap.mapper;

import com.pandoli365.bibimbap.data.UserAuthIdentityData;
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;

@Mapper
public interface UserAuthIdentitiesMapper {

    @Select("""
            SELECT
                id,
                user_id AS userId,
                provider,
                provider_user_id AS providerUserId,
                email,
                password_hash AS passwordHash,
                display_name AS displayName,
                avatar_url AS avatarUrl,
                last_login_at AS lastLoginAt,
                created_at AS createdAt,
                updated_at AS updatedAt
            FROM user_auth_identities
            WHERE id = #{id}
              AND is_delete IS NOT TRUE
            """)
    UserAuthIdentityData getUserAuthIdentity(long id);

    @Select("""
            SELECT
                id,
                user_id AS userId,
                provider,
                provider_user_id AS providerUserId,
                email,
                password_hash AS passwordHash,
                display_name AS displayName,
                avatar_url AS avatarUrl,
                last_login_at AS lastLoginAt,
                created_at AS createdAt,
                updated_at AS updatedAt
            FROM user_auth_identities
            WHERE provider = #{provider}
              AND provider_user_id = #{providerUserId}
              AND is_delete IS NOT TRUE
            """)
    UserAuthIdentityData getUserAuthIdentityByProvider(
            @Param("provider") String provider,
            @Param("providerUserId") String providerUserId
    );

    @Insert("""
            INSERT INTO user_auth_identities (
                user_id,
                provider,
                provider_user_id,
                email,
                password_hash,
                display_name,
                avatar_url,
                last_login_at
            ) VALUES (
                #{userId},
                #{provider},
                #{providerUserId},
                #{email},
                #{passwordHash},
                #{displayName},
                #{avatarUrl},
                #{lastLoginAt}
            )
            """)
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int addUserAuthIdentity(UserAuthIdentityData userAuthIdentity);

    @Update("""
            UPDATE user_auth_identities
            SET
                email = #{email},
                password_hash = #{passwordHash},
                display_name = #{displayName},
                avatar_url = #{avatarUrl},
                last_login_at = #{lastLoginAt},
                updated_at = now()
            WHERE id = #{id}
              AND is_delete IS NOT TRUE
            """)
    int updateUserAuthIdentity(UserAuthIdentityData userAuthIdentity);
}
