from django.core.management.base import BaseCommand
from datetime import datetime, timedelta
import logging

class Command(BaseCommand):
    help = 'Test FFT tournament scraping with Playwright'

    def add_arguments(self, parser):
        parser.add_argument(
            '--club-code',
            type=str,
            default='62130180',
            help='Club code for testing (default: 62130180)'
        )
        parser.add_argument(
            '--club-name',
            type=str,
            default='TENNIS SPORTING CLUB DE CASSIS',
            help='Club name for testing'
        )
        parser.add_argument(
            '--all-pages',
            action='store_true',
            help='Test all pages scraping'
        )
        parser.add_argument(
            '--verbose',
            action='store_true',
            help='Enable verbose logging'
        )

    def handle(self, *args, **options):
        if options['verbose']:
            logging.basicConfig(level=logging.INFO)

        club_code = options['club_code']
        club_name = options['club_name']
        all_pages = options['all_pages']
        verbose = options['verbose']

        # Calculate date range
        start_date = datetime.now()
        end_date = start_date + timedelta(days=90)
        start_date_str = start_date.strftime('%d/%m/%y')
        end_date_str = end_date.strftime('%d/%m/%y')

        self.stdout.write(self.style.SUCCESS("=== FFT Tournament Scraper ==="))
        self.stdout.write(f"Club: {club_name} ({club_code})")
        self.stdout.write(f"Date range: {start_date_str} to {end_date_str}")
        self.stdout.write(f"Method: Playwright (Chrome-free)")
        self.stdout.write("")

        try:
            if all_pages:
                from api.utils import scrape_fft_club_tournaments_all_pages
                self.stdout.write("🚀 Testing complete tournament scraping...")

                result = scrape_fft_club_tournaments_all_pages(
                    club_code=club_code,
                    club_name=club_name,
                    start_date=start_date_str,
                    end_date=end_date_str
                )
            else:
                from api.utils import scrape_fft_club_tournaments
                self.stdout.write("🚀 Testing single page scraping...")

                result = scrape_fft_club_tournaments(
                    club_code=club_code,
                    club_name=club_name,
                    start_date=start_date_str,
                    end_date=end_date_str,
                    page=0
                )

            # Debug: Show what we got (only in verbose mode)
            if verbose:
                self.stdout.write(f"🔍 Raw result: {result}")

            if result:
                tournaments = result.get('tournaments', [])
                self.stdout.write(self.style.SUCCESS(f"✅ SUCCESS: {len(tournaments)} tournaments found"))

                if tournaments:
                    self.stdout.write("\n📝 Sample tournament:")
                    sample = tournaments[0]
                    self.stdout.write(f"   ID: {sample.get('id')}")
                    self.stdout.write(f"   Name: {sample.get('libelle')}")
                    self.stdout.write(f"   Date: {sample.get('dateDebut', {}).get('date', 'N/A')}")
                    self.stdout.write(f"   Judge: {sample.get('jugeArbitre', {}).get('nom', 'N/A')}")

                self.stdout.write(f"\n📊 Summary:")
                self.stdout.write(f"   Total tournaments: {len(tournaments)}")
                self.stdout.write(f"   Pages scraped: {result.get('pages_scraped', 1)}")

            else:
                self.stdout.write(self.style.ERROR("❌ FAILED: No tournaments found"))

        except Exception as e:
            self.stdout.write(self.style.ERROR(f"❌ ERROR: {e}"))
            import traceback
            if verbose:
                self.stdout.write(traceback.format_exc())
