package com.kospo.talk.repository;

import com.kospo.talk.model.Manual;
import com.kospo.talk.model.ManualId;
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 ManualRepository extends JpaRepository<Manual, ManualId> {

    @Query(value="select * from manual where work_id = :workId and type in ('0100', '0300') and delete_yn = false order by ins_date desc, version desc limit 5",  nativeQuery = true)
    List<Manual> findByImageAndVideo(@Param("workId") String workId);

    @Query(value="select * from manual where work_id = :workId and type = '0000' and delete_yn = false order by ins_date desc, version desc limit 5",  nativeQuery = true)
    List<Manual> findByDoc(@Param("workId") String workId);

    Manual findByIdWorkIdAndVersionAndType(String workId, String version, String type);;

    @Modifying
    @Query(value="update manual set delete_yn = true where work_id = :workId and type = :type", nativeQuery = true)
    int updateByWorkIdAndType(@Param("workId") String workId, @Param("type") String type);

    @Query(value = "select a.* from manual a inner join work b  on a.work_id  = b.work_id\n" +
            "and a.delete_yn = false\n"+
            "and (:type is null or :type = '' or a.type = :type)\n"+
            "and (:onm is null or :onm = '' or a.onm like concat('%', :onm, '%'))\n" +
            "and (:workId is null or :workId = '' or b.work_id = :workId) order by a.version desc", nativeQuery = true)
    List<Manual> findByOnmAndWorkId(@Param("onm")String onm, @Param("workId")String workId, @Param("type")String type);

    @Query(value = "select *\n" +
            "from (select *\n" +
            "      from manual\n" +
            "      where type = :type)\n" +
            "where 1 = 1\n" +
            "  and (:onm is null or :onm = '' or onm like concat('%', :onm, '%'))\n" +
            "  and case\n" +
            "          when :onm = '' and :workId != '' then\n" +
            "              work_id = :workId\n" +
            "          else\n" +
            "              true\n" +
            "    end", nativeQuery = true)
    List<Manual> findDocByOnmAndWorkId(String onm, String workId, String type);


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

    @Query(value = "select * from manual where work_id = :workId and onm = :onm and version = :version", nativeQuery = true)
    Manual findByWorkIdAndOnm(@Param("workId") String workId, @Param("onm") String onm, @Param("version") String version);
}