package com.jeesharp.business.modules.setting.application;

import com.jeesharp.business.modules.setting.application.support.MiniHomeShellContentSupport;
import com.jeesharp.business.modules.setting.service.MiniHomeShellService;
import com.jeesharp.business.modules.setting.vo.MiniHomeBannerItemVO;
import com.jeesharp.business.modules.setting.vo.MiniHomeQuickNavItemVO;
import com.jeesharp.business.modules.setting.vo.MiniHomeShellVO;
import com.jeesharp.business.modules.setting.vo.MiniHomeSiteIconItemVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 小程序首页壳层编排：协调 {@link MiniHomeShellService} 与各子域 Service 组装 {@link MiniHomeShellVO}。
 * <p>不在本类开启事务：内部已对子调用做 try/catch 降级，若外层套只读事务，子调用异常会将会话标为
 * rollback-only，提交时出现 {@code Transaction rolled back because it has been marked as rollback-only}。</p>
 *
 * @author jeesharp
 * @since 2026-05-13
 */
@Slf4j
@Service
@AllArgsConstructor
public class MiniHomeShellApplicationService {

    private static final String SHELL_NOTICE_SUFFIX = " · 欢迎莅临杏花村片区 · 请提前预约、文明游览";

    private final MiniHomeShellService miniHomeShellService;
    private final MiniHomeShellContentSupport miniHomeShellContentSupport;

    /**
     * 构建首页壳层数据，供 C 端一次性拉取。
     */
    public MiniHomeShellVO buildShell() {
        MiniHomeShellVO vo = new MiniHomeShellVO();
        vo.setWeather(miniHomeShellService.defaultWeather());
        vo.setBookingGrid(miniHomeShellService.defaultBookingGrid());
        vo.setRankTabLabels(miniHomeShellService.rankTabLabels());
        vo.setQuickNav(resolveQuickNav());
        vo.setHomeIcons(resolveHomeIcons());
        vo.setBanners(resolveBanners());
        vo.setNoticeMarquee(resolveNoticeMarquee());
        return vo;
    }

    private List<MiniHomeQuickNavItemVO> resolveQuickNav() {
        try {
            List<MiniHomeQuickNavItemVO> fromDb = miniHomeShellContentSupport.listQuickNavForShell();
            return fromDb != null ? fromDb : new ArrayList<>();
        } catch (Exception ex) {
            log.warn("首页壳层拉取宫格配置失败，已降级为空列表", ex);
            return new ArrayList<>();
        }
    }

    private List<MiniHomeSiteIconItemVO> resolveHomeIcons() {
        try {
            return miniHomeShellContentSupport.listShellHomeIcons();
        } catch (Exception ex) {
            log.warn("首页壳层拉取站点图标失败，已降级为空列表", ex);
            return new ArrayList<>();
        }
    }

    private List<MiniHomeBannerItemVO> resolveBanners() {
        try {
            return miniHomeShellContentSupport.listHomeBannersForShell();
        } catch (Exception ex) {
            log.warn("首页壳层拉取轮播失败，已降级为空列表（公告等其余字段仍返回）", ex);
            return new ArrayList<>();
        }
    }

    private String resolveNoticeMarquee() {
        try {
            return miniHomeShellContentSupport.buildNoticeMarquee(SHELL_NOTICE_SUFFIX);
        } catch (Exception ex) {
            log.warn("首页壳层拉取最新公告标题失败，已跳过标题仅使用后缀/默认文案", ex);
            return miniHomeShellContentSupport.buildNoticeMarqueeFallback(SHELL_NOTICE_SUFFIX);
        }
    }
}
