package com.itsol.quantrivanphong.manager.employee.repository;

import com.itsol.quantrivanphong.model.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface EmployeeRepository  extends JpaRepository<Employee,Integer > {
    public Employee findEmployeeByUsername(String username);

    public Employee findEmployeeByEmailAddress(String emailAddress);

    public Employee findEmployeeByUsernameAndPassword(String username, String password);

    Employee findByEmailAddress(String emailAddress);



    //==================================================================================================================
    //Emplyee Repository Of Hieunv

    @Query(value = "SELECT e.username FROM employee e JOIN eproject ep ON e.id = ep.employee_id JOIN project p  ON ep.project_id = p.id WHERE p.id = ?1 AND ep.position = 'team_lead' GROUP BY e.id",nativeQuery = true)
    String findTeamLeader(int projectId);

    @Query(value = "SELECT COUNT(e.id)FROM employee e JOIN eproject ep ON e.id = ep.employee_id JOIN project p  ON ep.project_id = p.id WHERE p.id = ?1 AND ep.position = 'member'", nativeQuery = true)
    int numberOfMember(int projectId);


    @Query(value = "SELECT * FROM employee e JOIN eproject ep ON e.id = ep.employee_id JOIN project p on p.id = ep.project_id JOIN timesheet ts ON ep.id = ts.eproject_id WHERE p.id = ?1 AND ts.status = false AND ts.created_at Like ?2%", nativeQuery = true)
    List<Employee> listLackOfReport(int projectId, String createDate);

    @Query(value = "SELECT * FROM employee e JOIN eproject ep on e.id = ep.employee_id JOIN timesheet ts on ep.id = ts.eproject_id WHERE ts.status = false AND created_at >= ?1 AND created_at <= ?2 AND ep.project_id = ?3 GROUP BY e.id", nativeQuery =  true)
    List<Employee> listEmployeeLackOfReport(String firstDate, String finalDate, int projectId);

    @Query(value = "SELECT COUNT(ts.id) FROM timesheet ts JOIN eproject ep ON ep.id = ts.eproject_id JOIN employee e ON e.id = ep.employee_id WHERE ts.status = false AND created_at >= ?1 AND created_at <= ?2 AND ep.project_id = ?3 AND e.id = ?4", nativeQuery = true)
    int numberLack(String firstDate, String finalDate, int projectId, int employeeId);

    //==================================================================================================================
    //Emplyee Repository Of PhiCho
    Employee findEmployeeById(int id);

}
