from fastapi import FastAPI, APIRouter, HTTPException, Depends, UploadFile, File, Form
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from dotenv import load_dotenv
from starlette.middleware.cors import CORSMiddleware
from motor.motor_asyncio import AsyncIOMotorClient
import os
import logging
from pathlib import Path
from pydantic import BaseModel, Field, EmailStr
from typing import List, Optional, Dict, Any
import uuid
from datetime import datetime, timedelta
import jwt
from passlib.context import CryptContext
import base64
import json

ROOT_DIR = Path(__file__).parent
load_dotenv(ROOT_DIR / '.env')

# MongoDB connection
mongo_url = os.environ.get('MONGO_URL', 'mongodb://localhost:27017')
client = AsyncIOMotorClient(mongo_url)
db = client[os.environ.get('DB_NAME', 'abbasfit_db')]

# JWT Configuration
SECRET_KEY = os.environ.get('JWT_SECRET_KEY', 'abbas-fit-secret-key-2025-super-secure')
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_HOURS = 24

# Emergent LLM Key
EMERGENT_LLM_KEY = os.environ.get('EMERGENT_LLM_KEY', '')

# Password hashing
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

# Security
security = HTTPBearer()

# Create the main app
app = FastAPI(title="Abbas FIT API", version="2.0.0")

# Create a router with the /api prefix
api_router = APIRouter(prefix="/api")

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# ==================== CONSTANTS ====================

MEAL_TYPES = [
    "breakfast",
    "mid_morning_snack", 
    "lunch",
    "afternoon_snack",
    "pre_workout",
    "post_workout",
    "dinner"
]

FITNESS_GOALS = ["weight_loss", "weight_gain", "muscle_gain", "maintenance", "general_fitness"]

# ==================== MODELS ====================

class UserCreate(BaseModel):
    email: EmailStr
    name: str
    password: str
    role: str = "user"
    height_cm: Optional[float] = None
    weight_kg: Optional[float] = None
    age: Optional[int] = None
    gender: Optional[str] = None
    # Nutrition targets
    calorie_target: Optional[int] = 2000
    protein_target: Optional[int] = 150
    fiber_target: Optional[int] = 30
    fat_target: Optional[int] = 65
    carbs_target: Optional[int] = 250
    # New targets
    water_intake_goal: Optional[float] = 3.0  # liters
    step_goal: Optional[int] = 10000
    # Fitness goal
    fitness_goal: Optional[str] = "general_fitness"  # weight_loss, weight_gain, muscle_gain, maintenance
    target_weight: Optional[float] = None
    # Notification settings
    meal_reminders: Optional[bool] = True
    water_reminders: Optional[bool] = True
    workout_reminders: Optional[bool] = True

class UserUpdate(BaseModel):
    name: Optional[str] = None
    height_cm: Optional[float] = None
    weight_kg: Optional[float] = None
    age: Optional[int] = None
    gender: Optional[str] = None
    calorie_target: Optional[int] = None
    protein_target: Optional[int] = None
    fiber_target: Optional[int] = None
    fat_target: Optional[int] = None
    carbs_target: Optional[int] = None
    water_intake_goal: Optional[float] = None
    step_goal: Optional[int] = None
    fitness_goal: Optional[str] = None
    target_weight: Optional[float] = None
    meal_reminders: Optional[bool] = None
    water_reminders: Optional[bool] = None
    workout_reminders: Optional[bool] = None
    profile_photo: Optional[str] = None
    is_blocked: Optional[bool] = None  # Admin can block users

class UserResponse(BaseModel):
    id: str
    email: str
    name: str
    role: str
    height_cm: Optional[float] = None
    weight_kg: Optional[float] = None
    age: Optional[int] = None
    gender: Optional[str] = None
    calorie_target: Optional[int] = None
    protein_target: Optional[int] = None
    fiber_target: Optional[int] = None
    fat_target: Optional[int] = None
    carbs_target: Optional[int] = None
    water_intake_goal: Optional[float] = None
    step_goal: Optional[int] = None
    fitness_goal: Optional[str] = None
    target_weight: Optional[float] = None
    meal_reminders: Optional[bool] = None
    water_reminders: Optional[bool] = None
    workout_reminders: Optional[bool] = None
    profile_photo: Optional[str] = None
    is_blocked: Optional[bool] = False  # User blocked status
    bmi: Optional[float] = None
    bmr: Optional[float] = None
    created_at: datetime

class LoginRequest(BaseModel):
    email: str
    password: str

class TokenResponse(BaseModel):
    access_token: str
    token_type: str = "bearer"
    user: UserResponse

class ContactSubmission(BaseModel):
    id: str = Field(default_factory=lambda: str(uuid.uuid4()))
    name: str
    email: EmailStr
    phone: Optional[str] = None
    message: str
    created_at: datetime = Field(default_factory=datetime.utcnow)
    read: bool = False

class ContactCreate(BaseModel):
    name: str
    email: EmailStr
    phone: Optional[str] = None
    message: str

class VitalLog(BaseModel):
    id: str = Field(default_factory=lambda: str(uuid.uuid4()))
    user_id: str
    weight_kg: float
    height_cm: Optional[float] = None
    body_fat_percent: Optional[float] = None
    chest_cm: Optional[float] = None
    waist_cm: Optional[float] = None
    hips_cm: Optional[float] = None
    arms_cm: Optional[float] = None
    thighs_cm: Optional[float] = None
    bmi: Optional[float] = None
    bmr: Optional[float] = None
    notes: Optional[str] = None
    created_at: datetime = Field(default_factory=datetime.utcnow)

class VitalLogCreate(BaseModel):
    weight_kg: float
    height_cm: Optional[float] = None
    body_fat_percent: Optional[float] = None
    chest_cm: Optional[float] = None
    waist_cm: Optional[float] = None
    hips_cm: Optional[float] = None
    arms_cm: Optional[float] = None
    thighs_cm: Optional[float] = None
    notes: Optional[str] = None

class MealLog(BaseModel):
    id: str = Field(default_factory=lambda: str(uuid.uuid4()))
    user_id: str
    meal_type: str  # breakfast, mid_morning_snack, lunch, afternoon_snack, pre_workout, post_workout, dinner
    description: str
    photo_base64: Optional[str] = None
    calories: Optional[int] = None
    protein_g: Optional[float] = None
    carbs_g: Optional[float] = None
    fat_g: Optional[float] = None
    fiber_g: Optional[float] = None
    ai_analysis: Optional[Dict[str, Any]] = None
    created_at: datetime = Field(default_factory=datetime.utcnow)

class MealLogCreate(BaseModel):
    meal_type: str
    description: str
    photo_base64: Optional[str] = None
    calories: Optional[int] = None
    protein_g: Optional[float] = None
    carbs_g: Optional[float] = None
    fat_g: Optional[float] = None
    fiber_g: Optional[float] = None

class WaterLog(BaseModel):
    id: str = Field(default_factory=lambda: str(uuid.uuid4()))
    user_id: str
    amount_ml: int
    created_at: datetime = Field(default_factory=datetime.utcnow)

class WaterLogCreate(BaseModel):
    amount_ml: int

class StepLog(BaseModel):
    id: str = Field(default_factory=lambda: str(uuid.uuid4()))
    user_id: str
    steps: int
    date: str  # YYYY-MM-DD
    created_at: datetime = Field(default_factory=datetime.utcnow)

class StepLogCreate(BaseModel):
    steps: int
    date: Optional[str] = None  # If not provided, use today

class ActivityLog(BaseModel):
    id: str = Field(default_factory=lambda: str(uuid.uuid4()))
    user_id: str
    exercise_name: str
    duration_minutes: int
    sets: Optional[int] = None
    reps: Optional[int] = None
    weight_kg: Optional[float] = None
    rpe: Optional[int] = None  # Rate of Perceived Exertion 1-10
    calories_burned: Optional[int] = None
    notes: Optional[str] = None
    created_at: datetime = Field(default_factory=datetime.utcnow)

class ActivityLogCreate(BaseModel):
    exercise_name: str
    duration_minutes: int
    sets: Optional[int] = None
    reps: Optional[int] = None
    weight_kg: Optional[float] = None
    rpe: Optional[int] = None
    calories_burned: Optional[int] = None
    notes: Optional[str] = None

# Enhanced Diet Plan with 7 meals
class DietPlanMeal(BaseModel):
    meal_type: str
    name: str
    recipe: Optional[str] = None
    ingredients: Optional[List[str]] = []
    calories: Optional[int] = None
    protein_g: Optional[float] = None
    carbs_g: Optional[float] = None
    fat_g: Optional[float] = None
    time: Optional[str] = None  # e.g., "07:00 AM"

class DietPlan(BaseModel):
    id: str = Field(default_factory=lambda: str(uuid.uuid4()))
    name: str
    description: Optional[str] = None
    meals: List[DietPlanMeal] = []
    total_calories: Optional[int] = None
    created_by: str  # admin user_id
    assigned_to: List[str] = []  # list of user_ids
    created_at: datetime = Field(default_factory=datetime.utcnow)
    active: bool = True

class DietPlanCreate(BaseModel):
    name: str
    description: Optional[str] = None
    meals: List[Dict[str, Any]] = []
    total_calories: Optional[int] = None
    assigned_to: List[str] = []

# Supplements
class Supplement(BaseModel):
    id: str = Field(default_factory=lambda: str(uuid.uuid4()))
    name: str
    description: Optional[str] = None
    dosage: Optional[str] = None
    timing: Optional[str] = None  # e.g., "Before workout", "With breakfast"
    created_by: str
    created_at: datetime = Field(default_factory=datetime.utcnow)

class SupplementCreate(BaseModel):
    name: str
    description: Optional[str] = None
    dosage: Optional[str] = None
    timing: Optional[str] = None

