package com.devjob.job.repository;

import com.devjob.job.domain.Job;
import com.devjob.job.domain.JobStatus;
import java.time.LocalDate;
import java.util.List;
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;

public interface JobRepository extends JpaRepository<Job, Long>, JpaSpecificationExecutor<Job> {

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

    List<Job> findByStatusAndDeadlineBefore(JobStatus status, LocalDate date);

    @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);
}
