package kr.iotdoor.boot.mapper;

import kr.iotdoor.boot.model.contact.ContactSubmission;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ContactMapper {

    @Insert("""
        INSERT INTO TN_CONTACT_INQRY (
            CONTACT_IDNTF_NO,
            NM,
            EML_ADDR,
            TELNO,
            TTL,
            CN,
            SUBMIT_DT
        ) VALUES (
            #{contactIdntfNo},
            #{name},
            #{email},
            #{phone},
            #{subject},
            #{message},
            CURRENT_TIMESTAMP
        )
        """)
    int insertContact(ContactSubmission contactSubmission);

    @Select("""
        SELECT
            CONTACT_IDNTF_NO,
            NM AS NAME,
            EML_ADDR AS EMAIL,
            TELNO AS PHONE,
            TTL AS SUBJECT,
            CN AS MESSAGE,
            SUBMIT_DT AS SUBMITTED_AT
        FROM TN_CONTACT_INQRY
        ORDER BY SUBMIT_DT DESC
        LIMIT #{limit}
        """)
    List<ContactSubmission> selectRecentContacts(@Param("limit") int limit);

    @Select("""
        SELECT
            CONTACT_IDNTF_NO,
            NM AS NAME,
            EML_ADDR AS EMAIL,
            TELNO AS PHONE,
            TTL AS SUBJECT,
            CN AS MESSAGE,
            SUBMIT_DT AS SUBMITTED_AT
        FROM TN_CONTACT_INQRY
        ORDER BY SUBMIT_DT DESC
        """)
    List<ContactSubmission> selectAllContacts();

    @Select("""
        SELECT COUNT(*)
        FROM TN_CONTACT_INQRY
        """)
    int selectContactCount();
}
