package com.yaoai.domain.mapper;

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

import java.util.List;

@Mapper
public interface CharacterStateMapper extends BaseMapper<CharacterState> {

    @Select("""
            SELECT cs.*, c.name AS character_name
            FROM character_states cs
            LEFT JOIN characters c ON c.id = cs.character_id
            WHERE cs.project_id=#{projectId} AND cs.tenant_id=#{tenantId}
            ORDER BY c.created_at ASC, cs.created_at ASC
            """)
    List<CharacterState> findByProject(Long projectId, Long tenantId);

    @Select("""
            SELECT cs.*, c.name AS character_name
            FROM character_states cs
            LEFT JOIN characters c ON c.id = cs.character_id
            WHERE cs.project_id=#{projectId}
              AND cs.character_id=#{characterId}
              AND cs.tenant_id=#{tenantId}
            ORDER BY cs.created_at ASC
            """)
    List<CharacterState> findByCharacter(Long projectId, Long characterId, Long tenantId);

    @Delete("DELETE FROM character_states WHERE character_id=#{characterId} AND tenant_id=#{tenantId}")
    int deleteByCharacter(Long characterId, Long tenantId);
}
