import sys
import logging
from typing import Dict, List, Optional
from fastapi import FastAPI, Depends, HTTPException, Body, Path
from fastapi.middleware.cors import CORSMiddleware

# Настраиваем логирование
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

# Имитируем эндпоинты календарного сервиса для тестирования
app = FastAPI(title="Simplified Calendar Service")

# Включаем CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Упрощенная модель данных
from enum import Enum
from datetime import date, datetime
from typing import List, Optional
from pydantic import BaseModel, Field

# Модели для календарных записей
class EntryType(str, Enum):
    PERIOD = "period"
    OVULATION = "ovulation"
    SYMPTOMS = "symptoms"
    MOOD = "mood"
    OTHER = "other"

class FlowIntensity(str, Enum):
    LIGHT = "light"
    MEDIUM = "medium"
    HEAVY = "heavy"

class CalendarEntry(BaseModel):
    id: int
    user_id: int = 1
    entry_date: date
    entry_type: str
    flow_intensity: Optional[str] = None
    period_symptoms: Optional[str] = None
    mood: Optional[str] = None
    energy_level: Optional[int] = None
    sleep_hours: Optional[float] = None
    symptoms: Optional[str] = None
    medications: Optional[str] = None
    notes: Optional[str] = None
    created_at: datetime = datetime.now()
    updated_at: Optional[datetime] = None

# Хранилище данных в памяти
calendar_entries = []

# Вспомогательная функция для добавления тестовых данных
def add_test_entries():
    if not calendar_entries:
        for i in range(1, 5):
            calendar_entries.append(
                CalendarEntry(
                    id=i,
                    entry_date=date(2025, 9, 30),
                    entry_type="period",
                    flow_intensity="medium",
                    notes=f"Test entry {i}",
                )
            )

# Добавляем тестовые данные
add_test_entries()

@app.get("/")
def read_root():
    return {"message": "Simplified Calendar Service API"}

@app.get("/health")
def health():
    return {"status": "ok"}

# API для работы с календарем
@app.get("/api/v1/calendar/entries")
def get_calendar_entries():
    """Get all calendar entries"""
    return calendar_entries

@app.post("/api/v1/calendar/entries", status_code=201)
def create_calendar_entry(entry: dict):
    """Create a new calendar entry"""
    logger.debug(f"Received entry data: {entry}")
    
    # Преобразуем строку даты в объект date
    entry_date_str = entry.get("entry_date")
    if entry_date_str and isinstance(entry_date_str, str):
        try:
            entry_date = date.fromisoformat(entry_date_str)
        except ValueError:
            entry_date = date.today()
    else:
        entry_date = date.today()
    
    new_entry = CalendarEntry(
        id=len(calendar_entries) + 1,
        entry_date=entry_date,
        entry_type=entry.get("entry_type", "other"),
        flow_intensity=entry.get("flow_intensity"),
        period_symptoms=entry.get("period_symptoms"),
        mood=entry.get("mood"),
        energy_level=entry.get("energy_level"),
        sleep_hours=entry.get("sleep_hours"),
        symptoms=entry.get("symptoms"),
        medications=entry.get("medications"),
        notes=entry.get("notes"),
    )
    
    calendar_entries.append(new_entry)
    return new_entry

# Добавляем поддержку для /api/v1/entry
@app.post("/api/v1/entry", status_code=201)
def create_entry_without_calendar_prefix(entry: dict):
    """Create a new calendar entry via alternate endpoint (without /calendar/ prefix)"""
    logger.debug(f"Received entry data via /api/v1/entry: {entry}")
    return create_calendar_entry(entry)

# Добавляем поддержку для /api/v1/entries (legacy)
@app.post("/api/v1/entries", status_code=201)
def create_entry_legacy(entry: dict):
    """Create a new calendar entry via legacy endpoint"""
    logger.debug(f"Received entry data via legacy endpoint: {entry}")
    return create_calendar_entry(entry)

# Добавляем поддержку для мобильного формата
@app.post("/api/v1/calendar/entry", status_code=201)
def create_mobile_calendar_entry(mobile_entry: dict):
    """Create a new calendar entry from mobile app format"""
    logger.debug(f"Received mobile entry data: {mobile_entry}")
    
    # Преобразуем мобильный формат в стандартный
    entry = {
        "entry_date": mobile_entry.get("date", date.today().isoformat()),
        "entry_type": mobile_entry.get("type", "OTHER").lower(),
        "flow_intensity": "medium" if mobile_entry.get("flow_intensity") in [3, 4] else "light",
        "notes": mobile_entry.get("notes"),
        "symptoms": ", ".join(mobile_entry.get("symptoms", [])) if isinstance(mobile_entry.get("symptoms"), list) else ""
    }
    
    return create_calendar_entry(entry)

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8888)