class SupplementAssignment(BaseModel):
    id: str = Field(default_factory=lambda: str(uuid.uuid4()))
    user_id: str
    supplement_id: str
    supplement_name: str
    dosage: Optional[str] = None
    timing: Optional[str] = None
    notes: Optional[str] = None
    assigned_by: str
    created_at: datetime = Field(default_factory=datetime.utcnow)
    active: bool = True

class SupplementAssignmentCreate(BaseModel):
    user_id: str
    supplement_id: str
    dosage: Optional[str] = None
    timing: Optional[str] = None
    notes: Optional[str] = None

class SupplementLog(BaseModel):
    id: str = Field(default_factory=lambda: str(uuid.uuid4()))
    user_id: str
    supplement_id: str
    supplement_name: str
    taken: bool = True
    created_at: datetime = Field(default_factory=datetime.utcnow)

class SupplementLogCreate(BaseModel):
    supplement_id: str
    taken: bool = True

class WorkoutPlan(BaseModel):
    id: str = Field(default_factory=lambda: str(uuid.uuid4()))
    name: str
    description: Optional[str] = None
    exercises: List[Dict[str, Any]]  # [{name, sets, reps, rest_seconds, video_url, etc}]
    duration_minutes: Optional[int] = None
    created_by: str
    assigned_to: List[str] = []
    created_at: datetime = Field(default_factory=datetime.utcnow)
    active: bool = True

class WorkoutPlanCreate(BaseModel):
    name: str
    description: Optional[str] = None
    exercises: List[Dict[str, Any]]
    duration_minutes: Optional[int] = None
    assigned_to: List[str] = []

class ProgressPhoto(BaseModel):
    id: str = Field(default_factory=lambda: str(uuid.uuid4()))
    user_id: str
    photo_base64: str
    photo_type: str  # "front", "side", "back", "custom"
    week_number: Optional[int] = None
    notes: Optional[str] = None
    created_at: datetime = Field(default_factory=datetime.utcnow)

class ProgressPhotoCreate(BaseModel):
    photo_base64: str
    photo_type: str
    week_number: Optional[int] = None
    notes: Optional[str] = None

class SessionBooking(BaseModel):
    id: str = Field(default_factory=lambda: str(uuid.uuid4()))
    user_id: str
    date: str  # YYYY-MM-DD
    time_slot: str  # "09:00", "10:00", etc
    status: str = "pending"  # pending, confirmed, cancelled, completed
    notes: Optional[str] = None
    created_at: datetime = Field(default_factory=datetime.utcnow)

class SessionBookingCreate(BaseModel):
    date: str
    time_slot: str
    notes: Optional[str] = None

class Testimonial(BaseModel):
    id: str = Field(default_factory=lambda: str(uuid.uuid4()))
    name: str
    content: str
    photo_base64: Optional[str] = None
    rating: int = 5
    active: bool = True
    created_at: datetime = Field(default_factory=datetime.utcnow)

class TestimonialCreate(BaseModel):
    name: str
    content: str
    photo_base64: Optional[str] = None
    rating: int = 5

class AIGoalPrediction(BaseModel):
    id: str = Field(default_factory=lambda: str(uuid.uuid4()))
    user_id: str
    goal_type: str  # "weight_loss", "muscle_gain", "maintenance"
    prediction: str
    target_value: Optional[float] = None
    timeline_weeks: Optional[int] = None
    confidence: Optional[float] = None
    recommendations: List[str] = []
    created_at: datetime = Field(default_factory=datetime.utcnow)

class Notification(BaseModel):
    id: str = Field(default_factory=lambda: str(uuid.uuid4()))
    user_id: str
    title: str
    message: str
    type: str  # "meal_reminder", "water_reminder", "workout_reminder", "goal_achieved", "general"
    read: bool = False
    created_at: datetime = Field(default_factory=datetime.utcnow)

# ==================== APP SETTINGS MODELS ====================

class AppSettings(BaseModel):
    id: str = "app_settings"  # Singleton document
    app_name: str = "Abbas FIT"
    logo_base64: Optional[str] = None
    icon_base64: Optional[str] = None
    # Contact Details
    contact_email: Optional[str] = None
    contact_phone: Optional[str] = None
    contact_address: Optional[str] = None
    contact_whatsapp: Optional[str] = None
    # Social Media
    social_facebook: Optional[str] = None
    social_instagram: Optional[str] = None
    social_twitter: Optional[str] = None
    social_youtube: Optional[str] = None
    social_linkedin: Optional[str] = None
    # Landing Page Content
    hero_title: str = "Transform Your Body, Transform Your Life"
    hero_subtitle: str = "Personal training designed for your unique fitness journey"
    hero_cta_text: str = "Start Your Journey"
    about_title: str = "About Abbas FIT"
    about_description: str = "Professional personal training services to help you achieve your fitness goals."
    about_features: List[str] = []
    # Footer
    copyright_text: str = "© 2025 Abbas FIT. All rights reserved."
    # Metadata
    updated_at: datetime = Field(default_factory=datetime.utcnow)
    updated_by: Optional[str] = None

class AppSettingsUpdate(BaseModel):
    app_name: Optional[str] = None
    logo_base64: Optional[str] = None
    icon_base64: Optional[str] = None
    contact_email: Optional[str] = None
    contact_phone: Optional[str] = None
    contact_address: Optional[str] = None
    contact_whatsapp: Optional[str] = None
    social_facebook: Optional[str] = None
    social_instagram: Optional[str] = None
    social_twitter: Optional[str] = None
    social_youtube: Optional[str] = None
    social_linkedin: Optional[str] = None
    hero_title: Optional[str] = None
    hero_subtitle: Optional[str] = None
    hero_cta_text: Optional[str] = None
    about_title: Optional[str] = None
    about_description: Optional[str] = None
    about_features: Optional[List[str]] = None
    copyright_text: Optional[str] = None

class LegalPage(BaseModel):
    id: str = Field(default_factory=lambda: str(uuid.uuid4()))
    page_type: str  # "terms", "privacy", "refund", "disclaimer"
    title: str
    content: str  # HTML content from WYSIWYG editor
    is_active: bool = True
    created_at: datetime = Field(default_factory=datetime.utcnow)
    updated_at: datetime = Field(default_factory=datetime.utcnow)
    updated_by: Optional[str] = None

class LegalPageCreate(BaseModel):
    page_type: str
    title: str
    content: str

class LegalPageUpdate(BaseModel):
    title: Optional[str] = None
    content: Optional[str] = None
    is_active: Optional[bool] = None

class ApiKey(BaseModel):
    id: str = Field(default_factory=lambda: str(uuid.uuid4()))
    name: str  # e.g., "Gemini API", "OpenAI", "Stripe"
    key_type: str  # "gemini", "openai", "stripe", "sendgrid", "custom"
    api_key: str  # Encrypted/stored key
    description: Optional[str] = None
    is_active: bool = True
    created_at: datetime = Field(default_factory=datetime.utcnow)
    updated_at: datetime = Field(default_factory=datetime.utcnow)
    created_by: str

class ApiKeyCreate(BaseModel):
    name: str
    key_type: str
    api_key: str
    description: Optional[str] = None

class ApiKeyUpdate(BaseModel):
    name: Optional[str] = None
    api_key: Optional[str] = None
    description: Optional[str] = None
    is_active: Optional[bool] = None

class BulkUserAction(BaseModel):
    user_ids: List[str]
    action: str  # "delete", "block", "unblock", "assign_diet_plan", "assign_workout_plan"
    plan_id: Optional[str] = None  # For assign actions

# ==================== HELPER FUNCTIONS ====================

def serialize_mongo_doc(doc):
    """Convert MongoDB ObjectId to string for JSON serialization"""
    if doc and "_id" in doc:
        doc["_id"] = str(doc["_id"])
    return doc

def serialize_mongo_docs(docs):
    """Convert MongoDB ObjectIds to strings for a list of documents"""
    return [serialize_mongo_doc(doc) for doc in docs]

def calculate_bmi(weight_kg: float, height_cm: float) -> float:
    """Calculate BMI: weight(kg) / height(m)²"""
    if height_cm and height_cm > 0:
        height_m = height_cm / 100
        return round(weight_kg / (height_m ** 2), 1)
    return None

def calculate_bmr(weight_kg: float, height_cm: float, age: int, gender: str) -> float:
    """Calculate BMR using Mifflin-St Jeor equation"""
    if not all([weight_kg, height_cm, age, gender]):
        return None
    
    bmr = (10 * weight_kg) + (6.25 * height_cm) - (5 * age)
    if gender.lower() == "male":
        bmr += 5
    else:
        bmr -= 161
    return round(bmr, 0)

def hash_password(password: str) -> str:
    return pwd_context.hash(password)

def verify_password(plain_password: str, hashed_password: str) -> bool:
    return pwd_context.verify(plain_password, hashed_password)

def create_access_token(data: dict) -> str:
    to_encode = data.copy()
    expire = datetime.utcnow() + timedelta(hours=ACCESS_TOKEN_EXPIRE_HOURS)
    to_encode.update({"exp": expire})
    return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)

async def get_current_user(credentials: HTTPAuthorizationCredentials = Depends(security)) -> dict:
    try:
        token = credentials.credentials
        payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
        user_id = payload.get("user_id")
        if not user_id:
            raise HTTPException(status_code=401, detail="Invalid token")
        
        user = await db.users.find_one({"id": user_id})
        if not user:
            raise HTTPException(status_code=401, detail="User not found")
        return user
    except jwt.ExpiredSignatureError:
        raise HTTPException(status_code=401, detail="Token expired")
    except jwt.InvalidTokenError:
        raise HTTPException(status_code=401, detail="Invalid token")

