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

from rog.models import NewEvent2, NewCategory, Entry
from datetime import datetime
import pytz

# Step 1: Create test event "大垣テスト" based on "大垣３"
print("Step 1: Creating test event '大垣テスト'...")

# Get original event
try:
    original_event = NewEvent2.objects.get(event_name="大垣３")
    print(f"Found original event: {original_event.event_name} (ID: {original_event.id})")
except NewEvent2.DoesNotExist:
    print("Error: Original event '大垣３' not found!")
    exit(1)

# Check if test event already exists
if NewEvent2.objects.filter(event_name="大垣テスト").exists():
    print("Test event '大垣テスト' already exists, deleting it first...")
    test_event_existing = NewEvent2.objects.get(event_name="大垣テスト")
    Entry.objects.filter(event=test_event_existing).delete()
    test_event_existing.delete()

# Create new test event
test_event = NewEvent2(
    event_name="大垣テスト",
    event_description=f"Test event based on {original_event.event_name}",
    start_datetime=datetime.now(pytz.UTC),
    end_datetime=datetime.now(pytz.UTC).replace(hour=23, minute=59),
    public=True
)
test_event.save()
print(f"Created test event: {test_event.event_name} (ID: {test_event.id})")

# Get available categories and create Entry records
print("Creating Entry records...")
available_categories = NewCategory.objects.all()[:5]  # Use first 5 categories for testing

for category in available_categories:
    new_entry = Entry(
        event=test_event,
        category=category
    )
    new_entry.save()
    print(f"Created entry: Event {test_event.event_name} - Category {category.category_name}")

print(f"\nTest event '{test_event.event_name}' created successfully!")
print(f"Event ID: {test_event.id}")
print(f"Entries created: {Entry.objects.filter(event=test_event).count()}")
print("Available categories for this event:")
for entry in Entry.objects.filter(event=test_event):
    print(f"  - {entry.category.category_name} (Duration: {entry.category.hours}h)")

print("\nStep 1 completed successfully!")
