# import_images.py
#
# Use to import all images in IMAGE_DIR into the database
#

import os
from django.core.management.base import BaseCommand
from eggTrainerApp.models import EggImage
from django.conf import settings


class Command(BaseCommand):
    help = 'Import images from /Data/images/ into the database'

    def handle(self, *args, **options):
        image_dir = settings.IMAGE_DIR
        for root, _, files in os.walk(image_dir):
            for filename in files:
                if filename.lower().endswith(('.jpg', '.jpeg', '.png', '.gif')):
                    image_path = os.path.join(root, filename)
                    # Create or get EggImage object
                    image, created = EggImage.objects.get_or_create(
                        image_path=image_path,
                        defaults={'num_tags':0}
                    )
                    if created:
                        self.stdout.write(self.style.SUCCESS(f'Imported: {filename}'))
                    else:
                        self.stdout.write(f'{filename} already exists.')