async def get_admin_user(current_user: dict = Depends(get_current_user)) -> dict:
    if current_user.get("role") != "admin":
        raise HTTPException(status_code=403, detail="Admin access required")
    return current_user

def generate_random_password(length: int = 10) -> str:
    import random
    import string
    chars = string.ascii_letters + string.digits
    return ''.join(random.choice(chars) for _ in range(length))

def get_week_number(date: datetime) -> int:
    """Get week number since user started"""
    return date.isocalendar()[1]

def get_week_start_end(date: datetime = None):
    """Get start and end of the week for a given date"""
    if date is None:
        date = datetime.utcnow()
    start = date - timedelta(days=date.weekday())
    start = start.replace(hour=0, minute=0, second=0, microsecond=0)
    end = start + timedelta(days=7)
    return start, end

# ==================== PUBLIC ROUTES ====================

@api_router.get("/")
async def root():
    return {"message": "Abbas FIT API", "version": "2.0.0"}

@api_router.get("/health")
async def health_check():
    return {"status": "healthy", "timestamp": datetime.utcnow().isoformat()}

@api_router.get("/testimonials")
async def get_testimonials():
    testimonials = await db.testimonials.find({"active": True}).to_list(100)
    return serialize_mongo_docs(testimonials)

@api_router.post("/contact")
async def submit_contact(contact: ContactCreate):
    contact_obj = ContactSubmission(**contact.dict())
    await db.contacts.insert_one(contact_obj.dict())
    return contact_obj.dict()

# ==================== AUTH ROUTES ====================

@api_router.post("/auth/login", response_model=TokenResponse)
async def login(request: LoginRequest):
    user = await db.users.find_one({"email": request.email.lower()})
    if not user or not verify_password(request.password, user["password_hash"]):
        raise HTTPException(status_code=401, detail="Invalid email or password")
    
    # Check if user is blocked
    if user.get("is_blocked") and user.get("role") != "admin":
        raise HTTPException(status_code=403, detail="Your account has been blocked. Please contact admin.")
    
    token = create_access_token({"user_id": user["id"], "role": user["role"]})
    
    # Calculate BMI and BMR
    bmi = None
    bmr = None
    if user.get("weight_kg") and user.get("height_cm"):
        bmi = calculate_bmi(user["weight_kg"], user["height_cm"])
        if user.get("age") and user.get("gender"):
            bmr = calculate_bmr(user["weight_kg"], user["height_cm"], user["age"], user["gender"])
    
    user_response = UserResponse(
        id=user["id"],
        email=user["email"],
        name=user["name"],
        role=user["role"],
        height_cm=user.get("height_cm"),
        weight_kg=user.get("weight_kg"),
        age=user.get("age"),
        gender=user.get("gender"),
        calorie_target=user.get("calorie_target"),
        protein_target=user.get("protein_target"),
        fiber_target=user.get("fiber_target"),
        fat_target=user.get("fat_target"),
        carbs_target=user.get("carbs_target"),
        water_intake_goal=user.get("water_intake_goal"),
        step_goal=user.get("step_goal"),
        fitness_goal=user.get("fitness_goal"),
        target_weight=user.get("target_weight"),
        meal_reminders=user.get("meal_reminders", True),
        water_reminders=user.get("water_reminders", True),
        workout_reminders=user.get("workout_reminders", True),
        profile_photo=user.get("profile_photo"),
        is_blocked=user.get("is_blocked", False),
        bmi=bmi,
        bmr=bmr,
        created_at=user["created_at"]
    )
    
    return TokenResponse(access_token=token, user=user_response)

@api_router.get("/auth/me")
async def get_me(current_user: dict = Depends(get_current_user)):
    bmi = None
    bmr = None
    if current_user.get("weight_kg") and current_user.get("height_cm"):
        bmi = calculate_bmi(current_user["weight_kg"], current_user["height_cm"])
        if current_user.get("age") and current_user.get("gender"):
            bmr = calculate_bmr(current_user["weight_kg"], current_user["height_cm"], 
                              current_user["age"], current_user["gender"])
    
    return {
        **current_user,
        "_id": str(current_user.get("_id", "")),
        "bmi": bmi,
        "bmr": bmr
    }

# ==================== ADMIN - USER MANAGEMENT ====================

@api_router.post("/admin/users")
async def create_user(user: UserCreate, admin: dict = Depends(get_admin_user)):
    existing = await db.users.find_one({"email": user.email.lower()})
    if existing:
        raise HTTPException(status_code=400, detail="Email already registered")
    
    password = user.password if user.password else generate_random_password()
    
    user_obj = {
        "id": str(uuid.uuid4()),
        "email": user.email.lower(),
        "name": user.name,
        "password_hash": hash_password(password),
        "role": user.role,
        "height_cm": user.height_cm,
        "weight_kg": user.weight_kg,
        "age": user.age,
        "gender": user.gender,
        "calorie_target": user.calorie_target,
        "protein_target": user.protein_target,
        "fiber_target": user.fiber_target,
        "fat_target": user.fat_target,
        "carbs_target": user.carbs_target or 250,
        "water_intake_goal": user.water_intake_goal or 3.0,
        "step_goal": user.step_goal or 10000,
        "fitness_goal": user.fitness_goal or "general_fitness",
        "target_weight": user.target_weight,
        "meal_reminders": user.meal_reminders if user.meal_reminders is not None else True,
        "water_reminders": user.water_reminders if user.water_reminders is not None else True,
        "workout_reminders": user.workout_reminders if user.workout_reminders is not None else True,
        "profile_photo": None,
        "created_at": datetime.utcnow()
    }
    
    await db.users.insert_one(user_obj)
    
    return {
        "user": {**user_obj, "bmi": None, "bmr": None},
        "generated_password": password
    }

@api_router.get("/admin/users")
async def get_all_users(admin: dict = Depends(get_admin_user)):
    users = await db.users.find({"role": "user"}).to_list(1000)
    result = []
    for user in users:
        bmi = None
        bmr = None
        if user.get("weight_kg") and user.get("height_cm"):
            bmi = calculate_bmi(user["weight_kg"], user["height_cm"])
            if user.get("age") and user.get("gender"):
                bmr = calculate_bmr(user["weight_kg"], user["height_cm"], user["age"], user["gender"])
        result.append({**user, "_id": str(user.get("_id", "")), "bmi": bmi, "bmr": bmr})
    return result

@api_router.get("/admin/users/{user_id}")
async def get_user_by_id(user_id: str, admin: dict = Depends(get_admin_user)):
    user = await db.users.find_one({"id": user_id})
    if not user:
        raise HTTPException(status_code=404, detail="User not found")
    
    bmi = None
    bmr = None
    if user.get("weight_kg") and user.get("height_cm"):
        bmi = calculate_bmi(user["weight_kg"], user["height_cm"])
        if user.get("age") and user.get("gender"):
            bmr = calculate_bmr(user["weight_kg"], user["height_cm"], user["age"], user["gender"])
    
    return {**user, "_id": str(user.get("_id", "")), "bmi": bmi, "bmr": bmr}

@api_router.put("/admin/users/{user_id}")
async def update_user(user_id: str, update: UserUpdate, admin: dict = Depends(get_admin_user)):
    user = await db.users.find_one({"id": user_id})
    if not user:
        raise HTTPException(status_code=404, detail="User not found")
    
    update_data = {k: v for k, v in update.dict().items() if v is not None}
    if update_data:
        await db.users.update_one({"id": user_id}, {"$set": update_data})
    
    updated_user = await db.users.find_one({"id": user_id})
    bmi = None
    bmr = None
    if updated_user.get("weight_kg") and updated_user.get("height_cm"):
        bmi = calculate_bmi(updated_user["weight_kg"], updated_user["height_cm"])
        if updated_user.get("age") and updated_user.get("gender"):
            bmr = calculate_bmr(updated_user["weight_kg"], updated_user["height_cm"], 
                              updated_user["age"], updated_user["gender"])
    
    return {**updated_user, "_id": str(updated_user.get("_id", "")), "bmi": bmi, "bmr": bmr}

@api_router.delete("/admin/users/{user_id}")
async def delete_user(user_id: str, admin: dict = Depends(get_admin_user)):
    result = await db.users.delete_one({"id": user_id})
    if result.deleted_count == 0:
        raise HTTPException(status_code=404, detail="User not found")
    return {"message": "User deleted successfully"}

# ==================== ADMIN - CONTACTS ====================

@api_router.get("/admin/contacts")
async def get_contacts(admin: dict = Depends(get_admin_user)):
    contacts = await db.contacts.find().sort("created_at", -1).to_list(1000)
    return contacts

@api_router.put("/admin/contacts/{contact_id}/read")
async def mark_contact_read(contact_id: str, admin: dict = Depends(get_admin_user)):
    await db.contacts.update_one({"id": contact_id}, {"$set": {"read": True}})
    return {"message": "Marked as read"}

# ==================== ADMIN - SUPPLEMENTS ====================

@api_router.post("/admin/supplements")
async def create_supplement(supplement: SupplementCreate, admin: dict = Depends(get_admin_user)):
    supplement_obj = Supplement(**supplement.dict(), created_by=admin["id"])
    await db.supplements.insert_one(supplement_obj.dict())
    return supplement_obj.dict()

@api_router.get("/admin/supplements")
async def get_all_supplements(admin: dict = Depends(get_admin_user)):
    supplements = await db.supplements.find().to_list(1000)
    return serialize_mongo_docs(supplements)

@api_router.delete("/admin/supplements/{supplement_id}")
async def delete_supplement(supplement_id: str, admin: dict = Depends(get_admin_user)):
    await db.supplements.delete_one({"id": supplement_id})
    return {"message": "Supplement deleted"}

