<?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.seo.mapper.SeoMapper">

  <resultMap id="metaMap" type="com.zioinfo.cms.seo.CmsSeoMeta">
    <id property="id" column="id"/>
    <result property="pathKey" column="path_key"/>
    <result property="title" column="title"/>
    <result property="description" column="description"/>
    <result property="keywords" column="keywords"/>
    <result property="ogTitle" column="og_title"/>
    <result property="ogImage" column="og_image"/>
    <result property="canonical" column="canonical"/>
    <result property="locale" column="locale"/>
    <result property="updatedAt" column="updated_at"/>
  </resultMap>

  <resultMap id="redirectMap" type="com.zioinfo.cms.seo.CmsRedirect">
    <id property="id" column="id"/>
    <result property="fromPath" column="from_path"/>
    <result property="toPath" column="to_path"/>
    <result property="statusCode" column="status_code"/>
    <result property="active" column="is_active"/>
    <result property="createdAt" column="created_at"/>
  </resultMap>

  <select id="findAllMeta" resultMap="metaMap">SELECT * FROM cms_seo_meta ORDER BY id DESC</select>
  <select id="findMetaByPath" resultMap="metaMap">
    SELECT * FROM cms_seo_meta WHERE path_key = #{pathKey}
    <if test="locale != null and locale != ''">AND locale = #{locale}</if>
    LIMIT 1
  </select>
  <select id="findMetaById" resultMap="metaMap">SELECT * FROM cms_seo_meta WHERE id = #{id}</select>

  <insert id="insertMeta" parameterType="com.zioinfo.cms.seo.CmsSeoMeta" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO cms_seo_meta (path_key, title, description, keywords, og_title, og_image, canonical, locale)
    VALUES (#{pathKey}, #{title}, #{description}, #{keywords}, #{ogTitle}, #{ogImage}, #{canonical}, #{locale})
  </insert>

  <update id="updateMeta" parameterType="com.zioinfo.cms.seo.CmsSeoMeta">
    UPDATE cms_seo_meta SET path_key = #{pathKey}, title = #{title}, description = #{description}, keywords = #{keywords},
      og_title = #{ogTitle}, og_image = #{ogImage}, canonical = #{canonical}, locale = #{locale}, updated_at = NOW()
    WHERE id = #{id}
  </update>

  <delete id="deleteMeta">DELETE FROM cms_seo_meta WHERE id = #{id}</delete>

  <select id="findAllRedirects" resultMap="redirectMap">SELECT * FROM cms_redirect ORDER BY id DESC</select>

  <insert id="insertRedirect" parameterType="com.zioinfo.cms.seo.CmsRedirect" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO cms_redirect (from_path, to_path, status_code, is_active)
    VALUES (#{fromPath}, #{toPath}, #{statusCode}, #{active})
  </insert>

  <delete id="deleteRedirect">DELETE FROM cms_redirect WHERE id = #{id}</delete>

</mapper>
