package com.yaoai.domain.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yaoai.domain.entity.Storyboard;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface StoryboardMapper extends BaseMapper<Storyboard> {

    @Select("SELECT * FROM storyboards WHERE episode_id=#{episodeId} AND tenant_id=#{tenantId} ORDER BY sequence_num ASC")
    List<Storyboard> findByEpisode(Long episodeId, Long tenantId);

    @Select("SELECT * FROM storyboards WHERE project_id=#{projectId} AND tenant_id=#{tenantId} ORDER BY episode_id ASC, sequence_num ASC")
    List<Storyboard> findByProject(Long projectId, Long tenantId);

    @Select("SELECT COALESCE(MAX(sequence_num),0) FROM storyboards WHERE episode_id=#{episodeId}")
    int maxSequenceByEpisode(Long episodeId);

    @Select("SELECT COUNT(*) FROM storyboards WHERE episode_id=#{episodeId} AND tenant_id=#{tenantId}")
    int countByEpisode(Long episodeId, Long tenantId);

    @Select("SELECT * FROM storyboards WHERE episode_id=#{episodeId} AND tenant_id=#{tenantId} " +
            "AND (video_prompt IS NULL OR TRIM(video_prompt) = '') ORDER BY sequence_num ASC")
    List<Storyboard> findMissingVideoPromptByEpisode(Long episodeId, Long tenantId);

    @Update("UPDATE storyboards SET sequence_num=#{seq} WHERE id=#{id} AND tenant_id=#{tenantId}")
    int updateSequence(Long id, int seq, Long tenantId);
}