@api_router.post("/admin/supplements/assign")
async def assign_supplement(assignment: SupplementAssignmentCreate, admin: dict = Depends(get_admin_user)):
    supplement = await db.supplements.find_one({"id": assignment.supplement_id})
    if not supplement:
        raise HTTPException(status_code=404, detail="Supplement not found")
    
    assignment_obj = SupplementAssignment(
        **assignment.dict(),
        supplement_name=supplement["name"],
        assigned_by=admin["id"]
    )
    await db.supplement_assignments.insert_one(assignment_obj.dict())
    return assignment_obj.dict()

@api_router.get("/admin/supplements/assignments/{user_id}")
async def get_user_supplement_assignments(user_id: str, admin: dict = Depends(get_admin_user)):
    assignments = await db.supplement_assignments.find({"user_id": user_id, "active": True}).to_list(100)
    return serialize_mongo_docs(assignments)

@api_router.delete("/admin/supplements/assignments/{assignment_id}")
async def remove_supplement_assignment(assignment_id: str, admin: dict = Depends(get_admin_user)):
    await db.supplement_assignments.update_one({"id": assignment_id}, {"$set": {"active": False}})
    return {"message": "Assignment removed"}

# ==================== ADMIN - DIET PLANS ====================

@api_router.post("/admin/diet-plans")
async def create_diet_plan(plan: DietPlanCreate, admin: dict = Depends(get_admin_user)):
    # If no meals provided, create template with all meal types
    if not plan.meals:
        plan.meals = [
            {"meal_type": mt, "name": mt.replace("_", " ").title(), "recipe": "", "calories": 0}
            for mt in MEAL_TYPES
        ]
    
    plan_obj = DietPlan(**plan.dict(), created_by=admin["id"])
    await db.diet_plans.insert_one(plan_obj.dict())
    return plan_obj.dict()

@api_router.get("/admin/diet-plans")
async def get_all_diet_plans(admin: dict = Depends(get_admin_user)):
    plans = await db.diet_plans.find().to_list(1000)
    return serialize_mongo_docs(plans)

@api_router.put("/admin/diet-plans/{plan_id}")
async def update_diet_plan(plan_id: str, plan: DietPlanCreate, admin: dict = Depends(get_admin_user)):
    await db.diet_plans.update_one({"id": plan_id}, {"$set": plan.dict()})
    updated = await db.diet_plans.find_one({"id": plan_id})
    return serialize_mongo_doc(updated)

@api_router.delete("/admin/diet-plans/{plan_id}")
async def delete_diet_plan(plan_id: str, admin: dict = Depends(get_admin_user)):
    await db.diet_plans.delete_one({"id": plan_id})
    return {"message": "Diet plan deleted"}

# ==================== ADMIN - WORKOUT PLANS ====================

@api_router.post("/admin/workout-plans")
async def create_workout_plan(plan: WorkoutPlanCreate, admin: dict = Depends(get_admin_user)):
    plan_obj = WorkoutPlan(**plan.dict(), created_by=admin["id"])
    await db.workout_plans.insert_one(plan_obj.dict())
    return plan_obj.dict()

@api_router.get("/admin/workout-plans")
async def get_all_workout_plans(admin: dict = Depends(get_admin_user)):
    plans = await db.workout_plans.find().to_list(1000)
    return serialize_mongo_docs(plans)

@api_router.put("/admin/workout-plans/{plan_id}")
async def update_workout_plan(plan_id: str, plan: WorkoutPlanCreate, admin: dict = Depends(get_admin_user)):
    await db.workout_plans.update_one({"id": plan_id}, {"$set": plan.dict()})
    updated = await db.workout_plans.find_one({"id": plan_id})
    return serialize_mongo_doc(updated)

@api_router.delete("/admin/workout-plans/{plan_id}")
async def delete_workout_plan(plan_id: str, admin: dict = Depends(get_admin_user)):
    await db.workout_plans.delete_one({"id": plan_id})
    return {"message": "Workout plan deleted"}

# ==================== ADMIN - SESSIONS ====================

@api_router.get("/admin/sessions")
async def get_all_sessions(admin: dict = Depends(get_admin_user)):
    sessions = await db.sessions.find().sort("date", -1).to_list(1000)
    result = []
    for s in sessions:
        user = await db.users.find_one({"id": s["user_id"]})
        result.append({
            **s,
            "user_name": user["name"] if user else "Unknown",
            "user_email": user["email"] if user else "Unknown"
        })
    return result

@api_router.put("/admin/sessions/{session_id}/status")
async def update_session_status(session_id: str, status: str, admin: dict = Depends(get_admin_user)):
    await db.sessions.update_one({"id": session_id}, {"$set": {"status": status}})
    return {"message": f"Session status updated to {status}"}

# ==================== ADMIN - INSIGHTS ====================

@api_router.get("/admin/insights")
async def get_admin_insights(admin: dict = Depends(get_admin_user)):
    total_users = await db.users.count_documents({"role": "user"})
    total_meals = await db.meals.count_documents({})
    total_activities = await db.activities.count_documents({})
    total_sessions = await db.sessions.count_documents({})
    pending_sessions = await db.sessions.count_documents({"status": "pending"})
    unread_contacts = await db.contacts.count_documents({"read": False})
    
    week_ago = datetime.utcnow() - timedelta(days=7)
    active_users = await db.meals.distinct("user_id", {"created_at": {"$gte": week_ago}})
    adherence_rate = (len(active_users) / total_users * 100) if total_users > 0 else 0
    
    return {
        "total_users": total_users,
        "total_meals_logged": total_meals,
        "total_activities_logged": total_activities,
        "total_sessions": total_sessions,
        "pending_sessions": pending_sessions,
        "unread_contacts": unread_contacts,
        "adherence_rate": round(adherence_rate, 1),
        "active_users_this_week": len(active_users)
    }

@api_router.get("/admin/user-insights/{user_id}")
async def get_user_insights(user_id: str, admin: dict = Depends(get_admin_user)):
    user = await db.users.find_one({"id": user_id})
    if not user:
        raise HTTPException(status_code=404, detail="User not found")
    
    # Get all data
    vitals = await db.vitals.find({"user_id": user_id}).sort("created_at", -1).to_list(100)
    meals = await db.meals.find({"user_id": user_id}).sort("created_at", -1).to_list(1000)
    activities = await db.activities.find({"user_id": user_id}).sort("created_at", -1).to_list(1000)
    water_logs = await db.water_logs.find({"user_id": user_id}).sort("created_at", -1).to_list(1000)
    step_logs = await db.step_logs.find({"user_id": user_id}).sort("created_at", -1).to_list(100)
    supplement_logs = await db.supplement_logs.find({"user_id": user_id}).sort("created_at", -1).to_list(100)
    progress_photos = await db.progress_photos.find({"user_id": user_id}).sort("created_at", -1).to_list(50)
    
    # Weekly comparison
    this_week_start, this_week_end = get_week_start_end()
    last_week_start = this_week_start - timedelta(days=7)
    last_week_end = this_week_start
    
    this_week_meals = [m for m in meals if this_week_start <= m["created_at"] < this_week_end]
    last_week_meals = [m for m in meals if last_week_start <= m["created_at"] < last_week_end]
    
    this_week_activities = [a for a in activities if this_week_start <= a["created_at"] < this_week_end]
    last_week_activities = [a for a in activities if last_week_start <= a["created_at"] < last_week_end]
    
    # Calculate averages
    this_week_calories = sum(m.get("calories", 0) or 0 for m in this_week_meals)
    last_week_calories = sum(m.get("calories", 0) or 0 for m in last_week_meals)
    
    this_week_activity_mins = sum(a.get("duration_minutes", 0) or 0 for a in this_week_activities)
    last_week_activity_mins = sum(a.get("duration_minutes", 0) or 0 for a in last_week_activities)
    
    return {
        "user": {**user, "_id": str(user.get("_id", ""))},
        "vitals_history": vitals,
        "total_meals_logged": len(meals),
        "total_activities_logged": len(activities),
        "total_water_logs": len(water_logs),
        "total_step_logs": len(step_logs),
        "total_supplement_logs": len(supplement_logs),
        "progress_photos": progress_photos,
        "weekly_comparison": {
            "this_week": {
                "calories": this_week_calories,
                "meals_count": len(this_week_meals),
                "activity_minutes": this_week_activity_mins,
                "activities_count": len(this_week_activities)
            },
            "last_week": {
                "calories": last_week_calories,
                "meals_count": len(last_week_meals),
                "activity_minutes": last_week_activity_mins,
                "activities_count": len(last_week_activities)
            },
            "change": {
                "calories": this_week_calories - last_week_calories,
                "activity_minutes": this_week_activity_mins - last_week_activity_mins
            }
        },
        "recent_meals": meals[:10],
        "recent_activities": activities[:10]
    }

# ==================== ADMIN - TESTIMONIALS ====================

@api_router.post("/admin/testimonials")
async def create_testimonial(testimonial: TestimonialCreate, admin: dict = Depends(get_admin_user)):
    testimonial_obj = Testimonial(**testimonial.dict())
    await db.testimonials.insert_one(testimonial_obj.dict())
    return testimonial_obj.dict()

@api_router.delete("/admin/testimonials/{testimonial_id}")
async def delete_testimonial(testimonial_id: str, admin: dict = Depends(get_admin_user)):
    await db.testimonials.delete_one({"id": testimonial_id})
    return {"message": "Testimonial deleted"}

# ==================== ADMIN - NOTIFICATIONS ====================

@api_router.post("/admin/notifications/send")
async def send_notification(user_id: str, title: str, message: str, notification_type: str = "general", admin: dict = Depends(get_admin_user)):
    notification = Notification(
        user_id=user_id,
        title=title,
        message=message,
        type=notification_type
    )
    await db.notifications.insert_one(notification.dict())
    return notification

