from django.core.management.base import BaseCommand
import json
from iching.utils.bzshagod import (
    tian_yi_at, tian_de_at, yue_de_at, taiji_at, lushen_at, wenchang_at,
    yima_at, taohua_at, hongluan_at, tianxi_at, jiangxing_at, fuxing_at,
    huagai_at, jiesha_at, wangshen_at, yangren_at, guchen_at, guashu_at,
    tianyi_at, yinchayangcuo_at, zaisha_at, gejiao_at, feiren_at, shieda_at, liuxiu_at,
    jinyu_at, gShagodNames
)
from iching.utils.bz import gGodstem, gEarthstem

class Command(BaseCommand):
    help = 'Test the *_at methods for sha gods'

    def add_arguments(self, parser):
        parser.add_argument('--god', type=int, help='God stem index (0-9)', default=None)
        parser.add_argument('--earth', type=int, help='Earth branch index (0-11)', default=None)
        parser.add_argument('--test', type=str, help='Specific test to run (e.g., tian_yi, lushen)', default='all')

    def handle(self, *args, **options):
        if options['test'] == 'all':
            self._run_all_tests(options)
        else:
            test_method = getattr(self, f"_test_{options['test']}", None)
            if test_method:
                test_method(options)
            else:
                self.stdout.write(self.style.ERROR(f"Test {options['test']} not found"))
                self.stdout.write("Available tests: tian_yi, tian_de, yue_de, taiji, lushen, wenchang, "
                                 "yima, taohua, hongluan, tianxi, jiangxing, fuxing, huagai, jiesha, "
                                 "wangshen, yangren, guchen, guashu, tianyi, yinchayangcuo, zaisha, "
                                 "feiren, shieda, liuxiu, jinyu")
    
    def _run_all_tests(self, options):
        # Run all tests for the given god/earth
        for test in ['tian_yi', 'tian_de', 'yue_de', 'taiji', 'lushen', 'wenchang',
                    'yima', 'taohua', 'hongluan', 'tianxi', 'jiangxing', 'fuxing',
                    'huagai', 'jiesha', 'wangshen', 'yangren', 'guchen', 'guashu',
                    'tianyi', 'yinchayangcuo', 'zaisha', 'feiren', 'shieda', 'liuxiu',
                    'jinyu']:
            test_method = getattr(self, f"_test_{test}", None)
            if test_method:
                self.stdout.write(self.style.NOTICE(f"\n--- Testing {test} ---"))
                test_method(options)
    
    def _test_tian_yi(self, options):
        if options['god'] is not None:
            god_index = options['god']
            result = tian_yi_at(god_index, 'g', 'd', force_ymdh=True)
            self.stdout.write(f"Testing for 天乙贵人 with god stem {gGodstem[god_index]}")
            if result:
                for tian_yi_type in [0, 1]:
                    if tian_yi_type in result:
                        earth = result[tian_yi_type]
                        type_str = "阳贵人" if tian_yi_type == 1 else "阴贵人"
                        self.stdout.write(f"{type_str}: {gEarthstem[earth]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all god stems
            for idx in range(10):
                result = tian_yi_at(idx)
                if result:
                    for tian_yi_type in [0, 1]:
                        if tian_yi_type in result:
                            earth = result[tian_yi_type]
                            type_str = "阳贵人" if tian_yi_type == 1 else "阴贵人"
                            self.stdout.write(f"God {gGodstem[idx]} forms 天乙贵人 ({type_str}) with: {gEarthstem[earth]}")
    
    def _test_tian_de(self, options):
        if options['earth'] is not None:
            earth_index = options['earth']
            result = tian_de_at(earth_index)
            self.stdout.write(f"Testing for 天德贵人 with month earth {gEarthstem[earth_index]}")
            if result:
                if 'g' in result:
                    self.stdout.write(f"Found in god: {gGodstem[result['g']]}")
                if 'e' in result:
                    self.stdout.write(f"Found in earth: {gEarthstem[result['e']]}")
            else:
                self.stdout.write("No mapping found")
            
            # Test direct checking functionality
            self.stdout.write("\nTesting direct checks:")
        else:
            # Test all earth branches
            for idx in range(12):
                result = tian_de_at(month_earth=idx, ge='e', ymdh='m', force_ymdh=False)
                if result:
                    if "e" in result:
                        tiande =  f"earth {gEarthstem[result['e']]}"
                    else:
                        tiande = f"god {gGodstem[result['g']]}"
                    self.stdout.write(f"Earth {gEarthstem[idx]} forms 天德贵人 with month {tiande}")
                else:
                    self.stdout.write(f"No mapping found for 天德贵人 at {gEarthstem[idx]}")
    
    def _test_yue_de(self, options):
        if options['earth'] is not None:
            earth_index = options['earth']
            result = yue_de_at(earth_index)
            self.stdout.write(f"Testing for 月德贵人 with month earth {gEarthstem[earth_index]}")
            if result is not None:
                self.stdout.write(f"Month god: {gGodstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all earth branches
            for e_idx in range(12):
                result = yue_de_at(e_idx, ge='e')
                if result is not None:
                    self.stdout.write(f"Testing for 月德贵人 with month earth {gEarthstem[e_idx]}, god: {gGodstem[result]}")
                else:
                    self.stdout.write(f"No mapping found for 月德贵人 at {gEarthstem[e_idx]}")
    
    def _test_taiji(self, options):
        if options['god'] is not None:
            god_index = options['god']
            result = taiji_at(god_index)
            self.stdout.write(f"Testing for 太极贵人 with day god {gGodstem[god_index]}")
            if result:
                self.stdout.write("Earth branches: " + ", ".join([gEarthstem[e] for e in result]))
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all god
            for g_idx in range(10):
                result = taiji_at(g_idx, ge='g')
                if result is not None:
                    self.stdout.write(f"Testing for 太极贵人 with day god {gGodstem[g_idx]}, earth: " + "".join([gEarthstem[e] for e in result]))
                else:
                    self.stdout.write(f"No mapping found for 太极贵人 at {gGodstem[g_idx]}")
    
    def _test_lushen(self, options):
        if options['god'] is not None:
            god_index = options['god']
            result = lushen_at(god_index)
            self.stdout.write(f"Testing for 禄神 with day god {gGodstem[god_index]}")
            if result is not None:
                self.stdout.write(f"Earth branch: {gEarthstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all god
            for g_idx in range(10):
                result = lushen_at(g_idx, ge='g')
                if result is not None:
                    self.stdout.write(f"Testing for 禄神 with day god {gGodstem[g_idx]}, earth: {gEarthstem[result]}")
                else:
                    self.stdout.write(f"No mapping found for 禄神 at {gGodstem[g_idx]}")
    
    def _test_wenchang(self, options):
        if options['god'] is not None:
            god_index = options['god']
            result = wenchang_at(god_index)
            self.stdout.write(f"Testing for 文昌 with day god {gGodstem[god_index]}")
            if result is not None:
                self.stdout.write(f"Earth branch: {gEarthstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all god
            for g_idx in range(10):
                result = wenchang_at(g_idx, ge='g', ymdh='d', force_ymdh=True)
                if result is not None:
                    self.stdout.write(f"Testing for 文昌 with day god {gGodstem[g_idx]}, earth: {gEarthstem[result]}")
                else:
                    self.stdout.write(f"No mapping found for 文昌 at {gGodstem[g_idx]}")
    
    def _test_yima(self, options):
        if options['earth'] is not None:
            earth_index = options['earth']
            result = yima_at(earth_index)
            self.stdout.write(f"Testing for 驿马 with day earth {gEarthstem[earth_index]}")
            if result is not None:
                self.stdout.write(f"Earth branch: {gEarthstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all earth
            for e_idx in range(12):
                result = yima_at(e_idx, ge='e', ymdh='d', force_ymdh=True)
                if result is not None:
                    self.stdout.write(f"Testing for 驿马 with day earth {gEarthstem[e_idx]}, earth: {gEarthstem[result]}")
                else:
                    self.stdout.write(f"No mapping found for 驿马 at {gEarthstem[e_idx]}")
                
    def _test_taohua(self, options):
        if options['earth'] is not None:
            earth_index = options['earth']
            result = taohua_at(earth_index)
            self.stdout.write(f"Testing for 桃花 with day earth {gEarthstem[earth_index]}")
            if result is not None:
                self.stdout.write(f"Earth branch: {gEarthstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all earth
            for e_idx in range(12):
                result = taohua_at(e_idx, ge='e', ymdh='d', force_ymdh=True)
                if result is not None:
                    self.stdout.write(f"Testing for 桃花 with day earth {gEarthstem[e_idx]}, earth: {gEarthstem[result]}")
                else:
                    self.stdout.write(f"No mapping found for 桃花 at {gEarthstem[e_idx]}")
    
    def _test_hongluan(self, options):
        if options['earth'] is not None:
            earth_index = options['earth']
            result = hongluan_at(earth_index)
            self.stdout.write(f"Testing for 红鸾 with year earth {gEarthstem[earth_index]}")
            if result is not None:
                self.stdout.write(f"Earth branch: {gEarthstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all earth
            for e_idx in range(12):
                result = hongluan_at(e_idx, ge='e', ymdh='y', force_ymdh=True)
                if result is not None:
                    self.stdout.write(f"Testing for 红鸾 with year earth {gEarthstem[e_idx]}, earth: {gEarthstem[result]}")
                else:
                    self.stdout.write(f"No mapping found for 红鸾 at {gEarthstem[e_idx]}")
    
    def _test_tianxi(self, options):
        if options['earth'] is not None:
            earth_index = options['earth']
            result = tianxi_at(earth_index)
            self.stdout.write(f"Testing for 天喜 with year earth {gEarthstem[earth_index]}")
            if result is not None:
                self.stdout.write(f"Earth branch: {gEarthstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all earth
            for e_idx in range(12):
                result = tianxi_at(e_idx)
                if result is not None:
                    self.stdout.write(f"Testing for 天喜 with year earth {gEarthstem[e_idx]}, earth: {gEarthstem[result]}")
                else:
                    self.stdout.write("No mapping found")
    
    def _test_jiangxing(self, options):
        if options['earth'] is not None:
            earth_index = options['earth']
            result = jiangxing_at(earth_index)
            self.stdout.write(f"Testing for 将星 with day earth {gEarthstem[earth_index]}")
            if result is not None:
                self.stdout.write(f"Earth branch: {gEarthstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all earth
            for e_idx in range(12):
                result = jiangxing_at(e_idx)
                if result is not None:
                    self.stdout.write(f"Testing for 将星 with day earth {gEarthstem[e_idx]}, earth: {gEarthstem[result]}")
                else:
                    self.stdout.write("No mapping found")
    
    def _test_fuxing(self, options):
        if options['god'] is not None:
            god_index = options['god']
            result = fuxing_at(god_index)
            self.stdout.write(f"Testing for 福星 with day/year god {gGodstem[god_index]}")
            if result:
                self.stdout.write("Earth branches: " + ", ".join([gEarthstem[e] for e in result]))
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all god
            for g_idx in range(10):
                result = fuxing_at(g_idx)
                if result is not None:
                    self.stdout.write(f"Testing for 福星 with day god {gGodstem[g_idx]}, earth: " + "".join([gEarthstem[e] for e in result]))
                else:
                    self.stdout.write("No mapping found")
    
    def _test_huagai(self, options):
        if options['earth'] is not None:
            earth_index = options['earth']
            result = huagai_at(earth_index)
            self.stdout.write(f"Testing for 华盖 with day earth {gEarthstem[earth_index]}")
            if result is not None:
                self.stdout.write(f"Earth branch: {gEarthstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all earth
            for e_idx in range(12):
                result = huagai_at(e_idx)
                if result is not None:
                    self.stdout.write(f"Testing for 华盖 with day earth {gEarthstem[e_idx]}, earth: {gEarthstem[result]}")
                else:
                    self.stdout.write("No mapping found")
    
    def _test_jiesha(self, options):
        if options['earth'] is not None:
            earth_index = options['earth']
            result = jiesha_at(earth_index)
            self.stdout.write(f"Testing for 劫煞 with day earth {gEarthstem[earth_index]}")
            if result is not None:
                self.stdout.write(f"Earth branch: {gEarthstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all earth
            for e_idx in range(12):
                result = jiesha_at(e_idx)
                if result is not None:
                    self.stdout.write(f"Testing for 劫煞 with day earth {gEarthstem[e_idx]}, earth: {gEarthstem[result]}")
                else:
                    self.stdout.write("No mapping found")
    
    def _test_wangshen(self, options):
        if options['earth'] is not None:
            earth_index = options['earth']
            result = wangshen_at(earth_index)
            self.stdout.write(f"Testing for 亡神 with day earth {gEarthstem[earth_index]}")
            if result is not None:
                self.stdout.write(f"Earth branch: {gEarthstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all earth
            for e_idx in range(12):
                result = wangshen_at(e_idx)
                if result is not None:
                    self.stdout.write(f"Testing for 亡神 with day earth {gEarthstem[e_idx]}, earth: {gEarthstem[result]}")
                else:
                    self.stdout.write("No mapping found")
    
    def _test_yangren(self, options):
        if options['god'] is not None:
            god_index = options['god']
            result = yangren_at(god_index)
            self.stdout.write(f"Testing for 羊刃 with day god {gEarthstem[god_index]}")
            if result is not None:
                self.stdout.write(f"Earth branch: {gEarthstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all god
            for g_idx in range(10):
                result = yangren_at(g_idx)
                if result is not None:
                    self.stdout.write(f"Testing for 羊刃 with day god {gGodstem[g_idx]}, earth: {gEarthstem[result]}")
                else:
                    self.stdout.write("No mapping found")
    
    def _test_guchen(self, options):
        if options['earth'] is not None:
            earth_index = options['earth']
            result = guchen_at(earth_index)
            self.stdout.write(f"Testing for 孤辰 with year earth {gEarthstem[earth_index]}")
            if result is not None:
                self.stdout.write(f"Earth branch: {gEarthstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all earth
            for e_idx in range(12):
                result = guchen_at(e_idx)
                if result is not None:
                    self.stdout.write(f"Testing for 孤辰 with year earth {gEarthstem[e_idx]}, earth: {gEarthstem[result]}")
                else:
                    self.stdout.write("No mapping found")
    
    def _test_guashu(self, options):
        if options['earth'] is not None:
            earth_index = options['earth']
            result = guashu_at(earth_index)
            self.stdout.write(f"Testing for 寡宿 with year earth {gEarthstem[earth_index]}")
            if result is not None:
                self.stdout.write(f"Earth branch: {gEarthstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all earth
            for e_idx in range(12):
                result = guashu_at(e_idx)
                if result is not None:
                    self.stdout.write(f"Testing for 寡宿 with year earth {gEarthstem[e_idx]}, earth: {gEarthstem[result]}")
                else:
                    self.stdout.write("No mapping found")
    
    def _test_tianyi(self, options):
        if options['earth'] is not None:
            earth_index = options['earth']
            result = tianyi_at(earth_index)
            self.stdout.write(f"Testing for 天医 with month earth {gEarthstem[earth_index]}")
            if result is not None:
                self.stdout.write(f"Earth branch: {gEarthstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all earth
            for e_idx in range(12):
                result = tianyi_at(e_idx)
                if result is not None:
                    self.stdout.write(f"Testing for 天医 with month earth {gEarthstem[e_idx]}, earth: {gEarthstem[result]}")
                else:
                    self.stdout.write("No mapping found")
    
    def _test_yinchayangcuo(self, options):
        if options['god'] is not None:
            god_index = options['god']
            result = yinchayangcuo_at(god_index)
            self.stdout.write(f"Testing for 阴差阳错 with day god {gGodstem[god_index]}")
            if result is not None:
                self.stdout.write(f"Earth branch: {gEarthstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all god
            for g_idx in range(10):
                result = yinchayangcuo_at(g_idx)
                if result is not None:
                    self.stdout.write(f"Testing for 阴差阳错 with day god {gGodstem[g_idx]}, earth: " + "".join([gEarthstem[e] for e in result]))
                else:
                    self.stdout.write(f"No mapping found for day god {gGodstem[g_idx]}")
    
    def _test_zaisha(self, options):
        if options['earth'] is not None:
            earth_index = options['earth']
            result = zaisha_at(earth_index)
            self.stdout.write(f"Testing for 灾煞 with year earth {gEarthstem[earth_index]}")
            if result is not None:
                self.stdout.write(f"Earth branch: {gEarthstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all earth
            for e_idx in range(12):
                result = zaisha_at(e_idx)
                if result is not None:
                    self.stdout.write(f"Testing for 灾煞 with year earth {gEarthstem[e_idx]}, earth: {gEarthstem[result]}")
                else:
                    self.stdout.write("No mapping found")
    
    def _test_gejiao(self, options):
        if options['earth'] is not None:
            earth_index = options['earth']
            result = gejiao_at(earth_index)
            self.stdout.write(f"Testing for 隔角 with day earth {gEarthstem[earth_index]}")
            if result is not None:
                self.stdout.write(f"Earth branch: {gEarthstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all earth
            for e_idx in range(12):
                result = gejiao_at(e_idx)
                if result is not None:
                    self.stdout.write(f"Testing for 隔角 with day earth {gEarthstem[e_idx]}, hour earth: {gEarthstem[result]}")
                else:
                    self.stdout.write("No mapping found")
    
    def _test_feiren(self, options):
        if options['god'] is not None:
            god_index = options['god']
            result = feiren_at(god_index)
            self.stdout.write(f"Testing for 飞刃 with day god {gGodstem[god_index]}")
            if result is not None:
                self.stdout.write(f"Earth branch: {gEarthstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all god
            for g_idx in range(10):
                result = feiren_at(g_idx)
                if result is not None:
                    self.stdout.write(f"Testing for 飞刃 with day god {gGodstem[g_idx]}, earth: {gEarthstem[result]}")
                else:
                    self.stdout.write(f"No mapping found for day god {gGodstem[g_idx]}")
    
    def _test_shieda(self, options):
        if options['god'] is not None:
            god_index = options['god']
            result = shieda_at(god_index)
            self.stdout.write(f"Testing for 十恶大败 with day god {gGodstem[god_index]}")
            if result is not None:
                self.stdout.write(f"Earth branch: {gEarthstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all god
            for g_idx in range(10):
                result = shieda_at(g_idx)
                if result is not None:
                    self.stdout.write(f"Testing for 十恶大败 with day god {gGodstem[g_idx]}, earth: {gEarthstem[result]}")
                else:
                    self.stdout.write(f"No mapping found for day god {gGodstem[g_idx]}")
    
    def _test_liuxiu(self, options):
        if options['god'] is not None:
            god_index = options['god']
            result = liuxiu_at(god_index)
            self.stdout.write(f"Testing for 六秀 with day god {gGodstem[god_index]}")
            if result is not None:
                self.stdout.write(f"Earth branch: {gEarthstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all god
            for g_idx in range(10):
                result = liuxiu_at(g_idx)
                if result is not None:
                    self.stdout.write(f"Testing for 六秀 with day god {gGodstem[g_idx]}, earth: " + "".join([gEarthstem[e] for e in result]))
                else:
                    self.stdout.write(f"No mapping found for day god {gGodstem[g_idx]}")
    
    def _test_jinyu(self, options):
        if options['god'] is not None:
            god_index = options['god']
            result = jinyu_at(god_index)
            self.stdout.write(f"Testing for 金舆 with day god {gGodstem[god_index]}")
            if result is not None:
                self.stdout.write(f"Earth branch: {gEarthstem[result]}")
            else:
                self.stdout.write("No mapping found")
        else:
            # Test all god
            for g_idx in range(10):
                result = jinyu_at(g_idx)
                if result is not None:
                    self.stdout.write(f"Testing for 金舆 with day god {gGodstem[g_idx]}, earth: {gEarthstem[result]}")
                else:
                    self.stdout.write(f"No mapping found for day god {gGodstem[g_idx]}")