<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zioinfo.cms.theme.mapper.ThemeMapper">

  <resultMap id="themeMap" type="com.zioinfo.cms.theme.CmsTheme">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="layout" column="layout"/>
    <result property="tokens" column="tokens"/>
    <result property="active" column="is_active"/>
    <result property="createdBy" column="created_by"/>
    <result property="createdAt" column="created_at"/>
  </resultMap>

  <select id="findAll" resultMap="themeMap">SELECT * FROM cms_theme ORDER BY id DESC</select>
  <select id="findById" resultMap="themeMap">SELECT * FROM cms_theme WHERE id = #{id}</select>
  <select id="findActive" resultMap="themeMap">SELECT * FROM cms_theme WHERE is_active = TRUE ORDER BY id DESC LIMIT 1</select>

  <insert id="insert" parameterType="com.zioinfo.cms.theme.CmsTheme" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO cms_theme (name, layout, tokens, is_active, created_by)
    VALUES (#{name}, #{layout}::jsonb, #{tokens}::jsonb, #{active}, #{createdBy})
  </insert>

  <update id="update" parameterType="com.zioinfo.cms.theme.CmsTheme">
    UPDATE cms_theme SET name = #{name}, layout = #{layout}::jsonb, tokens = #{tokens}::jsonb WHERE id = #{id}
  </update>

  <update id="deactivateAll">UPDATE cms_theme SET is_active = FALSE</update>
  <update id="activate">UPDATE cms_theme SET is_active = TRUE WHERE id = #{id}</update>
  <delete id="delete">DELETE FROM cms_theme WHERE id = #{id}</delete>

</mapper>