@api_router.post("/admin/notifications/broadcast")
async def broadcast_notification(title: str, message: str, admin: dict = Depends(get_admin_user)):
    users = await db.users.find({"role": "user"}).to_list(1000)
    for user in users:
        notification = Notification(
            user_id=user["id"],
            title=title,
            message=message,
            type="general"
        )
        await db.notifications.insert_one(notification.dict())
    return {"message": f"Notification sent to {len(users)} users"}

# ==================== USER - VITALS ====================

@api_router.post("/user/vitals")
async def log_vitals(vital: VitalLogCreate, current_user: dict = Depends(get_current_user)):
    height = vital.height_cm or current_user.get("height_cm")
    
    bmi = calculate_bmi(vital.weight_kg, height) if height else None
    bmr = None
    if height and current_user.get("age") and current_user.get("gender"):
        bmr = calculate_bmr(vital.weight_kg, height, current_user["age"], current_user["gender"])
    
    vital_obj = VitalLog(
        **vital.dict(),
        user_id=current_user["id"],
        height_cm=height,
        bmi=bmi,
        bmr=bmr
    )
    await db.vitals.insert_one(vital_obj.dict())
    
    # Update user's current weight
    await db.users.update_one(
        {"id": current_user["id"]},
        {"$set": {"weight_kg": vital.weight_kg}}
    )
    
    return vital_obj.dict()

@api_router.get("/user/vitals")
async def get_vitals(current_user: dict = Depends(get_current_user)):
    vitals = await db.vitals.find({"user_id": current_user["id"]}).sort("created_at", -1).to_list(100)
    return vitals

# ==================== USER - MEALS ====================

@api_router.post("/user/meals")
async def log_meal(meal: MealLogCreate, current_user: dict = Depends(get_current_user)):
    meal_obj = MealLog(**meal.dict(), user_id=current_user["id"])
    await db.meals.insert_one(meal_obj.dict())
    return meal_obj.dict()

@api_router.get("/user/meals")
async def get_meals(current_user: dict = Depends(get_current_user)):
    meals = await db.meals.find({"user_id": current_user["id"]}).sort("created_at", -1).to_list(1000)
    return meals

@api_router.get("/user/meals/today")
async def get_todays_meals(current_user: dict = Depends(get_current_user)):
    today = datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)
    meals = await db.meals.find({
        "user_id": current_user["id"],
        "created_at": {"$gte": today}
    }).sort("created_at", -1).to_list(100)
    return meals

# ==================== USER - WATER ====================

@api_router.post("/user/water")
async def log_water(water: WaterLogCreate, current_user: dict = Depends(get_current_user)):
    water_obj = WaterLog(**water.dict(), user_id=current_user["id"])
    await db.water_logs.insert_one(water_obj.dict())
    return water_obj.dict()

@api_router.get("/user/water/today")
async def get_todays_water(current_user: dict = Depends(get_current_user)):
    today = datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)
    water_logs = await db.water_logs.find({
        "user_id": current_user["id"],
        "created_at": {"$gte": today}
    }).to_list(100)
    
    # Convert ObjectIds to strings
    water_logs = serialize_mongo_docs(water_logs)
    
    total_ml = sum(w.get("amount_ml", 0) for w in water_logs)
    goal = current_user.get("water_intake_goal", 3.0) * 1000  # Convert to ml
    return {
        "logs": water_logs,
        "total_ml": total_ml,
        "goal_ml": goal,
        "progress_percent": round((total_ml / goal) * 100, 1) if goal > 0 else 0
    }

# ==================== USER - STEPS ====================

@api_router.post("/user/steps")
async def log_steps(step: StepLogCreate, current_user: dict = Depends(get_current_user)):
    date = step.date or datetime.utcnow().strftime("%Y-%m-%d")
    
    # Update or create step log for the date
    existing = await db.step_logs.find_one({"user_id": current_user["id"], "date": date})
    if existing:
        await db.step_logs.update_one(
            {"id": existing["id"]},
            {"$set": {"steps": step.steps, "created_at": datetime.utcnow()}}
        )
        return serialize_mongo_doc({**existing, "steps": step.steps})
    
    step_obj = StepLog(
        user_id=current_user["id"],
        steps=step.steps,
        date=date
    )
    await db.step_logs.insert_one(step_obj.dict())
    return step_obj.dict()

@api_router.get("/user/steps/today")
async def get_todays_steps(current_user: dict = Depends(get_current_user)):
    today = datetime.utcnow().strftime("%Y-%m-%d")
    step_log = await db.step_logs.find_one({"user_id": current_user["id"], "date": today})
    goal = current_user.get("step_goal", 10000)
    steps = step_log.get("steps", 0) if step_log else 0
    return {
        "steps": steps,
        "goal": goal,
        "progress_percent": round((steps / goal) * 100, 1) if goal > 0 else 0
    }

@api_router.get("/user/steps/history")
async def get_steps_history(current_user: dict = Depends(get_current_user)):
    step_logs = await db.step_logs.find({"user_id": current_user["id"]}).sort("date", -1).to_list(30)
    return serialize_mongo_docs(step_logs)

# ==================== USER - ACTIVITIES ====================

@api_router.post("/user/activities")
async def log_activity(activity: ActivityLogCreate, current_user: dict = Depends(get_current_user)):
    activity_obj = ActivityLog(**activity.dict(), user_id=current_user["id"])
    await db.activities.insert_one(activity_obj.dict())
    return activity_obj.dict()

@api_router.get("/user/activities")
async def get_activities(current_user: dict = Depends(get_current_user)):
    activities = await db.activities.find({"user_id": current_user["id"]}).sort("created_at", -1).to_list(1000)
    return serialize_mongo_docs(activities)

# ==================== USER - SUPPLEMENTS ====================

@api_router.get("/user/supplements")
async def get_my_supplements(current_user: dict = Depends(get_current_user)):
    assignments = await db.supplement_assignments.find({
        "user_id": current_user["id"],
        "active": True
    }).to_list(100)
    return serialize_mongo_docs(assignments)

@api_router.post("/user/supplements/log")
async def log_supplement_taken(log: SupplementLogCreate, current_user: dict = Depends(get_current_user)):
    assignment = await db.supplement_assignments.find_one({
        "supplement_id": log.supplement_id, 
        "user_id": current_user["id"],
        "active": True
    })
    if not assignment:
        raise HTTPException(status_code=404, detail="Supplement not assigned to you")
    
    log_obj = SupplementLog(
        user_id=current_user["id"],
        supplement_id=log.supplement_id,
        supplement_name=assignment["supplement_name"],
        taken=log.taken
    )
    await db.supplement_logs.insert_one(log_obj.dict())
    return log_obj.dict()

@api_router.get("/user/supplements/today")
async def get_todays_supplement_logs(current_user: dict = Depends(get_current_user)):
    today = datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)
    logs = await db.supplement_logs.find({
        "user_id": current_user["id"],
        "created_at": {"$gte": today}
    }).to_list(100)
    logs = serialize_mongo_docs(logs)
    
    assignments = await db.supplement_assignments.find({
        "user_id": current_user["id"],
        "active": True
    }).to_list(100)
    assignments = serialize_mongo_docs(assignments)
    
    return {
        "assigned": assignments,
        "taken_today": logs,
        "taken_ids": [l["supplement_id"] for l in logs if l.get("taken")]
    }

# ==================== USER - PROGRESS PHOTOS ====================

@api_router.post("/user/progress-photos")
async def upload_progress_photo(photo: ProgressPhotoCreate, current_user: dict = Depends(get_current_user)):
    # Calculate week number
    week_num = photo.week_number or get_week_number(datetime.utcnow())
    
    # Create photo object without duplicate week_number
    photo_data = photo.dict()
    photo_data["week_number"] = week_num
    
    photo_obj = ProgressPhoto(
        **photo_data,
        user_id=current_user["id"]
    )
    await db.progress_photos.insert_one(photo_obj.dict())
    return photo_obj.dict()

@api_router.get("/user/progress-photos")
async def get_progress_photos(current_user: dict = Depends(get_current_user)):
    photos = await db.progress_photos.find({"user_id": current_user["id"]}).sort("created_at", -1).to_list(100)
    return serialize_mongo_docs(photos)

# ==================== USER - PLANS ====================

@api_router.get("/user/diet-plans")
async def get_my_diet_plans(current_user: dict = Depends(get_current_user)):
    plans = await db.diet_plans.find({
        "assigned_to": current_user["id"],
        "active": True
    }).to_list(100)
    return serialize_mongo_docs(plans)

@api_router.get("/user/workout-plans")
async def get_my_workout_plans(current_user: dict = Depends(get_current_user)):
    plans = await db.workout_plans.find({
        "assigned_to": current_user["id"],
        "active": True
    }).to_list(100)
    return serialize_mongo_docs(plans)

# ==================== USER - SESSIONS ====================

@api_router.post("/user/sessions")
async def book_session(booking: SessionBookingCreate, current_user: dict = Depends(get_current_user)):
    existing = await db.sessions.find_one({
        "date": booking.date,
        "time_slot": booking.time_slot,
        "status": {"$ne": "cancelled"}
    })
    if existing:
        raise HTTPException(status_code=400, detail="This time slot is already booked")
    
    session_obj = SessionBooking(**booking.dict(), user_id=current_user["id"])
    await db.sessions.insert_one(session_obj.dict())
    return session_obj.dict()

@api_router.get("/user/sessions")
async def get_my_sessions(current_user: dict = Depends(get_current_user)):
    sessions = await db.sessions.find({"user_id": current_user["id"]}).sort("date", -1).to_list(100)
    return serialize_mongo_docs(sessions)

@api_router.delete("/user/sessions/{session_id}")
async def cancel_session(session_id: str, current_user: dict = Depends(get_current_user)):
    session = await db.sessions.find_one({"id": session_id, "user_id": current_user["id"]})
    if not session:
        raise HTTPException(status_code=404, detail="Session not found")
    
    await db.sessions.update_one({"id": session_id}, {"$set": {"status": "cancelled"}})
    return {"message": "Session cancelled"}

