#!/usr/bin/env python3
"""
Yu-Gi-Oh Card Data Importer
Fetches all TCG cards from YGOPRODeck API and exports to Excel
"""

import requests
import pandas as pd
from datetime import datetime

def fetch_yugioh_cards():
    """Fetch all Yu-Gi-Oh cards from the API"""
    print("Fetching card data from API...")
    
    url = "https://db.ygoprodeck.com/api/v7/cardinfo.php"
    
    try:
        response = requests.get(url, timeout=30)
        response.raise_for_status()
        data = response.json()
        return data['data']
    except requests.exceptions.RequestException as e:
        print(f"Error fetching data: {e}")
        return None

def parse_card_data(cards):
    """Parse card data into a flat structure for Excel"""
    print(f"Processing {len(cards)} cards...")
    
    parsed_cards = []
    
    for card in cards:
        # Basic card info
        card_info = {
            'ID': card.get('id'),
            'Name': card.get('name'),
            'Type': card.get('type'),
            'Human Readable Type': card.get('humanReadableCardType', ''),
            'Frame Type': card.get('frameType', ''),
            'Description': card.get('desc', ''),
            'Race': card.get('race', ''),
            'Archetype': card.get('archetype', ''),
            'ATK': card.get('atk', ''),
            'DEF': card.get('def', ''),
            'Level': card.get('level', ''),
            'Attribute': card.get('attribute', ''),
            'Scale': card.get('scale', ''),
            'Linkval': card.get('linkval', ''),
            'YGOPRODeck URL': card.get('ygoprodeck_url', '')
        }
        
        # Get all set info if available (comma-separated lists)
        if 'card_sets' in card and card['card_sets']:
            set_names = []
            set_codes = []
            set_rarities = []
            set_prices = []
            
            for card_set in card['card_sets']:
                set_names.append(card_set.get('set_name', ''))
                set_codes.append(card_set.get('set_code', ''))
                set_rarities.append(card_set.get('set_rarity', ''))
                set_prices.append(card_set.get('set_price', ''))
            
            card_info['Set Name'] = ', '.join(set_names)
            card_info['Set Code'] = ', '.join(set_codes)
            card_info['Set Rarity'] = ', '.join(set_rarities)
            card_info['Set Price'] = ', '.join(set_prices)
        else:
            card_info['Set Name'] = ''
            card_info['Set Code'] = ''
            card_info['Set Rarity'] = ''
            card_info['Set Price'] = ''
        
        # Get price info if available
        if 'card_prices' in card and card['card_prices']:
            prices = card['card_prices'][0]
            card_info['TCGPlayer Price'] = prices.get('tcgplayer_price', '')
            card_info['Cardmarket Price'] = prices.get('cardmarket_price', '')
            card_info['eBay Price'] = prices.get('ebay_price', '')
            card_info['Amazon Price'] = prices.get('amazon_price', '')
        else:
            card_info['TCGPlayer Price'] = ''
            card_info['Cardmarket Price'] = ''
            card_info['eBay Price'] = ''
            card_info['Amazon Price'] = ''
        
        parsed_cards.append(card_info)
    
    return parsed_cards

def export_to_excel(cards, filename='yugioh_cards.xlsx'):
    """Export cards to Excel file"""
    print(f"Creating Excel file: {filename}")
    
    # Create DataFrame
    df = pd.DataFrame(cards)
    
    # Create Excel writer
    with pd.ExcelWriter(filename, engine='openpyxl') as writer:
        df.to_excel(writer, sheet_name='YuGiOh Cards', index=False)
        
        # Get the worksheet
        worksheet = writer.sheets['YuGiOh Cards']
        
        # Auto-adjust column widths
        for column in worksheet.columns:
            max_length = 0
            column_letter = column[0].column_letter
            
            for cell in column:
                try:
                    if cell.value:
                        max_length = max(max_length, len(str(cell.value)))
                except:
                    pass
            
            # Set width (max 50 for description column)
            adjusted_width = min(max_length + 2, 50)
            worksheet.column_dimensions[column_letter].width = adjusted_width
        
        # Apply header formatting
        for cell in worksheet[1]:
            cell.font = cell.font.copy(bold=True)
        
        # Add autofilter
        worksheet.auto_filter.ref = worksheet.dimensions
    
    print(f"Successfully exported {len(cards)} cards to {filename}")

def main():
    """Main function"""
    print("Yu-Gi-Oh Card Data Importer")
    print("=" * 50)
    
    # Fetch cards
    cards = fetch_yugioh_cards()
    
    if not cards:
        print("Failed to fetch card data. Exiting.")
        return
    
    # Parse cards
    parsed_cards = parse_card_data(cards)
    
    # Export to Excel
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    filename = f"yugioh_cards_{timestamp}.xlsx"
    export_to_excel(parsed_cards, filename)
    
    print("\nDone! You can now open the file in LibreOffice Calc or Excel.")

if __name__ == "__main__":
    main()