package com.yaoai.domain.mapper;

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

import java.util.List;

@Mapper
public interface ShotAssetMapper extends BaseMapper<ShotAsset> {

    @Select("SELECT * FROM shot_assets WHERE storyboard_id=#{storyboardId} AND is_active=1 ORDER BY created_at DESC")
    List<ShotAsset> findActiveByStoryboard(Long storyboardId);

    @Select("SELECT * FROM shot_assets WHERE storyboard_id=#{storyboardId} AND tenant_id=#{tenantId} ORDER BY asset_type, asset_version DESC, created_at DESC")
    List<ShotAsset> findByStoryboard(@Param("storyboardId") Long storyboardId, @Param("tenantId") Long tenantId);

    @Select("""
            SELECT * FROM shot_assets
            WHERE storyboard_id=#{storyboardId}
              AND tenant_id=#{tenantId}
              AND asset_type='video'
              AND is_active=1
              AND status='ready'
              AND external_url IS NOT NULL
              AND external_url <> ''
            ORDER BY asset_version DESC, created_at DESC
            LIMIT 1
            """)
    ShotAsset findActiveReadyVideoByStoryboard(@Param("storyboardId") Long storyboardId,
                                               @Param("tenantId") Long tenantId);

    @Select("""
            SELECT a.* FROM shot_assets a
            LEFT JOIN storyboards s ON a.storyboard_id = s.id
            LEFT JOIN ai_tasks t ON a.source_task_id = t.id AND a.tenant_id = t.tenant_id
            WHERE a.episode_id = #{episodeId}
              AND a.tenant_id = #{tenantId}
              AND a.asset_type = 'video'
              AND a.is_active = 1
              AND a.status = 'ready'
              AND a.external_url IS NOT NULL
              AND a.external_url <> ''
              AND (a.source_task_id IS NULL OR t.id IS NOT NULL)
            ORDER BY COALESCE(s.sequence_num, 9999), a.created_at
            """)
    List<ShotAsset> findActiveReadyVideosByEpisode(@Param("episodeId") Long episodeId,
                                                   @Param("tenantId") Long tenantId);

    @Select("SELECT * FROM shot_assets WHERE storyboard_id=#{storyboardId} AND tenant_id=#{tenantId} AND asset_type=#{assetType} ORDER BY asset_version DESC, created_at DESC")
    List<ShotAsset> findHistory(@Param("storyboardId") Long storyboardId,
                                @Param("tenantId") Long tenantId,
                                @Param("assetType") String assetType);

    @Select("SELECT COALESCE(MAX(asset_version), 0) FROM shot_assets WHERE storyboard_id=#{storyboardId} AND tenant_id=#{tenantId} AND asset_type=#{assetType}")
    Integer maxVersion(@Param("storyboardId") Long storyboardId,
                       @Param("tenantId") Long tenantId,
                       @Param("assetType") String assetType);

    @Select("SELECT * FROM shot_assets WHERE source_task_id=#{sourceTaskId} AND tenant_id=#{tenantId} LIMIT 1")
    ShotAsset findBySourceTask(@Param("sourceTaskId") Long sourceTaskId, @Param("tenantId") Long tenantId);

    @Update("UPDATE shot_assets SET is_active=0 WHERE storyboard_id=#{storyboardId} AND tenant_id=#{tenantId} AND asset_type=#{assetType} AND is_active=1")
    int deactivateActive(@Param("storyboardId") Long storyboardId,
                         @Param("tenantId") Long tenantId,
                         @Param("assetType") String assetType);

    @Update("UPDATE shot_assets SET is_active=0 WHERE source_task_id=#{sourceTaskId} AND tenant_id=#{tenantId} AND is_active=1")
    int deactivateBySourceTask(@Param("sourceTaskId") Long sourceTaskId, @Param("tenantId") Long tenantId);
}
