import type {
  UserRole,
  MembershipType,
  MembershipStatus,
  Gender,
  ActivityLevel,
  GoalType,
  GoalStatus,
  PriorityLevel,
  AttendanceType,
  PaymentStatus,
  PaymentMethod,
  NotificationType,
  GymStatus,
  RecommendationStatus,
} from "../constants";

export interface User {
  id: string;
  email: string;
  firstName: string;
  lastName: string;
  password?: string;
  phone?: string;
  role: UserRole;
  gymId?: string;
  imageUrl?: string;
  expoPushToken?: string;
  deviceType?: "ios" | "android";
  createdAt: Date;
  updatedAt: Date;
}

export interface Client {
  id: string;
  userId: string;
  user?: User;
  membershipType: MembershipType;
  membershipStatus: MembershipStatus;
  joinDate: Date;
  lastVisit?: Date;
  emergencyContact?: {
    name: string;
    phone: string;
    relationship: string;
  };
}

export interface FitnessProfile {
  id: string;
  userId: string;
  height?: number; // in cm (fixed from string to number)
  weight?: number; // in kg (fixed from string to number)
  age?: number; // (fixed from string to number)
  gender?: Gender;
  activityLevel?: ActivityLevel;
  fitnessGoals?: string[];
  medicalConditions?: string;
  allergies?: string;
  injuries?: string;
  createdAt: Date;
  updatedAt: Date;
}

export interface Attendance {
  id: string;
  userId: string;
  clientId?: string;
  client?: Client;
  checkInTime: Date;
  checkOutTime?: Date;
  type: AttendanceType;
  notes?: string;
  createdAt?: Date;
}

export interface Recommendation {
  id: string;
  userId: string;
  fitnessProfileId: string;
  recommendationText: string;
  activityPlan: string;
  dietPlan: string;
  status: RecommendationStatus;
  generatedAt: Date;
  approvedAt?: Date;
  approvedBy?: string;
  createdAt: Date;
  updatedAt: Date;
}

export interface FitnessGoal {
  id: string;
  userId: string;
  fitnessProfileId?: string;
  goalType: GoalType;
  title: string;
  description?: string;
  targetValue?: number;
  currentValue?: number;
  unit?: string;
  startDate: Date;
  targetDate?: Date;
  completedDate?: Date;
  status: GoalStatus;
  progress: number;
  priority: PriorityLevel;
  notes?: string;
  createdAt: Date;
  updatedAt: Date;
}

export interface Payment {
  id: string;
  clientId: string;
  client?: Client; // Made optional (was required)
  amount: number;
  currency: string;
  status: PaymentStatus;
  paymentMethod: PaymentMethod;
  dueDate: Date;
  paidAt?: Date;
  description: string;
}

export interface Notification {
  id: string;
  userId: string;
  title: string;
  message: string;
  type: NotificationType;
  read: boolean;
  createdAt: Date;
}

export interface Gym {
  id: string;
  name: string;
  location?: string;
  status: GymStatus;
  adminUserId: string;
}

export interface Invitation {
  id: string;
  inviterUserId: string;
  inviteeEmail: string;
  roleAssigned: UserRole;
  gymId: string;
  token: string;
  status: "sent" | "accepted" | "expired";
  expiresAt: Date;
  createdAt: Date;
}

export interface TrainerClient {
  id: string;
  trainerUserId: string;
  clientUserId: string;
  gymId: string;
  createdAt: Date;
}

export interface DailyNutrition {
  id: string;
  userId: string;
  date: string; // YYYY-MM-DD
  totalCalories: number;
  calorieGoal: number;
  meals?: Array<{
    type: "breakfast" | "lunch" | "dinner" | "snack";
    name: string;
    calories: number;
    time?: string;
  }>;
  createdAt: Date;
  updatedAt: Date;
}

export interface MealEntry {
  id: string;
  userId: string;
  dailyNutritionId?: string;
  mealType: "breakfast" | "lunch" | "dinner" | "snack";
  foodName: string;
  calories: number;
  protein?: number; // grams
  carbs?: number; // grams
  fats?: number; // grams
  timestamp: Date;
  createdAt: Date;
}

export interface DailyHydration {
  id: string;
  userId: string;
  date: string; // YYYY-MM-DD
  totalWater: number; // ml
  waterGoal: number; // ml
  entries?: Array<{
    amount: number;
    time: string;
  }>;
  createdAt: Date;
  updatedAt: Date;
}

export interface FitnessProfileHistory {
  id: string;
  userId: string;
  fitnessProfileId: string;
  changeType:
    | "weight"
    | "height"
    | "age"
    | "activity_level"
    | "goals"
    | "medical"
    | "other";
  fieldName: string;
  previousValue?: string;
  newValue?: string;
  changedAt: Date;
  createdAt: Date;
}

export interface TrainerClientAssignment {
  id: string;
  trainerId: string;
  clientId: string;
  assignedAt: Date;
  assignedBy?: string;
  isActive: boolean;
  createdAt: Date;
  updatedAt: Date;
}

// User Report Types
export interface WeeklyCheckInStats {
  weekStart: string; // ISO 8601 date (YYYY-MM-DD)
  weekEnd: string; // ISO 8601 date (YYYY-MM-DD)
  totalCheckIns: number;
  totalTimeMinutes: number;
  averageDurationMinutes: number;
  checkInsByType: {
    type: AttendanceType;
    count: number;
  }[];
}

export interface NutritionSummary {
  date: string; // YYYY-MM-DD
  totalCalories: number;
  calorieGoal: number;
  caloriesDelta: number; // totalCalories - calorieGoal
  mealsCount: number;
}

export interface HydrationSummary {
  date: string; // YYYY-MM-DD
  totalWater: number; // ml
  waterGoal: number; // ml
  hydrationPercentage: number; // (totalWater / waterGoal) * 100
}

export interface GoalSummary {
  active: FitnessGoal[];
  completed: FitnessGoal[];
  totalActive: number;
  totalCompleted: number;
  averageProgress: number; // Average progress of active goals
}

export interface ProfileChangeSummary {
  changeType: FitnessProfileHistory["changeType"];
  fieldName: string;
  previousValue?: string;
  newValue?: string;
  changedAt: Date;
}

export interface RecommendationSummary {
  accepted: Recommendation[];
  rejected: Recommendation[];
  pending: Recommendation[];
  totalAccepted: number;
  totalRejected: number;
  totalPending: number;
}

export interface UserReport {
  userId: string;
  user: User;
  client?: Client | null;
  fitnessProfile?: FitnessProfile | null;
  reportPeriod: {
    startDate: string; // ISO 8601
    endDate: string; // ISO 8601
  };
  weeklyCheckIns: WeeklyCheckInStats[];
  nutrition: {
    dailySummaries: NutritionSummary[];
    averageDailyCalories: number;
    totalDays: number;
    daysMetGoal: number; // Days where calories were within ±10% of goal
  };
  hydration: {
    dailySummaries: HydrationSummary[];
    averageDailyWater: number;
    totalDays: number;
    daysMetGoal: number; // Days where water goal was met
  };
  goals: GoalSummary;
  profileHistory: ProfileChangeSummary[];
  recommendations: RecommendationSummary;
  generatedAt: Date;
}