# ==================== USER - NOTIFICATIONS ====================

@api_router.get("/user/notifications")
async def get_my_notifications(current_user: dict = Depends(get_current_user)):
    notifications = await db.notifications.find({"user_id": current_user["id"]}).sort("created_at", -1).to_list(50)
    return notifications

@api_router.get("/user/notifications/unread-count")
async def get_unread_notification_count(current_user: dict = Depends(get_current_user)):
    count = await db.notifications.count_documents({"user_id": current_user["id"], "read": False})
    return {"count": count}

@api_router.put("/user/notifications/{notification_id}/read")
async def mark_notification_read(notification_id: str, current_user: dict = Depends(get_current_user)):
    await db.notifications.update_one(
        {"id": notification_id, "user_id": current_user["id"]},
        {"$set": {"read": True}}
    )
    return {"message": "Marked as read"}

@api_router.put("/user/notifications/read-all")
async def mark_all_notifications_read(current_user: dict = Depends(get_current_user)):
    await db.notifications.update_many(
        {"user_id": current_user["id"], "read": False},
        {"$set": {"read": True}}
    )
    return {"message": "All notifications marked as read"}

# ==================== USER - DASHBOARD STATS ====================

@api_router.get("/user/dashboard-stats")
async def get_dashboard_stats(current_user: dict = Depends(get_current_user)):
    today = datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)
    week_ago = today - timedelta(days=7)
    
    # Today's data
    todays_meals = await db.meals.find({
        "user_id": current_user["id"],
        "created_at": {"$gte": today}
    }).to_list(100)
    
    todays_activities = await db.activities.find({
        "user_id": current_user["id"],
        "created_at": {"$gte": today}
    }).to_list(100)
    
    todays_water = await db.water_logs.find({
        "user_id": current_user["id"],
        "created_at": {"$gte": today}
    }).to_list(100)
    
    todays_steps = await db.step_logs.find_one({
        "user_id": current_user["id"],
        "date": today.strftime("%Y-%m-%d")
    })
    
    # Calculate totals
    calories_consumed = sum(m.get("calories", 0) or 0 for m in todays_meals)
    protein_consumed = sum(m.get("protein_g", 0) or 0 for m in todays_meals)
    carbs_consumed = sum(m.get("carbs_g", 0) or 0 for m in todays_meals)
    fat_consumed = sum(m.get("fat_g", 0) or 0 for m in todays_meals)
    fiber_consumed = sum(m.get("fiber_g", 0) or 0 for m in todays_meals)
    calories_burned = sum(a.get("calories_burned", 0) or 0 for a in todays_activities)
    water_ml = sum(w.get("amount_ml", 0) for w in todays_water)
    steps = todays_steps.get("steps", 0) if todays_steps else 0
    
    # Weekly progress (vitals)
    weekly_vitals = await db.vitals.find({
        "user_id": current_user["id"],
        "created_at": {"$gte": week_ago}
    }).sort("created_at", 1).to_list(100)
    
    # Upcoming sessions
    upcoming_sessions = await db.sessions.find({
        "user_id": current_user["id"],
        "date": {"$gte": today.strftime("%Y-%m-%d")},
        "status": {"$ne": "cancelled"}
    }).sort("date", 1).to_list(5)
    
    # Latest vital
    latest_vital = await db.vitals.find_one(
        {"user_id": current_user["id"]},
        sort=[("created_at", -1)]
    )
    
    # Unread notifications
    unread_notifications = await db.notifications.count_documents({
        "user_id": current_user["id"],
        "read": False
    })
    
    return {
        "today": {
            "calories_consumed": calories_consumed,
            "protein_consumed": round(protein_consumed, 1),
            "carbs_consumed": round(carbs_consumed, 1),
            "fat_consumed": round(fat_consumed, 1),
            "fiber_consumed": round(fiber_consumed, 1),
            "calories_burned": calories_burned,
            "water_ml": water_ml,
            "steps": steps,
            "meals_count": len(todays_meals),
            "activities_count": len(todays_activities)
        },
        "targets": {
            "calories": current_user.get("calorie_target", 2000),
            "protein": current_user.get("protein_target", 150),
            "carbs": current_user.get("carbs_target", 250),
            "fat": current_user.get("fat_target", 65),
            "fiber": current_user.get("fiber_target", 30),
            "water_ml": (current_user.get("water_intake_goal", 3.0) * 1000),
            "steps": current_user.get("step_goal", 10000)
        },
        "fitness_goal": current_user.get("fitness_goal", "general_fitness"),
        "target_weight": current_user.get("target_weight"),
        "latest_vital": latest_vital,
        "weekly_vitals": weekly_vitals,
        "upcoming_sessions": upcoming_sessions,
        "unread_notifications": unread_notifications
    }

# ==================== USER - WEEKLY COMPARISON ====================

@api_router.get("/user/weekly-comparison")
async def get_weekly_comparison(current_user: dict = Depends(get_current_user)):
    this_week_start, this_week_end = get_week_start_end()
    last_week_start = this_week_start - timedelta(days=7)
    last_week_end = this_week_start
    
    # This week data
    this_week_meals = await db.meals.find({
        "user_id": current_user["id"],
        "created_at": {"$gte": this_week_start, "$lt": this_week_end}
    }).to_list(1000)
    
    this_week_activities = await db.activities.find({
        "user_id": current_user["id"],
        "created_at": {"$gte": this_week_start, "$lt": this_week_end}
    }).to_list(1000)
    
    this_week_water = await db.water_logs.find({
        "user_id": current_user["id"],
        "created_at": {"$gte": this_week_start, "$lt": this_week_end}
    }).to_list(1000)
    
    # Last week data
    last_week_meals = await db.meals.find({
        "user_id": current_user["id"],
        "created_at": {"$gte": last_week_start, "$lt": last_week_end}
    }).to_list(1000)
    
    last_week_activities = await db.activities.find({
        "user_id": current_user["id"],
        "created_at": {"$gte": last_week_start, "$lt": last_week_end}
    }).to_list(1000)
    
    last_week_water = await db.water_logs.find({
        "user_id": current_user["id"],
        "created_at": {"$gte": last_week_start, "$lt": last_week_end}
    }).to_list(1000)
    
    # Calculate metrics
    def calc_meal_metrics(meals):
        return {
            "total_calories": sum(m.get("calories", 0) or 0 for m in meals),
            "total_protein": sum(m.get("protein_g", 0) or 0 for m in meals),
            "total_carbs": sum(m.get("carbs_g", 0) or 0 for m in meals),
            "total_fat": sum(m.get("fat_g", 0) or 0 for m in meals),
            "meals_count": len(meals)
        }
    
    def calc_activity_metrics(activities):
        return {
            "total_duration": sum(a.get("duration_minutes", 0) or 0 for a in activities),
            "total_calories_burned": sum(a.get("calories_burned", 0) or 0 for a in activities),
            "activities_count": len(activities)
        }
    
    this_week = {
        "meals": calc_meal_metrics(this_week_meals),
        "activities": calc_activity_metrics(this_week_activities),
        "water_ml": sum(w.get("amount_ml", 0) for w in this_week_water)
    }
    
    last_week = {
        "meals": calc_meal_metrics(last_week_meals),
        "activities": calc_activity_metrics(last_week_activities),
        "water_ml": sum(w.get("amount_ml", 0) for w in last_week_water)
    }
    
    # Calculate changes
    changes = {
        "calories": this_week["meals"]["total_calories"] - last_week["meals"]["total_calories"],
        "protein": this_week["meals"]["total_protein"] - last_week["meals"]["total_protein"],
        "activity_minutes": this_week["activities"]["total_duration"] - last_week["activities"]["total_duration"],
        "calories_burned": this_week["activities"]["total_calories_burned"] - last_week["activities"]["total_calories_burned"],
        "water_ml": this_week["water_ml"] - last_week["water_ml"]
    }
    
    return {
        "this_week": this_week,
        "last_week": last_week,
        "changes": changes,
        "period": {
            "this_week_start": this_week_start.isoformat(),
            "this_week_end": this_week_end.isoformat(),
            "last_week_start": last_week_start.isoformat(),
            "last_week_end": last_week_end.isoformat()
        }
    }

# ==================== USER - PROFILE ====================

@api_router.put("/user/profile")
async def update_profile(update: UserUpdate, current_user: dict = Depends(get_current_user)):
    update_data = {k: v for k, v in update.dict().items() if v is not None}
    if update_data:
        await db.users.update_one({"id": current_user["id"]}, {"$set": update_data})
    
    updated_user = await db.users.find_one({"id": current_user["id"]})
    bmi = None
    bmr = None
    if updated_user.get("weight_kg") and updated_user.get("height_cm"):
        bmi = calculate_bmi(updated_user["weight_kg"], updated_user["height_cm"])
        if updated_user.get("age") and updated_user.get("gender"):
            bmr = calculate_bmr(updated_user["weight_kg"], updated_user["height_cm"], 
                              updated_user["age"], updated_user["gender"])
    
    return {**updated_user, "_id": str(updated_user.get("_id", "")), "bmi": bmi, "bmr": bmr}

# ==================== AI ROUTES ====================

