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

  <resultMap id="alarmMap" type="com.zioinfo.signage.domain.AlarmVo">
    <id property="id"           column="id"/>
    <result property="tenantCode"   column="tenant_code"/>
    <result property="storeId"      column="store_id"/>
    <result property="storeName"    column="store_name"/>
    <result property="alarmType"    column="alarm_type"/>
    <result property="alarmCode"    column="alarm_code"/>
    <result property="severity"     column="severity"/>
    <result property="message"      column="message"/>
    <result property="status"       column="status"/>
    <result property="resolvedBy"   column="resolved_by"/>
    <result property="resolution"   column="resolution"/>
    <result property="resolvedAt"   column="resolved_at"/>
    <result property="createdAt"    column="created_at"/>
  </resultMap>

  <select id="findAll" resultMap="alarmMap">
    SELECT a.id, a.tenant_code, a.store_id, s.store_name, a.alarm_type, a.alarm_code,
           a.severity, a.message, a.status, a.resolved_by, a.resolution, a.resolved_at, a.created_at
    FROM signage_alarm a
    LEFT JOIN signage_store s ON a.store_id = s.id
    <where>
      <if test="tenantCode != null and tenantCode != ''">AND a.tenant_code = #{tenantCode}</if>
      <if test="storeId != null">AND a.store_id = #{storeId}</if>
      <if test="severity != null and severity != ''">AND a.severity = #{severity}</if>
      <if test="status != null and status != ''">AND a.status = #{status}</if>
    </where>
    ORDER BY a.created_at DESC LIMIT 200
  </select>

  <select id="findById" resultMap="alarmMap">
    SELECT a.id, a.tenant_code, a.store_id, s.store_name, a.alarm_type, a.alarm_code,
           a.severity, a.message, a.status, a.resolved_by, a.resolution, a.resolved_at, a.created_at
    FROM signage_alarm a
    LEFT JOIN signage_store s ON a.store_id = s.id
    WHERE a.id = #{id}
  </select>

  <insert id="insert" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO signage_alarm (tenant_code, store_id, alarm_type, alarm_code, severity, message, status)
    VALUES (#{tenantCode}, #{storeId}, #{alarmType}, #{alarmCode}, #{severity}, #{message}, 'OPEN')
  </insert>

  <update id="update">
    UPDATE signage_alarm SET
      alarm_type = #{alarmType},
      severity   = #{severity},
      message    = #{message},
      status     = #{status}
    WHERE id = #{id}
  </update>

  <update id="resolve">
    UPDATE signage_alarm SET
      status      = 'RESOLVED',
      resolved_by = #{resolvedBy},
      resolution  = #{resolution},
      resolved_at = NOW()
    WHERE id = #{id}
  </update>

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

  <select id="countUnresolved" resultType="long">
    SELECT COUNT(*) FROM signage_alarm WHERE status != 'RESOLVED'
    <if test="tenantCode != null and tenantCode != ''">AND tenant_code = #{tenantCode}</if>
  </select>

  <select id="countCritical" resultType="long">
    SELECT COUNT(*) FROM signage_alarm WHERE severity = 'CRITICAL' AND status != 'RESOLVED'
    <if test="tenantCode != null and tenantCode != ''">AND tenant_code = #{tenantCode}</if>
  </select>

  <select id="countAll" resultType="long">
    SELECT COUNT(*) FROM signage_alarm
    <where>
      <if test="tenantCode != null and tenantCode != ''">AND tenant_code = #{tenantCode}</if>
    </where>
  </select>

</mapper>
