import os
import django
from decimal import Decimal
from django.utils import timezone
from datetime import timedelta

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'flashsale.settings')
django.setup()

from django.contrib.auth.models import User
from businesses.models import Business, DeliveryOption
from products.models import Product
from sales.models import Sale, SaleItem

def seed():
    # 1. Admin / Seller user
    user, created = User.objects.get_or_create(username='admin')
    if created:
        user.set_password('admin1234')
        user.is_staff = True
        user.is_superuser = True
        user.save()
        print("Created superuser: admin / admin1234")

    # 2. Sample Business
    business, _ = Business.objects.get_or_create(
        user=user,
        defaults={
            'name': 'Kofi Electronics & Fashion',
            'phone': '+233241234567',
            'email': 'kofi@example.com',
            'is_verified': True
        }
    )

    # 3. Delivery Options
    d1, _ = DeliveryOption.objects.get_or_create(
        business=business,
        name='Within Accra (Free)',
        defaults={
            'fee': Decimal('0.00'),
            'description': 'This includes areas such as Nungua, Osu, Spintex, East Legon, Cantoments.',
            'order': 1
        }
    )
    d2, _ = DeliveryOption.objects.get_or_create(
        business=business,
        name='Greater Accra Outer & Tema',
        defaults={
            'fee': Decimal('30.00'),
            'description': 'Includes Tema, Kasoa, Adenta, Ashaiman, Pokuase.',
            'order': 2
        }
    )
    d3, _ = DeliveryOption.objects.get_or_create(
        business=business,
        name='Other Regions (STC / VIP Courier)',
        defaults={
            'fee': Decimal('50.00'),
            'description': 'Waybill delivery via STC Parcel or VIP Express to Kumasi, Takoradi, Tamale.',
            'order': 3
        }
    )
    print("Seeded Delivery Options!")

    # 4. Sample Products
    p1, _ = Product.objects.get_or_create(
        business=business,
        name='Wireless Bluetooth Headphones',
        defaults={'description': 'High quality noise cancelling headphones with bass boost.', 'base_price': Decimal('250.00')}
    )
    p2, _ = Product.objects.get_or_create(
        business=business,
        name='Smart Fitness Watch',
        defaults={'description': 'Waterproof fitness tracker with heart rate monitor.', 'base_price': Decimal('350.00')}
    )
    p3, _ = Product.objects.get_or_create(
        business=business,
        name='Designer Leather Wallet',
        defaults={'description': 'Genuine leather compact bi-fold wallet.', 'base_price': Decimal('120.00')}
    )

    # 5. Sample Sale
    now = timezone.now()
    sale, created = Sale.objects.get_or_create(
        business=business,
        title='Mega Weekend Flash Sale',
        defaults={
            'start_at': now - timedelta(hours=1),
            'end_at': now + timedelta(days=2),
            'status': 'live',
            'pricing_model': 'daily_rate',
            'booking_fee_amount': Decimal('100.00'),
        }
    )

    if created:
        SaleItem.objects.create(
            sale=sale, product=p1, price=Decimal('180.00'), discount_percent=Decimal('28'), stock_total=15, stock_remaining=15
        )
        SaleItem.objects.create(
            sale=sale, product=p2, price=Decimal('270.00'), discount_percent=Decimal('22'), stock_total=10, stock_remaining=10
        )
        SaleItem.objects.create(
            sale=sale, product=p3, price=Decimal('85.00'), discount_percent=Decimal('29'), stock_total=25, stock_remaining=25
        )

if __name__ == '__main__':
    seed()