@api_router.post("/ai/analyze-meal")
async def analyze_meal(
    description: str = Form(...),
    photo_base64: Optional[str] = Form(None),
    current_user: dict = Depends(get_current_user)
):
    """Analyze meal using Gemini AI"""
    try:
        from emergentintegrations.llm.chat import LlmChat, UserMessage, ImageContent
        
        chat = LlmChat(
            api_key=EMERGENT_LLM_KEY,
            session_id=f"meal-analysis-{current_user['id']}-{uuid.uuid4()}",
            system_message="""You are a nutritionist AI specializing in Indian foods. Analyze the meal described or shown and provide nutritional information.
            
            Return ONLY a valid JSON object with this exact structure:
            {
                "food_items": ["item1", "item2"],
                "calories": 500,
                "protein_g": 20,
                "carbs_g": 60,
                "fat_g": 15,
                "fiber_g": 8,
                "suggestions": ["suggestion1", "suggestion2"],
                "health_rating": 7
            }
            
            Be accurate with Indian foods like dal, chapati, rice, sabzi, etc. Health rating is 1-10."""
        ).with_model("gemini", "gemini-2.5-flash")
        
        message_content = f"Analyze this meal: {description}"
        
        if photo_base64:
            if "," in photo_base64:
                photo_base64 = photo_base64.split(",")[1]
            
            image_content = ImageContent(image_base64=photo_base64)
            user_message = UserMessage(
                text=message_content,
                image_contents=[image_content]
            )
        else:
            user_message = UserMessage(text=message_content)
        
        response = await chat.send_message(user_message)
        
        try:
            import re
            json_match = re.search(r'\{[^{}]*\}', response, re.DOTALL)
            if json_match:
                analysis = json.loads(json_match.group())
            else:
                analysis = json.loads(response)
        except json.JSONDecodeError:
            analysis = {
                "food_items": [description],
                "calories": 300,
                "protein_g": 10,
                "carbs_g": 40,
                "fat_g": 10,
                "fiber_g": 5,
                "suggestions": ["Unable to fully analyze, using estimates"],
                "health_rating": 5,
                "raw_response": response
            }
        
        return analysis
        
    except Exception as e:
        logger.error(f"AI meal analysis error: {str(e)}")
        raise HTTPException(status_code=500, detail=f"AI analysis failed: {str(e)}")

@api_router.post("/ai/predict-goals")
async def predict_goals(current_user: dict = Depends(get_current_user)):
    """Generate AI goal predictions based on user data"""
    try:
        from emergentintegrations.llm.chat import LlmChat, UserMessage
        
        # Get user's history
        vitals = await db.vitals.find({"user_id": current_user["id"]}).sort("created_at", -1).to_list(30)
        meals = await db.meals.find({"user_id": current_user["id"]}).sort("created_at", -1).to_list(100)
        activities = await db.activities.find({"user_id": current_user["id"]}).sort("created_at", -1).to_list(100)
        
        avg_calories = sum(m.get("calories", 0) or 0 for m in meals) / max(len(meals), 1)
        avg_protein = sum(m.get("protein_g", 0) or 0 for m in meals) / max(len(meals), 1)
        total_activities = len(activities)
        
        weight_trend = []
        for v in vitals[:10]:
            weight_trend.append({"weight": v.get("weight_kg"), "date": str(v.get("created_at"))})
        
        user_context = f"""
        User Profile:
        - Name: {current_user.get('name')}
        - Age: {current_user.get('age', 'Unknown')}
        - Gender: {current_user.get('gender', 'Unknown')}
        - Current Weight: {current_user.get('weight_kg', 'Unknown')} kg
        - Height: {current_user.get('height_cm', 'Unknown')} cm
        - Fitness Goal: {current_user.get('fitness_goal', 'general_fitness')}
        - Target Weight: {current_user.get('target_weight', 'Not set')} kg
        - Calorie Target: {current_user.get('calorie_target', 2000)} kcal
        - Protein Target: {current_user.get('protein_target', 150)} g
        - Step Goal: {current_user.get('step_goal', 10000)} steps
        
        Recent Data:
        - Average Daily Calories: {round(avg_calories)} kcal
        - Average Daily Protein: {round(avg_protein, 1)} g
        - Total Activities Logged: {total_activities}
        - Weight History: {weight_trend[:5]}
        """
        
        chat = LlmChat(
            api_key=EMERGENT_LLM_KEY,
            session_id=f"goal-prediction-{current_user['id']}-{uuid.uuid4()}",
            system_message="""You are a fitness coach AI. Based on user data, predict realistic fitness goals.
            
            Return ONLY a valid JSON object:
            {
                "goal_type": "weight_loss" or "muscle_gain" or "maintenance",
                "prediction": "Clear, motivating prediction statement",
                "target_value": 75.0,
                "timeline_weeks": 8,
                "confidence": 0.75,
                "recommendations": ["recommendation1", "recommendation2", "recommendation3"],
                "weekly_milestones": ["Week 1-2: ...", "Week 3-4: ..."]
            }
            
            Be realistic and encouraging. Consider Indian lifestyle and food habits."""
        ).with_model("gemini", "gemini-2.5-flash")
        
        response = await chat.send_message(UserMessage(text=user_context))
        
        try:
            import re
            json_match = re.search(r'\{[^{}]*"goal_type"[^{}]*\}', response, re.DOTALL)
            if json_match:
                prediction = json.loads(json_match.group())
            else:
                prediction = json.loads(response)
        except json.JSONDecodeError:
            prediction = {
                "goal_type": current_user.get("fitness_goal", "maintenance"),
                "prediction": "Keep up your current routine and stay consistent!",
                "target_value": current_user.get("target_weight") or current_user.get("weight_kg", 70),
                "timeline_weeks": 4,
                "confidence": 0.6,
                "recommendations": [
                    "Log meals consistently",
                    "Stay active daily",
                    "Track your progress weekly"
                ],
                "raw_response": response
            }
        
        goal_obj = AIGoalPrediction(
            user_id=current_user["id"],
            **prediction
        )
        await db.goal_predictions.insert_one(goal_obj.dict())
        
        return prediction
        
    except Exception as e:
        logger.error(f"AI goal prediction error: {str(e)}")
        raise HTTPException(status_code=500, detail=f"AI prediction failed: {str(e)}")

@api_router.get("/user/goal-predictions")
async def get_goal_predictions(current_user: dict = Depends(get_current_user)):
    predictions = await db.goal_predictions.find(
        {"user_id": current_user["id"]}
    ).sort("created_at", -1).to_list(10)
    return predictions

# ==================== APP SETTINGS ROUTES ====================

@api_router.get("/settings")
async def get_app_settings():
    """Public endpoint to get app settings for frontend"""
    settings = await db.app_settings.find_one({"id": "app_settings"})
    if not settings:
        # Return default settings
        default_settings = AppSettings()
        return default_settings.dict()
    return serialize_mongo_doc(settings)

@api_router.get("/admin/settings")
async def get_admin_settings(admin: dict = Depends(get_admin_user)):
    """Admin endpoint to get full app settings"""
    settings = await db.app_settings.find_one({"id": "app_settings"})
    if not settings:
        default_settings = AppSettings()
        await db.app_settings.insert_one(default_settings.dict())
        return default_settings.dict()
    return serialize_mongo_doc(settings)

@api_router.put("/admin/settings")
async def update_app_settings(update: AppSettingsUpdate, admin: dict = Depends(get_admin_user)):
    """Update app settings"""
    update_data = {k: v for k, v in update.dict().items() if v is not None}
    update_data["updated_at"] = datetime.utcnow()
    update_data["updated_by"] = admin["id"]
    
    settings = await db.app_settings.find_one({"id": "app_settings"})
    if not settings:
        new_settings = AppSettings(**update_data)
        await db.app_settings.insert_one(new_settings.dict())
        return new_settings.dict()
    
    await db.app_settings.update_one({"id": "app_settings"}, {"$set": update_data})
    updated = await db.app_settings.find_one({"id": "app_settings"})
    return serialize_mongo_doc(updated)

# ==================== LEGAL PAGES ROUTES ====================

@api_router.get("/legal/{page_type}")
async def get_legal_page(page_type: str):
    """Public endpoint to get legal page content"""
    page = await db.legal_pages.find_one({"page_type": page_type, "is_active": True})
    if not page:
        raise HTTPException(status_code=404, detail=f"Legal page '{page_type}' not found")
    return serialize_mongo_doc(page)

@api_router.get("/admin/legal")
async def get_all_legal_pages(admin: dict = Depends(get_admin_user)):
    """Get all legal pages for admin"""
    pages = await db.legal_pages.find().to_list(100)
    return serialize_mongo_docs(pages)

@api_router.post("/admin/legal")
async def create_legal_page(page: LegalPageCreate, admin: dict = Depends(get_admin_user)):
    """Create or update a legal page"""
    existing = await db.legal_pages.find_one({"page_type": page.page_type})
    if existing:
        # Update existing page
        update_data = {
            "title": page.title,
            "content": page.content,
            "updated_at": datetime.utcnow(),
            "updated_by": admin["id"]
        }
        await db.legal_pages.update_one({"page_type": page.page_type}, {"$set": update_data})
        updated = await db.legal_pages.find_one({"page_type": page.page_type})
        return serialize_mongo_doc(updated)
    
    page_obj = LegalPage(**page.dict(), updated_by=admin["id"])
    await db.legal_pages.insert_one(page_obj.dict())
    return page_obj.dict()

@api_router.put("/admin/legal/{page_id}")
async def update_legal_page(page_id: str, update: LegalPageUpdate, admin: dict = Depends(get_admin_user)):
    """Update a legal page"""
    update_data = {k: v for k, v in update.dict().items() if v is not None}
    update_data["updated_at"] = datetime.utcnow()
    update_data["updated_by"] = admin["id"]
    
    await db.legal_pages.update_one({"id": page_id}, {"$set": update_data})
    updated = await db.legal_pages.find_one({"id": page_id})
    return serialize_mongo_doc(updated)

@api_router.delete("/admin/legal/{page_id}")
async def delete_legal_page(page_id: str, admin: dict = Depends(get_admin_user)):
    """Delete a legal page"""
    await db.legal_pages.delete_one({"id": page_id})
    return {"message": "Legal page deleted"}

