<?php
if ( ! defined( 'ABSPATH' ) ) exit;

// 注意这里的类名，要和主文件里 YFJ_Module_ + ucfirst('bazi_cesuan') 对应
class YFJ_Module_Bazi_hehun extends YFJ_Base_Module {

    public function __construct() {
        $this->module_id = 'bazi_hehun';             // 1. 修改模块 ID
        $this->shortcode = 'yfj_bazi_hehun';         // 2. 修改短代码
        $this->api_endpoint = '/v1/Bazi/hehun';      // 3. 配置对应的真实 API 接口

        parent::__construct();
    }

    /**
     * 专属数据拦截与加工引擎 (重写基类方法)
     * 在这里可以对 API 返回的原始数据进行格式化、过滤或追加二次计算的自定义数据。
     */
    protected function process_api_data($raw_data) {
        // 1. 确保数据是数组格式，防止报错
        if (is_array($raw_data)) {
            //如果 API 没返回格局，给个默认的兜底值
            if (!isset($raw_data['base_info']['zhengge'])) {
                $raw_data['base_info']['zhengge'] = '未入格';
            }
        }

        //将处理后的数据返回给 views/result.php 进行渲染
        return $raw_data;
    }

    /**
     * 沙盒模式下的模拟数据
     */
    protected function get_demo_data($post_data) {
        //获取当前后台设置的语言
        $lang = get_option('yfj_language', 'zh-cn');

        // 1. 把你的完整 JSON 粘贴到单引号里面（要考虑到zh-cn 和 zh-tw）
        $demo_json_zh_cn = '{"errcode":0,"errmsg":"请求成功","notice":"本次测算结果仅供娱乐使用，请勿用于封建迷信和违法用途。","data":{"male":{"bazi":["戊辰","乙卯","壬戌","丙午"],"gl_year":"1988","gl_month":"3","gl_day":"8","gl_hours":"12","nl_year":"1988","nl_month":"1","nl_day":"21","nl_hours":"12","sex":"乾造","name":"张三","gongli":"1988年3月8日12时20分","nongli":"一九八八年正月廿一日 午时","kw":"子丑","tg_cg_god":["七杀","伤官","日元","偏财"],"dz_cg":["戊|乙|癸","乙","戊|辛|丁","丁|己"],"dz_cg_god":["七杀|伤官|劫财","伤官","七杀|正印|正财","正财|正官"],"day_cs":["墓","死","冠带","胎"],"na_yin":["大林木","大溪水","大海水","天河水"]},"female":{"bazi":["癸未","壬戌","壬午","丙午"],"gl_year":"2003","gl_month":"11","gl_day":"5","gl_hours":"12","nl_year":"2003","nl_month":"10","nl_day":"12","nl_hours":"12","sex":"坤造","name":"李四","gongli":"2003年11月5日12时20分","nongli":"二〇〇三年十月十二日 午时","kw":"申酉","tg_cg_god":["劫财","比肩","日元","偏财"],"dz_cg":["己|丁|乙","戊|辛|丁","丁|己","丁|己"],"dz_cg_god":["正官|正财|伤官","七杀|正印|正财","正财|正官","正财|正官"],"day_cs":["养","冠带","胎","胎"],"na_yin":["杨柳木","大海水","杨柳木","天河水"]},"minggong":{"male_fengshui":"东四命","female_fengshui":"东四命","score":"30","male_minggong":"震","female_minggong":"离","description":"此项总分为30分。说明：以东四命与西四命之说来合，如果相合，那么在购房时，应买与自己命宫相合的房子。","detail_description":"男方命卦为震，适宜住宅方位为东方。女方命卦为离，适宜住宅方位为南方。两者命卦方位均属于：东四命。说明夫妻两人，命宫相合，同四命之间可以互相帮助，以修正特定的煞之威力，并尽量减少不利的影响，并且婚姻中彼此间的爱情可以相互增强，帮助彼此改善关系，从而使双方获得幸福。婚姻也可以让双方的子女获得更好的前程，从而为孩子们给予更多的机会和福利。"},"nianqitongzhi":{"score":"0","male_nian_zhi":"辰","male_nian_zhi_desc":"木","female_nian_zhi":"未","female_nian_zhi_desc":"火","description":"此项总分为20分。说明：如寅卯辰会东方木气，虎兔龙结合的机缘就大于其它属相；巳午未会南方火气，蛇马羊结合的机缘就大于其它属相；申酉戌会西方金气，猴鸡狗结合的机缘就大于其它属相；亥子丑会北方水气，猪鼠牛结合的机缘就大于其它属相。","detail_description":"男方年支为辰，女方年支为未。此局为未拱辰。年支代表父母宫，此局拱合，故可能双方父母会在亲朋好友劝说下，同意这门婚事，男方和女方年支拱合，代表双方能够相互尊重，建立起牢固的联系，并产生忠诚和信任。这是通过协调和妥协的方式解决彼此的不同意见而达成的。此婚姻会非常稳固，也会一生幸福。"},"yueling":{"score":"5","male_yue_zhi":"卯","female_yue_zhi":"戌","description":"此项总分为5分。说明：男女生月相同者互相间也是很有缘份的。","detail_description":"男方月支为卯，女方月支为戌。此局为卯戌合化火。月支代表兄弟宫，此局全六合，故可能双方亲朋好友都会同意这门婚事，月支代表兄弟宫，此局全六合，象征着兄弟姐妹之间的紧密联系。这对夫妻的婚姻如同深厚的兄弟姐妹情谊，充满亲情的温暖。他们在平凡中找到了真正的幸福，彼此之间充满了默契和互相支持。他们的生活虽然平和，但却饱含深情，永远紧密相依。"},"rigan":{"score":"0","detail_description":"男方日干为壬。五行信息：五行属水，谓之纯阳之水；女方日干为壬。五行信息：五行属水，谓之纯阳之水。男女日干五行阴阳不和，男女结合机缘较小，这对夫妻面对着一些挑战，但也正因为这些困难，他们可以更加珍惜彼此的相遇。虽然初时机缘较小，但通过坚持和努力，他们或许会创造出一段克服困难的美丽爱情。","male_yue_zhi":"壬","female_yue_zhi":"壬","description":"此项总分为25分。说明：何谓日干阴阳舒配得所？日干五行相同，一阴一阳的组合男女结合的机缘最大，如甲日干逢乙日干，庚日干逢辛日干之类。"},"tiangan":{"score":"0","male_yue_zhi":"壬","female_yue_zhi":"壬","description":"此项总分为5分。说明：其次是天干五合，如甲日干逢己日干，庚日干逢乙日干之类。再次则是比和或相生。","detail_description":"男方日天干为壬，女方日天干为壬。此局为平。日干代表夫妻宫，此局平，这意味着这对夫妻的婚姻关系充满平静和稳定，他们之间的生活过得宁静和谐。虽然没有过多的戏剧性，但他们在平凡的日子里寻找着幸福，注定会过得平顺而充实。"},"zinv":{"nannv":"女","detail_description":"头胎可能生女，此为报恩女，她将继承家族的温柔和智慧，传承家族的美德和爱。她将成为家庭的一颗明珠，带来更多的欢笑和温情。这个宝贝女孩将在家庭中注入新的希望和幸福，为家庭创造更多美好的时刻。","score":"15","description":"此项总分为15分。说明：何谓子女同步？西方的科学家在探索男女结合的奥秘时提出了【性染色体论】，我们东方人在四柱预测中看头胎子女的性别，男女双方的八字中头胎子女的性别必须一致。"},"all_score":50,"male_sx":"龙","female_sx":"羊","male_xz":"双鱼座","female_xz":"天蝎座"}}';
        $demo_json_zh_tw = '{"errcode":0,"errmsg":"请求成功","notice":"本次测算结果仅供娱乐使用，请勿用于封建迷信和违法用途。","data":{"male":{"bazi":["戊辰","乙卯","壬戌","丙午"],"gl_year":"1988","gl_month":"3","gl_day":"8","gl_hours":"12","nl_year":"1988","nl_month":"1","nl_day":"21","nl_hours":"12","sex":"乾造","name":"張三","gongli":"1988年3月8日12時20分","nongli":"一九八八年正月廿一日 午時","kw":"子丑","tg_cg_god":["七殺","傷官","日元","偏財"],"dz_cg":["戊|乙|癸","乙","戊|辛|丁","丁|己"],"dz_cg_god":["七殺|傷官|劫財","傷官","七殺|正印|正財","正財|正官"],"day_cs":["墓","死","冠帶","胎"],"na_yin":["大林木","大溪水","大海水","天河水"]},"female":{"bazi":["癸未","壬戌","壬午","丙午"],"gl_year":"2003","gl_month":"11","gl_day":"5","gl_hours":"12","nl_year":"2003","nl_month":"10","nl_day":"12","nl_hours":"12","sex":"坤造","name":"李四","gongli":"2003年11月5日12時20分","nongli":"二〇〇三年十月十二日 午時","kw":"申酉","tg_cg_god":["劫財","比肩","日元","偏財"],"dz_cg":["己|丁|乙","戊|辛|丁","丁|己","丁|己"],"dz_cg_god":["正官|正財|傷官","七殺|正印|正財","正財|正官","正財|正官"],"day_cs":["養","冠帶","胎","胎"],"na_yin":["楊柳木","大海水","楊柳木","天河水"]},"minggong":{"male_fengshui":"東四命","female_fengshui":"東四命","score":"30","male_minggong":"震","female_minggong":"離","description":"此項總分為30分。說明：以東四命與西四命之說來合，如果相合，那麼在購屋時，應買與自己命宮相合的房子。","detail_description":"男方命卦為震，適宜住宅方位為東方。女方命卦為離，適宜住宅方位為南方。兩者命卦方位均屬於：東四命。說明夫妻兩人，命宮相合，同四命之間可以互相幫助，以修正特定的煞之威力，並盡量減少不利的影響，並且婚姻中彼此間的愛情可以相互增強，幫助彼此改善關系，從而使雙方獲得幸福。婚姻也可以讓雙方的子女獲得更好的前程，從而為孩子們給予更多的機會和福利。"},"nianqitongzhi":{"score":"0","male_nian_zhi":"辰","male_nian_zhi_desc":"木","female_nian_zhi":"未","female_nian_zhi_desc":"火","description":"此項總分為20分。說明：如寅卯辰會東方木氣，虎兔龍結合的機緣就大於其它屬相；巳午未會南方火氣，蛇馬羊結合的機緣就大於其它屬相；申酉戌會西方金氣，猴雞狗結合的機緣就大於其它屬相；亥子丑會北方水氣，豬鼠牛結合的機緣就大於其它屬相。","detail_description":"男方年支為辰，女方年支為未。此局為未拱辰。年支代表父母宮，此局拱合，故可能雙方父母會在親朋好友勸說下，同意這門婚事，男方和女方年支拱合，代表雙方能夠相互尊重，建立起牢固的聯系，並產生忠誠和信任。這是通過協調和妥協的方式解決彼此的不同意見而達成的。此婚姻會非常穩固，也會一生幸福。"},"yueling":{"score":"5","male_yue_zhi":"卯","female_yue_zhi":"戌","description":"此項總分為5分。說明：男女生月相同者互相間也是很有緣份的。","detail_description":"男方月支為卯，女方月支為戌。此局為卯戌合化火。月支代表兄弟宮，此局全六合，故可能雙方親朋好友都會同意這門婚事，月支代表兄弟宮，此局全六合，象征著兄弟姐妹之間的緊密聯系。這對夫妻的婚姻如同深厚的兄弟姐妹情誼，充滿親情的溫暖。他們在平凡中找到了真正的幸福，彼此之間充滿了默契和互相支持。他們的生活雖然平和，但卻飽含深情，永遠緊密相依。"},"rigan":{"score":"0","detail_description":"男方日干為壬。五行資訊：五行属水，谓之純陽之水；女方日干為壬。五行資訊：五行属水，谓之純陽之水。男女日干五行陰陽不和，男女結合機緣較小，這對夫妻面對著一些挑戰，但也正因為這些困難，他們可以更加珍惜彼此的相遇。雖然初時機緣較小，但通過堅持和努力，他們或許會創造出一段剋服困難的美麗愛情。","male_yue_zhi":"壬","female_yue_zhi":"壬","description":"此項總分為25分。說明：何謂日干陰陽舒配得所？日干五行相同，一陰一陽的組合男女結合的機緣最大，如甲日干逢乙日干，庚日干逢辛日干之類。"},"tiangan":{"score":"0","male_yue_zhi":"壬","female_yue_zhi":"壬","description":"此項總分為5分。說明：其次是天干五合，如甲日干逢己日干，庚日干逢乙日干之類。再次則是比和或相生。","detail_description":"男方日天干為壬，女方日天干為壬。此局為平。日干代表夫妻宮，此局平，這意味著這對夫妻的婚姻關系充滿平靜和穩定，他們之間的生活過得寧靜和諧。雖然沒有過多的戲劇性，但他們在平凡的日子裏尋找著幸福，註定會過得平順而充實。"},"zinv":{"nannv":"女","detail_description":"頭胎可能生女，此為報恩女，她將繼承家族的溫柔和智慧，傳承家族的美德和愛。她將成為家庭的一顆明珠，帶來更多的歡笑和溫情。這個寶貝女孩將在家庭中註入新的希望和幸福，為家庭創造更多美好的時刻。","score":"15","description":"此項總分為15分。說明：何謂子女同步？西方的科學家在探索男女結合的奧秘時提出了【性染色體論】，我們東方人在四柱預測中看頭胎子女的性別，男女雙方的八字中頭胎子女的性別必須一致。"},"all_score":50,"male_sx":"龍","female_sx":"羊","male_xz":"雙魚座","female_xz":"天蠍座"}}';

        if ($lang == 'zh-tw') {
            $demo_json = $demo_json_zh_tw;
        } else {
            $demo_json = $demo_json_zh_cn;
        }

        // 2. 解析为 PHP 数组
        $demo_array_json = json_decode($demo_json, true);

        // 3. 【核心提取】剥离外层的 errcode 状态，只提取 data 节点内容
        $data = $demo_array_json['data'] ?? [];

        // 5. 返回处理好的干净数据给 process_api_data
        return $data;
    }
}