import os
from django.core.management.base import BaseCommand
from django.core.files import File
from django.conf import settings
from ad_static.models import Banner


class Command(BaseCommand):
    help = "Seed Banner data using images from static/img/bbanner folder (update if exists)"

    def handle(self, *args, **kwargs):
        image_dir = os.path.join(settings.BASE_DIR, "ad_static", "banner")

        data = [
            {
                "title": "Crafting Elegant",
                "subtitle": "Spaces",
                "content": "Transforming interiors with minimalist design.",
                "type": 1,
                "image": "133990158653571775.jpg",
            },
            {
                "title": "About Our Vision",
                "subtitle": "Who We Are",
                "content": "We believe in designing spaces that inspire and empower.",
                "type": 2,
                "image": "134030920802221299.jpg",
            },
            {
                "title": "Our Creative Services",
                "subtitle": "What We Do",
                "content": "Offering interior design, architecture, and project planning.",
                "type": 3,
                "image": "134055814789041596.jpg",
            },
            {
                "title": "Our Latest Projects",
                "subtitle": "Portfolio",
                "content": "Explore our collection of modern and sustainable designs.",
                "type": 4,
                "image": "sung-jinwoo-solo-leveling-3840x2160-14579.jpg",
            },
            {
                "title": "Our Latest Vaastu",
                "subtitle": "Vaastu",
                "content": "UTILIZE THE BENEFITS OF NATURAL ENERGY.",
                "type": 5,
                "image": "istockphoto.jpg",
            },
            {
                "title": "Let's Work Together",
                "subtitle": "Contact Us",
                "content": "Reach out to us for your next design journey.",
                "type": 6,
                "image": "zenitsu-agatsuma-5120x4102-17042.jpg",
            },
        ]

        for banner_data in data:
            banner, created = Banner.objects.update_or_create(
                title=banner_data["title"],
                defaults={
                    "subtitle": banner_data["subtitle"],
                    "content": banner_data["content"],
                    "type": banner_data["type"],
                },
            )

            # Always update the image if exists
            image_path = os.path.join(image_dir, banner_data["image"])
            if os.path.exists(image_path):
                with open(image_path, "rb") as img_file:
                    banner.image.save(banner_data["image"], File(img_file), save=True)
                print(f"Image set/updated for: {banner.title}")
            else:
                print(f"Image not found: {banner_data['image']}")

            if created:
                print(f"Created banner: {banner.title} (Type {banner.type})")
            else:
                print(f"Updated banner: {banner.title} (Type {banner.type})")
