package com.jeesharp.business.modules.setting.service.impl;

import com.jeesharp.business.modules.setting.client.QWeatherNowClient;
import com.jeesharp.business.modules.setting.service.MiniHomeShellService;
import com.jeesharp.business.modules.setting.vo.MiniHomeBookingCellVO;
import com.jeesharp.business.modules.setting.vo.MiniHomeWeatherVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 小程序首页壳层静态配置领域服务实现；天气文案在开启和风配置时拉取实况，否则使用内置默认。
 *
 * @author jeesharp
 * @since 2026-05-15
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class MiniHomeShellServiceImpl implements MiniHomeShellService {

    private static final String SHELL_WEATHER_LINE1 = "小雨 22℃";
    private static final String SHELL_WEATHER_LINE2 = "东北风 3级 · 杏花村景区";
    private static final List<String> SHELL_RANK_TAB_LABELS = List.of("酒店", "民宿", "美食餐饮", "景点必玩");

    private final QWeatherNowClient qWeatherNowClient;

    @Override
    public MiniHomeWeatherVO defaultWeather() {
        try {
            MiniHomeWeatherVO fromQ = qWeatherNowClient.fetchNowOrNull();
            if (fromQ != null) {
                return fromQ;
            }
        } catch (Exception ex) {
            log.warn("首页天气实况降级为默认文案", ex);
        }
        MiniHomeWeatherVO w = new MiniHomeWeatherVO();
        w.setLine1(SHELL_WEATHER_LINE1);
        w.setLine2(SHELL_WEATHER_LINE2);
        return w;
    }

    @Override
    public List<MiniHomeBookingCellVO> defaultBookingGrid() {
        List<MiniHomeBookingCellVO> cells = new ArrayList<>(4);
        cells.add(bookingCell("travel_entrance_1", "景区景点", "门票预约·分时入园",
            "/static/images/home-hero-default.png", "/package-views/pages/ticket/index"));
        cells.add(bookingCell("travel_entrance_2", "美食餐饮", "特色店铺·在线预订",
            "/static/images/home-hero-default.png", "/package-views/pages/food/index"));
        cells.add(bookingCell("travel_entrance_3", "酒店民宿", "舒适旅居随心选",
            "/static/banner/2.png", "/package-views/pages/hotel/index"));
        cells.add(bookingCell("travel_entrance_4", "跟团游", "线路规划·团建研学",
            "/static/banner/3.png", ""));
        return cells;
    }

    @Override
    public List<String> rankTabLabels() {
        return new ArrayList<>(SHELL_RANK_TAB_LABELS);
    }

    private static MiniHomeBookingCellVO bookingCell(String id, String title, String subtitle, String image, String url) {
        MiniHomeBookingCellVO cell = new MiniHomeBookingCellVO();
        cell.setId(id);
        cell.setTitle(title);
        cell.setSubtitle(subtitle);
        cell.setImage(image);
        cell.setUrl(url);
        return cell;
    }
}
