package com.pandoli365.bibimbap.mapper;

import com.pandoli365.bibimbap.data.UnityFeedSourceData;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
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 UnityFeedSourcesMapper {

    @Select("""
            SELECT
                id,
                name,
                feed_url AS feedUrl,
                is_active AS isActive,
                last_polled_at AS lastPolledAt,
                last_seen_guid AS lastSeenGuid,
                last_error AS lastError,
                created_at AS createdAt
            FROM unity_feed_sources
            WHERE is_active = true
            ORDER BY id ASC
            """)
    List<UnityFeedSourceData> listActive();

    @Select("""
            SELECT
                id,
                name,
                feed_url AS feedUrl,
                is_active AS isActive,
                last_polled_at AS lastPolledAt,
                last_seen_guid AS lastSeenGuid,
                last_error AS lastError,
                created_at AS createdAt
            FROM unity_feed_sources
            ORDER BY id ASC
            """)
    List<UnityFeedSourceData> listAll();

    @Select("""
            SELECT
                id,
                name,
                feed_url AS feedUrl,
                is_active AS isActive,
                last_polled_at AS lastPolledAt,
                last_seen_guid AS lastSeenGuid,
                last_error AS lastError,
                created_at AS createdAt
            FROM unity_feed_sources
            WHERE id = #{id}
            """)
    UnityFeedSourceData getById(@Param("id") long id);

    @Insert("""
            INSERT INTO unity_feed_sources (
                name,
                feed_url,
                is_active
            ) VALUES (
                #{name},
                #{feedUrl},
                #{isActive}
            )
            """)
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insert(UnityFeedSourceData s);

    @Update("""
            UPDATE unity_feed_sources
            SET last_seen_guid = #{lastSeenGuid},
                last_polled_at = now(),
                last_error = NULL
            WHERE id = #{id}
            """)
    int updateCursor(@Param("id") long id, @Param("lastSeenGuid") String lastSeenGuid);

    @Update("""
            UPDATE unity_feed_sources
            SET last_error = #{lastError},
                last_polled_at = now()
            WHERE id = #{id}
            """)
    int updateError(@Param("id") long id, @Param("lastError") String lastError);

    @Update("""
            UPDATE unity_feed_sources
            SET is_active = NOT is_active
            WHERE id = #{id}
            """)
    int toggle(@Param("id") long id);

    @Delete("""
            DELETE FROM unity_feed_sources
            WHERE id = #{id}
            """)
    int delete(@Param("id") long id);
}
