package com.ylx.massage.mapper;

import org.junit.jupiter.api.Test;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TechnicianMomentMapperXmlTest {

    @Test
    public void baseResultMapMapsRecommendStatus() throws Exception {
        String xml = readMapperXml();

        assertTrue(xml.contains("column=\"is_recommend\" property=\"isRecommend\""));
    }

    @Test
    public void baseResultMapMapsListStatus() throws Exception {
        String xml = readMapperXml();

        assertTrue(xml.contains("column=\"is_list\" property=\"isList\""));
    }

    @Test
    public void recommendedMomentsOnlyReturnMarkedRecommendations() throws Exception {
        String xml = readMapperXml();

        assertTrue(xml.contains("selectRecommendedMoments"));
        assertTrue(xml.contains("AND tm.audit_status = 2"));
        assertTrue(xml.contains("AND tm.visible_range = 1"));
        assertTrue(xml.contains("AND tm.is_recommend = 1"));
        assertTrue(xml.contains("AND tm.is_list = 1"));
    }

    @Test
    public void updateRecommendStatusOnlyUpdatesNormalMoment() throws Exception {
        String xml = readMapperXml();

        assertTrue(xml.contains("update id=\"updateRecommendStatus\""));
        assertTrue(xml.contains("is_recommend = #{isRecommend}"));
        assertTrue(xml.contains("update_time = NOW()"));
        assertTrue(xml.contains("WHERE"));
        assertTrue(xml.contains("id = #{momentId}"));
        assertTrue(xml.contains("AND status = 1"));
    }

    @Test
    public void updateListStatusOnlyUpdatesNormalMoment() throws Exception {
        String xml = readMapperXml();

        assertTrue(xml.contains("update id=\"updateListStatus\""));
        assertTrue(xml.contains("is_list = #{isList}"));
        assertTrue(xml.contains("update_time = NOW()"));
        assertTrue(xml.contains("WHERE"));
        assertTrue(xml.contains("id = #{momentId}"));
        assertTrue(xml.contains("AND status = 1"));
    }

    @Test
    public void publicMomentQueriesOnlyReturnListedMoments() throws Exception {
        String xml = readMapperXml();

        assertTrue(xml.contains("selectSameCityMoments"));
        assertTrue(xml.contains("selectNearbyMoments"));
        assertTrue(xml.contains("getMerchantProject"));
        assertTrue(xml.split("AND tm.is_list = 1", -1).length - 1 >= 3);
        assertTrue(xml.contains("AND is_list = 1"));
    }

    @Test
    public void manageAuditStatusCountCountsOnlyManageStatuses() throws Exception {
        String xml = readMapperXml();

        assertTrue(xml.contains("selectManageAuditStatusCount"));
        assertTrue(xml.contains("audit_status = 1"));
        assertTrue(xml.contains("audit_status = 2"));
        assertTrue(xml.contains("audit_status = 3"));
        assertTrue(xml.contains("audit_status IN (1, 2, 3)"));
        assertTrue(xml.contains("is_delete = 0"));
    }

    @Test
    public void simpleDetailExcludesDeletedMedia() throws Exception {
        String xml = readMapperXml();

        assertTrue(xml.contains("selectMomentSimpleDetail"));
        assertTrue(xml.contains("LEFT JOIN t_moment_media b ON a.id = b.moment_id AND IFNULL(b.is_delete, 0) = 0"));
    }

    private String readMapperXml() throws Exception {
        InputStream inputStream = getClass().getClassLoader()
                .getResourceAsStream("mapper/moment/TechnicianMomentMapper.xml");
        assertNotNull(inputStream, "TechnicianMomentMapper.xml should exist");
        byte[] bytes = new byte[inputStream.available()];
        int read = inputStream.read(bytes);
        assertTrue(read > 0, "TechnicianMomentMapper.xml should not be empty");
        return new String(bytes, StandardCharsets.UTF_8);
    }
}
