<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.datamate.datamanagement.infrastructure.persistence.mapper.DatasetFileMapper">
    <sql id="Base_Column_List">
      id, dataset_id, file_name, file_path, logical_path, version, file_type, file_size, check_sum, tags, metadata, status,
          upload_time, last_access_time, created_at, updated_at
    </sql>

  <select id="findById" parameterType="string"
          resultType="com.datamate.datamanagement.domain.model.dataset.DatasetFile">
        SELECT <include refid="Base_Column_List"/>
        FROM t_dm_dataset_files
        WHERE id = #{id}
    </select>

  <select id="findByDatasetId" parameterType="string"
          resultType="com.datamate.datamanagement.domain.model.dataset.DatasetFile">
        SELECT <include refid="Base_Column_List"/>
        FROM t_dm_dataset_files
        WHERE dataset_id = #{datasetId}
        ORDER BY upload_time DESC
    </select>

  <select id="findByDatasetIdAndStatus" resultType="com.datamate.datamanagement.domain.model.dataset.DatasetFile">
        SELECT <include refid="Base_Column_List"/>
        FROM t_dm_dataset_files
        WHERE dataset_id = #{datasetId}
          AND status = #{status}
        ORDER BY upload_time DESC
    </select>

  <select id="findByDatasetIdAndFileType" resultType="com.datamate.datamanagement.domain.model.dataset.DatasetFile">
        SELECT <include refid="Base_Column_List"/>
        FROM t_dm_dataset_files
        WHERE dataset_id = #{datasetId}
          AND file_type = #{fileType}
        ORDER BY upload_time DESC
    </select>

    <select id="countByDatasetId" parameterType="string" resultType="long">
        SELECT COUNT(*)
        FROM t_dm_dataset_files
        WHERE dataset_id = #{datasetId}
          AND (status IS NULL OR status &lt;&gt; 'ARCHIVED')
    </select>

    <select id="countNonDerivedByDatasetId" parameterType="string" resultType="long">
        SELECT COUNT(*)
        FROM t_dm_dataset_files
        WHERE dataset_id = #{datasetId}
          AND (status IS NULL OR status &lt;&gt; 'ARCHIVED')
          AND (metadata IS NULL OR JSON_EXTRACT(metadata, '$.derived_from_file_id') IS NULL)
    </select>

    <select id="countCompletedByDatasetId" parameterType="string" resultType="long">
        SELECT COUNT(*) FROM t_dm_dataset_files WHERE dataset_id = #{datasetId} AND status = 'COMPLETED'
    </select>

    <select id="sumSizeByDatasetId" parameterType="string" resultType="long">
        SELECT COALESCE(SUM(file_size), 0)
        FROM t_dm_dataset_files
        WHERE dataset_id = #{datasetId}
          AND (status IS NULL OR status &lt;&gt; 'ARCHIVED')
    </select>

   <select id="findByDatasetIdAndFileName" resultType="com.datamate.datamanagement.domain.model.dataset.DatasetFile">
        SELECT <include refid="Base_Column_List"/>
        FROM t_dm_dataset_files
        WHERE dataset_id = #{datasetId}
          AND file_name = #{fileName}
          AND (status IS NULL OR status &lt;&gt; 'ARCHIVED')
        ORDER BY version DESC, upload_time DESC
        LIMIT 1
    </select>

   <select id="findAllByDatasetIdAndLogicalPath" resultType="com.datamate.datamanagement.domain.model.dataset.DatasetFile">
        SELECT <include refid="Base_Column_List"/>
        FROM t_dm_dataset_files
        WHERE dataset_id = #{datasetId}
          AND logical_path = #{logicalPath}
        ORDER BY version DESC, upload_time DESC
    </select>

   <select id="findAllByDatasetId" parameterType="string"
           resultType="com.datamate.datamanagement.domain.model.dataset.DatasetFile">
        SELECT <include refid="Base_Column_List"/>
        FROM t_dm_dataset_files
        WHERE dataset_id = #{datasetId}
        ORDER BY upload_time DESC
    </select>

  <select id="findByCriteria" resultType="com.datamate.datamanagement.domain.model.dataset.DatasetFile">
        SELECT <include refid="Base_Column_List"/>
        FROM t_dm_dataset_files
        WHERE dataset_id = #{datasetId}
        <!-- Replace invalid XML '&&' with 'and' for MyBatis OGNL -->
        <if test="fileType != null and fileType != ''">
            AND file_type = #{fileType}
        </if>
        <if test="status != null and status != ''">
            AND status = #{status}
        </if>
        ORDER BY upload_time DESC
    </select>


    <update id="updateFilePathPrefix">
        UPDATE t_dm_dataset_files
        SET file_path = CONCAT(#{newPrefix}, SUBSTRING(file_path, LENGTH(#{oldPrefix}) + 1))
        WHERE dataset_id = #{datasetId}
          AND file_path LIKE CONCAT(#{oldPrefix}, '%')
    </update>

    <select id="findSourceFileIdsWithDerivedFiles" resultType="java.lang.String">
        SELECT DISTINCT JSON_UNQUOTE(JSON_EXTRACT(metadata, '$.derived_from_file_id')) AS source_file_id
        FROM t_dm_dataset_files
        WHERE dataset_id = #{datasetId}
          AND metadata IS NOT NULL
          AND JSON_EXTRACT(metadata, '$.derived_from_file_id') IS NOT NULL
    </select>

    <select id="countNonDerivedByDatasetIds" resultType="com.datamate.datamanagement.infrastructure.persistence.repository.dto.DatasetFileCount">
        SELECT dataset_id AS datasetId,
               COUNT(*) AS fileCount
        FROM t_dm_dataset_files
        WHERE dataset_id IN
        <foreach collection="datasetIds" item="datasetId" open="(" separator="," close=")">
            #{datasetId}
        </foreach>
          AND (status IS NULL OR status &lt;&gt; 'ARCHIVED')
          AND (metadata IS NULL OR JSON_EXTRACT(metadata, '$.derived_from_file_id') IS NULL)
        GROUP BY dataset_id
    </select>
</mapper>
