from django.core.management.base import BaseCommand
from datetime import datetime
from iching.utils.bz import getDateTimeGodEarthStem
from iching.utils.bzshagod import calcBaziShaGods, gShagodNames, gGoodGods, gBadGods

class Command(BaseCommand):
    help = 'Calculate and display sha gods for a given date and time'

    def add_arguments(self, parser):
        parser.add_argument('year', type=int, help='Year (e.g., 2024)')
        parser.add_argument('month', type=int, help='Month (1-12)')
        parser.add_argument('day', type=int, help='Day (1-31)')
        parser.add_argument('--hour', type=int, default=0, help='Hour (0-23)')
        parser.add_argument('--minute', type=int, default=0, help='Minute (0-59)')

    def handle(self, *args, **options):
        year = options['year']
        month = options['month']
        day = options['day']
        hour = options['hour']
        minute = options['minute']

        # Get BaZi data using getDateTimeGodEarthStem
        bazi_data = getDateTimeGodEarthStem(year, month, day, hour, minute)

        # Calculate sha gods
        sha_gods_result = calcBaziShaGods(bazi_data)

        # Print results in a formatted way
        self.stdout.write(self.style.SUCCESS(f'\nBaZi Sha Gods for {year}-{month:02d}-{day:02d} {hour:02d}:{minute:02d}\n'))
        
        for pillar, gods in sha_gods_result.items():
            if gods:
                self.stdout.write(f'\n{pillar.upper()} Pillar:')
                # Sort gods by kindness (0: good, 1: bad, 2: neutral)
                gods_by_type = {
                    'Good Gods': [],
                    'Bad Gods': [],
                    'Neutral Gods': []
                }
                
                for god_idx, kindness in gods:
                    god_name = gShagodNames[god_idx]
                    if kindness == 0:
                        gods_by_type['Good Gods'].append(god_name)
                    elif kindness == 1:
                        gods_by_type['Bad Gods'].append(god_name)
                    else:
                        gods_by_type['Neutral Gods'].append(god_name)
                
                # Display gods by type
                for type_name, god_list in gods_by_type.items():
                    if god_list:
                        if type_name == 'Good Gods':
                            self.stdout.write(self.style.SUCCESS(f'  {type_name}:'))
                            for god in god_list:
                                self.stdout.write(self.style.SUCCESS(f'    - {god}'))
                        elif type_name == 'Bad Gods':
                            self.stdout.write(self.style.ERROR(f'  {type_name}:'))
                            for god in god_list:
                                self.stdout.write(self.style.ERROR(f'    - {god}'))
                        else:
                            self.stdout.write(self.style.WARNING(f'  {type_name}:'))
                            for god in god_list:
                                self.stdout.write(self.style.WARNING(f'    - {god}'))
            else:
                self.stdout.write(f'\n{pillar.upper()} Pillar: No sha gods found') 