package com.honey.honey.repository;

import com.honey.honey.model.SupportTicket;
import com.honey.honey.model.SupportTicket.TicketStatus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface SupportTicketRepository extends JpaRepository<SupportTicket, Long>, JpaSpecificationExecutor<SupportTicket> {
    
    /**
     * Count OPENED tickets for a user.
     */
    long countByUserIdAndStatus(Integer userId, TicketStatus status);
    
    /**
     * Count tickets by status (for admin dashboard).
     */
    long countByStatus(TicketStatus status);
    
    /**
     * Find tickets by user ID, ordered by created_at DESC.
     */
    Page<SupportTicket> findByUserIdOrderByCreatedAtDesc(Integer userId, Pageable pageable);
    
    /**
     * Find a ticket by ID and user ID (for security - users can only access their own tickets).
     */
    Optional<SupportTicket> findByIdAndUserId(Long id, Integer userId);
    
    /**
     * Find all tickets ordered by created_at DESC (for admin panel).
     */
    Page<SupportTicket> findAllByOrderByCreatedAtDesc(Pageable pageable);
    
    /**
     * Find tickets by status, ordered by created_at DESC (for admin panel).
     */
    Page<SupportTicket> findByStatusOrderByCreatedAtDesc(TicketStatus status, Pageable pageable);
}



