import logging

from django.core.management.base import BaseCommand
from django.db import models, transaction

logger = logging.getLogger(__name__)


class Command(BaseCommand):
    help = "Enrich TV series metadata from TMDB/OMDB APIs"

    def add_arguments(self, parser):
        parser.add_argument(
            "--force",
            action="store_true",
            help="Overwrite existing cover image",
        )
        parser.add_argument(
            "--dry-run",
            action="store_true",
            help="Show what would be done without making changes",
        )
        parser.add_argument(
            "--imdb-id",
            type=str,
            help="Only process series with this imdb_id",
        )
        parser.add_argument(
            "--needs-metadata",
            action="store_true",
            help="Only process series missing imdb_id or cover image",
        )

    def handle(self, *args, **options):
        from videos.models import Series

        force = options["force"]
        dry_run = options["dry_run"]
        imdb_id = options["imdb_id"]
        needs_metadata = options["needs_metadata"]

        qs = Series.objects.all()
        if imdb_id:
            qs = qs.filter(imdb_id=imdb_id)
        if needs_metadata:
            qs = qs.filter(
                models.Q(imdb_id__isnull=True)
                | models.Q(imdb_id="")
                | models.Q(cover_image__isnull=True)
                | models.Q(cover_image="")
            )

        total = qs.count()
        self.stdout.write(f"Processing {total} series")

        if dry_run:
            for series in qs.iterator():
                has_imdb = bool(series.imdb_id)
                has_image = bool(series.cover_image)
                self.stdout.write(
                    f"  [DRY RUN] Would fix {series.name}"
                    f" (imdb_id={'✓' if has_imdb else '✗'}"
                    f", image={'✓' if has_image else '✗'})"
                )
            return

        updated = 0
        errors = 0
        for series in qs.iterator():
            try:
                with transaction.atomic():
                    series.fix_metadata(force_update=force)
                updated += 1
                self.stdout.write(f"  [{updated}/{total}] {series.name}")
            except Exception as e:
                errors += 1
                self.stdout.write(
                    self.style.ERROR(
                        f"  Error updating series {series.name} (imdb_id={series.imdb_id}): {e}"
                    )
                )

        self.stdout.write(
            self.style.SUCCESS(f"\nDone! {updated} series updated, {errors} errors")
        )
