package com.kospo.talk.repository;

import com.kospo.talk.model.Work;
import com.kospo.talk.model.WorkS;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface WorkRepository extends JpaRepository<Work, String> {
    Work findByWorkId(String workId);
    @Query(value="select * from work where work_id  = (select work_id from talk where talk_id = :talkId)", nativeQuery = true)
    Work findByTalkId(@Param("talkId") String talkId);
    @Query(value = "select *\n" +
            "from work\n" +
            "where COALESCE(p_work_id, '') = :pWorkId \n" +
            "and use_yn = true", nativeQuery = true)
    List<Work> findByWorkDepth(@Param("pWorkId") String pWorkId);

    @Query(value = "with recursive find_work(work_id, p_work_id, gubun, work_nm, use_yn, path)\n" +
            "                   as (select work_id, p_work_id, gubun, work_nm, use_yn, CAST(Array [work_nm] as text[]) as path\n" +
            "                       from work\n" +
            "                       where 1 = 1\n" +
            "                         and use_yn = true\n" +
            "                         and gubun = '2'\n" +
            "                       union all\n" +
            "                       select work.work_id,\n" +
            "                              work.p_work_id,\n" +
            "                              work.gubun,\n" +
            "                              work.work_nm,\n" +
            "                              work.use_yn,\n" +
            "                              CAST(find_work.path || work.work_nm as text[])\n" +
            "                       from find_work\n" +
            "                                inner join work on find_work.work_id = work.p_work_id)\n" +
            "select *\n" +
            "from (select work_id, work_nm, p_work_id, gubun, use_yn, array_to_string(path, ' -> ') as work_full_nm\n" +
            "      from find_work\n" +
            "      where gubun = '3')\n" +
            "where 1 = 1\n" +
            "  and work_id in (select work_id from sm_user where name like  '%' || :name || '%')\n" +
            "  or work_full_nm like '%' || :name || '%' order by work_nm", nativeQuery = true)
    List<Work> findByWorkSearch(@Param("name") String name);

    @Query(value= "select b.* from sm_user a inner join work b\n" +
            "on a.work_id = b.work_id\n" +
            "and a.sabun = :sabun", nativeQuery = true)
    List<Work> findBySabun(@Param("sabun") String sabun);

    @Query(value = "with recursive workR as (\n" +
            "    select work_id, work_nm, p_work_id, use_yn, gubun\n" +
            "    from work\n" +
            "    where work_id = :workId\n" +
            "    union all\n" +
            "    select work.work_id, work.work_nm, work.p_work_id, work.use_yn, work.gubun\n" +
            "    from work\n" +
            "    inner join workR on workR.p_work_id = work.work_id\n" +
            "\n" +
            ") select * from workR\n" +
            "order by gubun asc", nativeQuery = true)
    List<Work> findByAlLDepth(@Param("workId") String workId);
}