package com.zioinfo.kintex.system.loginslide;

import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/** 로그인 슬라이드 매퍼. 노출 순서 권위 = created_at DESC(최신 등록 먼저). */
@Mapper
public interface LoginSlideMapper {

    String COLS = "id, image_url AS \"imageUrl\", title, caption, sort_order AS \"sortOrder\", "
            + "is_active AS active, to_char(created_at, 'YYYY-MM-DD HH24:MI') AS \"createdAt\", "
            + "to_char(updated_at, 'YYYY-MM-DD HH24:MI') AS \"updatedAt\"";

    /** 공개 노출용 — 활성만, 최신 등록순 최대 {limit}건. */
    @Select("SELECT " + COLS + " FROM login_slide WHERE is_active = true "
            + "ORDER BY created_at DESC, id DESC LIMIT #{limit}")
    List<Map<String, Object>> findActiveLatest(@Param("limit") int limit);

    /** 관리자 목록 — 전체, 최신 등록순. */
    @Select("SELECT " + COLS + " FROM login_slide ORDER BY created_at DESC, id DESC")
    List<Map<String, Object>> findAll();

    @Select("SELECT " + COLS + " FROM login_slide WHERE id = #{id}")
    Map<String, Object> findById(@Param("id") long id);

    @Insert("INSERT INTO login_slide (image_url, title, caption, sort_order, is_active) "
            + "VALUES (#{imageUrl}, #{title}, #{caption}, COALESCE(#{sortOrder}, 0), true)")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insert(Map<String, Object> p);

    @Update("UPDATE login_slide SET title = #{title}, caption = #{caption}, "
            + "sort_order = COALESCE(#{sortOrder}, sort_order), "
            + "is_active = COALESCE(#{active}, is_active), updated_at = now() WHERE id = #{id}")
    int update(Map<String, Object> p);

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