package com.devjob.repository;

import com.devjob.domain.Job;
import com.devjob.domain.JobStatus;
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.Query;
import org.springframework.data.repository.query.Param;

/**
 * EP11: Pageable을 받아 Page<Job>을 반환하는 페이징 쿼리 메서드.
 * EP13: company는 LAZY이므로, 목록 조회처럼 company 정보가 항상 필요한 경우
 * fetch join으로 N+1을 방지한 findByStatusWithCompany를 사용한다.
 */
public interface JobRepository extends JpaRepository<Job, Long> {

    Page<Job> findByStatus(JobStatus status, Pageable pageable);

    Page<Job> findByTitleContaining(String title, Pageable pageable);

    @Query(value = """
            SELECT j FROM Job j
            JOIN FETCH j.company
            WHERE j.status = :status
            """,
            countQuery = "SELECT COUNT(j) FROM Job j WHERE j.status = :status")
    Page<Job> findByStatusWithCompany(@Param("status") JobStatus status, Pageable pageable);
}
