"""
Management command to test the hexagram numbering fix.

This command creates a sample LiuYao and tests that the hexagram number is correctly mapped to a name.
"""
import logging
from django.core.management.base import BaseCommand
from django.utils import timezone
from liuyao.models import liuyao
from ai.utils.liuyao_analysis import get_hexagram_name
import iching.utils as iutils

class Command(BaseCommand):
    help = 'Test the hexagram numbering fix'

    def handle(self, *args, **options):
        # Test each hexagram to see if indexing works correctly
        self.stdout.write("Testing hexagram numbering...\n")
        
        # Test a specific hexagram case mentioned by the user
        gua_index = 11  # 0-indexed
        expected_name = "否 (pǐ) - Standstill"
        adjusted_index = gua_index + 1  # 1-indexed
        
        self.stdout.write(f"Gua index (0-indexed): {gua_index}")
        self.stdout.write(f"Adjusted index (1-indexed): {adjusted_index}")
        self.stdout.write(f"Expected name: {expected_name}")
        self.stdout.write(f"Actual name from dictionary: {get_hexagram_name(adjusted_index)}")
        
        # Create a test hexagram with this configuration
        self.stdout.write("\nCreating a test LiuYao instance with hexagram #12...\n")
        
        # Create a binary representation that would result in hexagram #12
        # This is just a test example (might not be the exact binary for hexagram 12)
        y1, y2, y3 = '111', '111', '111'  # Upper trigram
        y4, y5, y6 = '000', '000', '000'  # Lower trigram
        
        # Calculate the actual hexagram
        ly = iutils.liuyao.calc6Yao(y1, y2, y3, y4, y5, y6)
        
        if 'ogua' in ly and 'gua' in ly['ogua']:
            original_gua = ly['ogua']['gua']
            # Original (0-indexed) gua
            self.stdout.write(f"Test hexagram gua index (0-indexed): {original_gua}")
            # Adjusted (1-indexed) gua 
            adjusted_gua = original_gua + 1
            self.stdout.write(f"Test hexagram adjusted index (1-indexed): {adjusted_gua}")
            self.stdout.write(f"Test hexagram name: {get_hexagram_name(adjusted_gua)}")
        
        # Test the entire hexagram list
        self.stdout.write("\nTesting all hexagrams...\n")
        
        for i in range(64):
            adjusted_i = i + 1
            self.stdout.write(f"{i} (0-indexed) -> {adjusted_i} (1-indexed): {get_hexagram_name(adjusted_i)}") 