package com.smyhub.store.mengyastorebackendjava.repository;

import com.smyhub.store.mengyastorebackendjava.entity.Product;
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 org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface ProductRepository extends JpaRepository<Product, String> {

    List<Product> findByActiveTrueOrderByCreatedAtDesc();

    List<Product> findAllByOrderByCreatedAtDesc();

    @Transactional
    @Modifying
    @Query("UPDATE Product p SET p.totalSold = p.totalSold + :count WHERE p.id = :id")
    void incrementTotalSold(@Param("id") String id, @Param("count") int count);

    @Transactional
    @Modifying
    @Query("UPDATE Product p SET p.viewCount = p.viewCount + 1 WHERE p.id = :id")
    void incrementViewCount(@Param("id") String id);
}
