package net.educoder.ecsonar.dao;

import net.educoder.ecsonar.model.IssuesMetrics;
import net.educoder.ecsonar.model.Metrics;
import net.educoder.ecsonar.model.Project;
import net.educoder.ecsonar.model.Rule;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

@Repository
@Mapper
public interface ProjectDao {

    @Select("select * from projects where name = #{name}")
    Project findByName(@Param("name")String name);


    @Select("select COALESCE(value,0) from project_measures where component_uuid = #{uuid}  and metric_id = 19 order by id desc limit 1")
    Float getComplexity(@Param("uuid") String uuid);

    @Select("select COALESCE(value,0) from project_measures where component_uuid = #{uuid}  and metric_id = 1 order by id desc limit 1")
    Integer getLines(@Param("uuid") String uuid);

    @Select("select text_value from project_measures where component_uuid = #{uuid}  and metric_id = #{metric_id} order by id desc limit 1")
    String getLevel(@Param("uuid") String uuid, @Param("metric_id") int metric_id);

    @Select("SELECT count(*) FROM issues where project_uuid=#{uuid} and " +
            "status='OPEN' and issue_type=#{issue_type} and severity=#{severity}")
    int getIndicator(@Param("uuid") String uuid, @Param("issue_type") int issue_type, @Param("severity") String severity);


    @Select("select " +
            "(select COALESCE(value,0) complexity from project_measures where component_uuid = #{projectUuid}  and metric_id = 19 order by id desc limit 1)," +
            "(select COALESCE(value,0) lines from project_measures where component_uuid = #{projectUuid}  and metric_id = 1 order by id desc limit 1)," +
            "(SELECT COALESCE(count(*),0) block_code_smells FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=1 and severity='BLOCKER'),        " +
            "(SELECT COALESCE(count(*),0) critical_code_smells FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=1 and severity='CRITICAL')," +
            "(SELECT COALESCE(count(*),0) major_code_smells FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=1 and severity='MAJOR')," +
            "(SELECT COALESCE(count(*),0) minor_code_smells FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=1 and severity='MINOR')," +
            "(select text_value code_smells from project_measures where component_uuid = #{projectUuid}  and metric_id = 80 order by id desc limit 1)," +
            "(SELECT COALESCE(count(*),0) block_bugs FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=2 and severity='BLOCKER'),        " +
            "(SELECT COALESCE(count(*),0) critical_bugs FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=2 and severity='CRITICAL')," +
            "(SELECT COALESCE(count(*),0) major_bugs FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=2 and severity='MAJOR')," +
            "(SELECT COALESCE(count(*),0) minor_bugs FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=2 and severity='MINOR')," +
            "(select text_value bugs from project_measures where component_uuid = #{projectUuid}  and metric_id = 89 order by id desc limit 1)," +
            "(SELECT COALESCE(count(*),0) block_violations FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=3 and severity='BLOCKER'),        " +
            "(SELECT COALESCE(count(*),0) critical_violations FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=3 and severity='CRITICAL')," +
            "(SELECT COALESCE(count(*),0) major_violations FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=3 and severity='MAJOR')," +
            "(SELECT COALESCE(count(*),0) minor_violations FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=3 and severity='MINOR')," +
            "(select text_value violations from project_measures where component_uuid = #{projectUuid}  and metric_id = 93 order by id desc limit 1)")
    Metrics selectMetricsByProjectUuid(String projectUuid);




    @Select("select (select count(1) from issues where project_uuid=#{projectUuid} and issue_type=1) codeSmell," +
            "(select count(1) from issues where project_uuid=#{projectUuid} and issue_type=2) bugs," +
            "(select count(1) from issues where project_uuid=#{projectUuid} and issue_type=3) vulnerability," +
            "(select value from project_measures pm where component_uuid=#{projectUuid} and metric_id=3) codeLines")
    IssuesMetrics selectIssuesMetrics(String projectUuid);

    /**
     * 查询rule
     * @param id
     * @return
     */
    Rule findRuleById(@Param("id") Integer id);
}
