package com.kospo.talk.repository;

import com.kospo.talk.model.User;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface UserRepository extends JpaRepository<User, String> {
    User findBySabun(String sabun);

    @Query(value = "WITH client AS (select *\n" +
            "                from users\n" +
            "                where exists(select 1\n" +
            "                                 from sm_user\n" +
            "                                 where sm_user.sabun = users.sabun))\n" +
            "select sabun\n" +
            "     , name\n" +
            "     , dept_cd\n" +
            "     , ptsn_nm\n" +
            "     , grade_cd\n" +
            "     , grade_nm\n" +
            "     , ofce_phn\n" +
            "     , mbl_phn\n" +
            "     , state\n" +
            "     , online_yn\n" +
            "     , stress\n" +
            "from client\n" +
            "where sabun = :sabun", nativeQuery = true)
    User findSmBySabun(@Param("sabun") String sabun);

    @Query(value = "WITH client AS (select *\n" +
            "                from users\n" +
            "                where not exists(select 1\n" +
            "                                 from sm_user\n" +
            "                                 where sm_user.sabun = users.sabun))\n" +
            "select sabun\n" +
            "     , name\n" +
            "     , dept_cd\n" +
            "     , ptsn_nm\n" +
            "     , grade_cd\n" +
            "     , grade_nm\n" +
            "     , ofce_phn\n" +
            "     , mbl_phn\n" +
            "     , state\n" +
            "     , online_yn\n" +
            "     , stress\n" +
            "from client\n" +
            "where sabun = :sabun", nativeQuery = true)
    User findClientBySabun(@Param("sabun") String sabun);

    @Query(value="select a.* from users a inner join role b on a.sabun = b.sabun\n" +
            "where b.role_id = 'ROLE_SD' and a.online_yn = true", nativeQuery = true)
    List<User> findAllSD();

    @Query(value="select a.*, c.work_nm from users a inner join sm_user c on a.sabun = c.sabun\n" +
            "and c.work_id = :workId", nativeQuery = true)
    List<User> findSM(@Param("workId") String workId);

    @Query(value="select a.* from users a inner join sm_user c on a.sabun = c.sabun\n" +
            "and c.work_id = :workId and c.sabun = :sabun", nativeQuery = true)
    User findByWorkIdAndSabun(String workId, String sabun);

    @Query(value="select a.* from users a inner join sm_user c on a.sabun = c.sabun\n" +
            "and c.work_id = :workId and a.online_yn = true", nativeQuery = true)
    List<User> findOnlineSM(@Param("workId") String workId);

    @Transactional
    @Query(value="update users set online_yn = false", nativeQuery = true)
    @Modifying
    int allOffline();
}