package com.smyhub.infogenie.infogeniebackendjava.repository;

import com.smyhub.infogenie.infogeniebackendjava.entity.SiteFeatureCardClick;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SiteFeatureCardClickRepository
        extends JpaRepository<SiteFeatureCardClick, SiteFeatureCardClick.ClickId> {

    List<SiteFeatureCardClick> findBySection(String section);

    @Modifying
    @Query(value = """
            INSERT INTO site_feature_card_clicks (section, item_id, click_count, updated_at)
            VALUES (:section, :itemId, 1, NOW())
            ON DUPLICATE KEY UPDATE click_count = click_count + 1, updated_at = NOW()
            """, nativeQuery = true)
    void upsertIncrement(@Param("section") String section, @Param("itemId") String itemId);
}