# ==================== API KEYS ROUTES ====================

def mask_api_key(key: str) -> str:
    """Mask API key for display (show first 4 and last 4 characters)"""
    if len(key) <= 8:
        return "*" * len(key)
    return key[:4] + "*" * (len(key) - 8) + key[-4:]

@api_router.get("/admin/api-keys")
async def get_api_keys(admin: dict = Depends(get_admin_user)):
    """Get all API keys (masked)"""
    keys = await db.api_keys.find().to_list(100)
    # Mask the keys for display
    for key in keys:
        key["api_key_masked"] = mask_api_key(key.get("api_key", ""))
        key["api_key"] = None  # Don't send actual key in list view
    return serialize_mongo_docs(keys)

@api_router.post("/admin/api-keys")
async def create_api_key(key_data: ApiKeyCreate, admin: dict = Depends(get_admin_user)):
    """Create a new API key"""
    key_obj = ApiKey(**key_data.dict(), created_by=admin["id"])
    await db.api_keys.insert_one(key_obj.dict())
    
    # Return with masked key
    result = key_obj.dict()
    result["api_key_masked"] = mask_api_key(result["api_key"])
    result["api_key"] = None
    return result

@api_router.put("/admin/api-keys/{key_id}")
async def update_api_key(key_id: str, update: ApiKeyUpdate, admin: dict = Depends(get_admin_user)):
    """Update an API key"""
    update_data = {k: v for k, v in update.dict().items() if v is not None}
    update_data["updated_at"] = datetime.utcnow()
    
    await db.api_keys.update_one({"id": key_id}, {"$set": update_data})
    updated = await db.api_keys.find_one({"id": key_id})
    if updated:
        updated["api_key_masked"] = mask_api_key(updated.get("api_key", ""))
        updated["api_key"] = None
    return serialize_mongo_doc(updated)

@api_router.delete("/admin/api-keys/{key_id}")
async def delete_api_key(key_id: str, admin: dict = Depends(get_admin_user)):
    """Delete an API key"""
    await db.api_keys.delete_one({"id": key_id})
    return {"message": "API key deleted"}

@api_router.get("/admin/api-keys/{key_id}/reveal")
async def reveal_api_key(key_id: str, admin: dict = Depends(get_admin_user)):
    """Reveal the actual API key (admin only)"""
    key = await db.api_keys.find_one({"id": key_id})
    if not key:
        raise HTTPException(status_code=404, detail="API key not found")
    return {"api_key": key.get("api_key")}

# ==================== BULK USER ACTIONS ====================

@api_router.post("/admin/users/bulk-action")
async def bulk_user_action(action_data: BulkUserAction, admin: dict = Depends(get_admin_user)):
    """Perform bulk actions on users"""
    user_ids = action_data.user_ids
    action = action_data.action
    
    if not user_ids:
        raise HTTPException(status_code=400, detail="No users selected")
    
    results = {"success": 0, "failed": 0, "action": action}
    
    if action == "delete":
        # Don't allow deleting admin users
        result = await db.users.delete_many({
            "id": {"$in": user_ids},
            "role": {"$ne": "admin"}
        })
        results["success"] = result.deleted_count
        results["failed"] = len(user_ids) - result.deleted_count
        
    elif action == "block":
        result = await db.users.update_many(
            {"id": {"$in": user_ids}, "role": {"$ne": "admin"}},
            {"$set": {"is_blocked": True}}
        )
        results["success"] = result.modified_count
        results["failed"] = len(user_ids) - result.modified_count
        
    elif action == "unblock":
        result = await db.users.update_many(
            {"id": {"$in": user_ids}},
            {"$set": {"is_blocked": False}}
        )
        results["success"] = result.modified_count
        results["failed"] = len(user_ids) - result.modified_count
        
    elif action == "assign_diet_plan":
        if not action_data.plan_id:
            raise HTTPException(status_code=400, detail="Plan ID required for assignment")
        
        plan = await db.diet_plans.find_one({"id": action_data.plan_id})
        if not plan:
            raise HTTPException(status_code=404, detail="Diet plan not found")
        
        # Add users to the plan's assigned_to list
        current_assigned = plan.get("assigned_to", [])
        new_assigned = list(set(current_assigned + user_ids))
        
        await db.diet_plans.update_one(
            {"id": action_data.plan_id},
            {"$set": {"assigned_to": new_assigned}}
        )
        results["success"] = len(user_ids)
        
    elif action == "assign_workout_plan":
        if not action_data.plan_id:
            raise HTTPException(status_code=400, detail="Plan ID required for assignment")
        
        plan = await db.workout_plans.find_one({"id": action_data.plan_id})
        if not plan:
            raise HTTPException(status_code=404, detail="Workout plan not found")
        
        current_assigned = plan.get("assigned_to", [])
        new_assigned = list(set(current_assigned + user_ids))
        
        await db.workout_plans.update_one(
            {"id": action_data.plan_id},
            {"$set": {"assigned_to": new_assigned}}
        )
        results["success"] = len(user_ids)
        
    else:
        raise HTTPException(status_code=400, detail=f"Unknown action: {action}")
    
    return results

@api_router.put("/admin/users/{user_id}/block")
async def block_user(user_id: str, admin: dict = Depends(get_admin_user)):
    """Block a single user"""
    user = await db.users.find_one({"id": user_id})
    if not user:
        raise HTTPException(status_code=404, detail="User not found")
    if user.get("role") == "admin":
        raise HTTPException(status_code=400, detail="Cannot block admin users")
    
    await db.users.update_one({"id": user_id}, {"$set": {"is_blocked": True}})
    return {"message": "User blocked successfully"}

@api_router.put("/admin/users/{user_id}/unblock")
async def unblock_user(user_id: str, admin: dict = Depends(get_admin_user)):
    """Unblock a single user"""
    await db.users.update_one({"id": user_id}, {"$set": {"is_blocked": False}})
    return {"message": "User unblocked successfully"}

# ==================== INITIALIZATION ====================

@app.on_event("startup")
async def startup_event():
    """Initialize database with default admin user"""
    admin = await db.users.find_one({"email": "admin@abbasfit.com"})
    if not admin:
        admin_user = {
            "id": str(uuid.uuid4()),
            "email": "admin@abbasfit.com",
            "name": "Abbas",
            "password_hash": hash_password("admin"),
            "role": "admin",
            "height_cm": 175,
            "weight_kg": 80,
            "age": 35,
            "gender": "male",
            "calorie_target": 2500,
            "protein_target": 180,
            "fiber_target": 35,
            "fat_target": 80,
            "carbs_target": 300,
            "water_intake_goal": 3.5,
            "step_goal": 12000,
            "fitness_goal": "maintenance",
            "target_weight": 78,
            "meal_reminders": True,
            "water_reminders": True,
            "workout_reminders": True,
            "profile_photo": None,
            "created_at": datetime.utcnow()
        }
        await db.users.insert_one(admin_user)
        logger.info("Default admin user created: admin@abbasfit.com / admin")
    
    # Add sample testimonials if none exist
    testimonials_count = await db.testimonials.count_documents({})
    if testimonials_count == 0:
        sample_testimonials = [
            {
                "id": str(uuid.uuid4()),
                "name": "Rahul Sharma",
                "content": "Abbas transformed my fitness journey! Lost 15kg in 4 months with his personalized diet and workout plans. Best trainer in the city!",
                "rating": 5,
                "active": True,
                "created_at": datetime.utcnow()
            },
            {
                "id": str(uuid.uuid4()),
                "name": "Priya Patel",
                "content": "As a working professional, I thought I'd never have time for fitness. Abbas designed a program that fits my schedule perfectly. Down 2 dress sizes!",
                "rating": 5,
                "active": True,
                "created_at": datetime.utcnow()
            },
            {
                "id": str(uuid.uuid4()),
                "name": "Amit Kumar",
                "content": "The AI meal tracking is amazing! It recognizes Indian foods perfectly. Abbas FIT helped me gain 8kg of lean muscle in 6 months.",
                "rating": 5,
                "active": True,
                "created_at": datetime.utcnow()
            }
        ]
        await db.testimonials.insert_many(sample_testimonials)
        logger.info("Sample testimonials created")
    
    # Add sample supplements if none exist
    supplements_count = await db.supplements.count_documents({})
    if supplements_count == 0:
        sample_supplements = [
            {"id": str(uuid.uuid4()), "name": "Whey Protein", "description": "High-quality protein powder", "dosage": "1 scoop (30g)", "timing": "Post workout", "created_by": "system", "created_at": datetime.utcnow()},
            {"id": str(uuid.uuid4()), "name": "Creatine Monohydrate", "description": "Muscle strength and power", "dosage": "5g", "timing": "With breakfast", "created_by": "system", "created_at": datetime.utcnow()},
            {"id": str(uuid.uuid4()), "name": "Multivitamin", "description": "Daily vitamin supplement", "dosage": "1 tablet", "timing": "With breakfast", "created_by": "system", "created_at": datetime.utcnow()},
            {"id": str(uuid.uuid4()), "name": "Fish Oil", "description": "Omega-3 fatty acids", "dosage": "1000mg", "timing": "With meals", "created_by": "system", "created_at": datetime.utcnow()},
            {"id": str(uuid.uuid4()), "name": "BCAA", "description": "Branch Chain Amino Acids", "dosage": "5g", "timing": "During workout", "created_by": "system", "created_at": datetime.utcnow()},
        ]
        await db.supplements.insert_many(sample_supplements)
        logger.info("Sample supplements created")
    
    logger.info("Abbas FIT API v2.0 started successfully!")

# Include the router
app.include_router(api_router)

# CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_credentials=True,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.on_event("shutdown")
async def shutdown_db_client():
    client.close()
