#!/usr/bin/env python3
"""
Supervisor Web App用のAPI
認証なしでアクセス可能
"""

from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework import status

from rog.models import NewEvent2
from rog.serializers import NewEvent2Serializer
import logging

logger = logging.getLogger(__name__)

@api_view(['GET'])
@permission_classes([AllowAny])
def get_events_for_supervisor(request):
    """
    Supervisor用のイベント一覧取得API
    認証なし・CSRF保護なし（urls.pyでcsrf_exemptを適用）
    """
    try:
        events = NewEvent2.objects.all()
        data = []
        
        for event in events:
            data.append({
                'id': event.id,
                'code': event.event_name,
                'name': event.event_name,
                'event_name': event.event_name,
                'start_datetime': event.start_datetime,
                'end_datetime': event.end_datetime,
                'status': event.status,
                'public': event.public
            })
        
        return Response(data, status=status.HTTP_200_OK)
    
    except Exception as e:
        logger.error(f"Error in get_events_for_supervisor: {str(e)}")
        return Response(
            {'error': 'イベント取得中にエラーが発生しました'}, 
            status=status.HTTP_500_INTERNAL_SERVER_ERROR
        )
