/*
**************************************************************************************
* Program Name  : PurchaseOrderRepository.java
* Author        : Kawade Swapnali
* Date          : Feb 14, 2026
* System Name   : gswbs
* SRF No.       : 
* Purpose       : To perform database operations for Purchase Order module
*                 such as Fetch, Auto-Id support, and Duplicate validation.
*
* Fields        : purchaseOrderId (PK),
*                 purchaseOrderNo,
*                 vendorName,
*                 vendorGSTNo,
*                 vendorAddress,
*                 requestedBy,
*                 purchaseOrderStatus,
*                 assetType,
*                 assetMake,
*                 assetModel,
*                 description,
*                 quantity,
*                 unitPrice,
*                 gstPercentage,
*                 gstAmount,
*                 subTotal,
*                 grandTotal,
*                 purchaseOrderCreateDate,
*                 deliveryLocation,
*                 expectedDate,
*                 loginId,
*                 status
*
* Functions     : findByPurchaseOrderIdAndLoginId(),
*                 findTopByPrefix(),
*                 findAllByLoginId(),
*                 checkDuplicateRecords()
*
* Called From   : PurchaseOrderServiceImpl
* Beans Accessed: PurchaseOrder (Entity), purchaseOrder_info (Table)
**************************************************************************************
*/
package com.coder.purchase.order.repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import org.springframework.data.domain.Pageable;
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 com.coder.purchase.order.model.PurchaseOrder;

@Repository
public interface PurchaseOrderRepository extends JpaRepository<PurchaseOrder, String> {

    /*
     * FETCH SINGLE PURCHASE ORDER ID BY PURCHASE ORDER ID AND CREATED BY
     */
    Optional<PurchaseOrder> findByPurchaseOrderIdAndCreatedBy(String purchaseOrderId, String createdBy);

    /*
     * FIND LAST AssetType ID FOR AUTO GENERATION
     * Example: ATY/2026/001
     */
    @Query("SELECT p.purchaseOrderId FROM PurchaseOrder p " +
           "WHERE p.purchaseOrderId LIKE CONCAT(:prefix, '%') " +
           "ORDER BY p.purchaseOrderId DESC")
    List<String> findTopByPrefix(
            @Param("prefix") String prefix,
            Pageable pageable
    );

    /*
     * FETCH ALL AssetType BY Login ID
     */
    List<PurchaseOrder> findAllByCreatedBy(String createdBy);

    /*
     * CHECK DUPLICATE AssetType NAME LOGIN-WISE
     */
    @Query("SELECT p FROM PurchaseOrder p " +
    	       "WHERE p.purchaseOrderNumber = :purchaseOrderNumber " +
    	       "AND p.createdBy = :createdBy " +
    	       "AND p.createdDate = :createdDate")
    	Optional<PurchaseOrder> checkDuplicateRecords(
    	        @Param("purchaseOrderNumber") String purchaseOrderNumber,
    	        @Param("createdBy") String createdBy,
    	        @Param("createdDate") LocalDateTime createdDate);
}

