#!/usr/bin/env python
"""
Location2025とイベントの関係を調査するスクリプト
"""

from rog.models import Location2025, NewEvent2
from django.db import connection

def main():
    print('=== Location2025とイベントの関係調査 ===')

    # Location2025のeventフィールドの外部キー先を確認
    event_field = Location2025._meta.get_field('event')
    print(f'Location2025.event field references: {event_field.related_model}')

    # 現在のLocation2025データのイベント分布
    print('\n=== Location2025のイベント分布 ===')
    cursor = connection.cursor()
    cursor.execute("""
        SELECT l.event_id, ne.event_name, COUNT(*) as count
        FROM rog_location2025 l
        LEFT JOIN rog_newevent2 ne ON l.event_id = ne.id
        GROUP BY l.event_id, ne.event_name
        ORDER BY count DESC
    """)

    for row in cursor.fetchall():
        event_id, event_name, count = row
        print(f'  Event ID {event_id}: {event_name} ({count}件)')

    # NewEvent2の一覧
    print('\n=== NewEvent2テーブルの全イベント ===')
    for event in NewEvent2.objects.all()[:10]:
        print(f'  ID {event.id}: {event.event_name} (status: {event.status})')

    # CSVアップロード画面のイベント選択肢を確認
    print('\n=== CSVアップロード画面のイベント選択肢 ===')
    events = NewEvent2.objects.filter(status='public').order_by('-start_datetime')
    for event in events[:5]:
        print(f'  ID {event.id}: {event.event_name} (status: {event.status}, start: {event.start_datetime})')

    # 実際のLocation2025サンプルデータ
    print('\n=== Location2025サンプルデータ ===')
    sample_locations = Location2025.objects.all()[:3]
    for loc in sample_locations:
        print(f'  CP{loc.cp_number}: {loc.cp_name} -> Event ID {loc.event_id} ({loc.event.event_name if loc.event else "None"})')

if __name__ == '__main__':
    main()
