package com.smyhub.store.mengyastorebackendjava.repository;

import com.smyhub.store.mengyastorebackendjava.entity.Order;
import org.springframework.data.jpa.repository.JpaRepository;
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 OrderRepository extends JpaRepository<Order, String> {

    List<Order> findByUserAccountOrderByCreatedAtDesc(String userAccount);

    List<Order> findAllByOrderByCreatedAtDesc();

    @Query("SELECT COALESCE(SUM(o.quantity), 0) FROM Order o WHERE o.userAccount = :account AND o.productId = :productId AND o.status <> 'cancelled'")
    int sumQuantityByUserAccountAndProductId(@Param("account") String account, @Param("productId") String productId);
}
