import { Document } from "mongoose";
import type {
  SubscriptionEntitlement,
  SubscriptionStatus,
} from "../../constants/subscription";

export const locationSchema = {
  type: {
    type: String,
    enum: ["Point"],
  },
  coordinates: {
    type: [Number],
    validate: {
      validator(coords: number[] | null | undefined) {
        if (coords == null || coords.length === 0) return true;
        return (
          coords.length === 2 &&
          typeof coords[0] === "number" &&
          typeof coords[1] === "number"
        );
      },
      message: "Coordinates must be an array of two numbers [longitude, latitude]",
    },
  },
  address: {
    type: String,
    trim: true,
    default: "",
  },
  label: {
    type: String,
    trim: true,
    default: "",
  },
};

export interface IUser extends Document {
  email: string;
  password?: string;
  fullName?: string;
  code?: number;
  image?: string;
  cover?: string;
  dob?: string;
  phone?: string;
  address?: string;
  city?: string;
  zip?: string;
  state?: string;
  country?: string;
  gender?: string;
  userType: "admin" | "user" | "guest";
  socialType: "facebook" | "google" | "tank";
  location?: {
    type: "Point";
    coordinates?: [number, number];
    address?: string;
    label?: string;
  };
  deletedEmail?: string | null;
  isVerified: boolean;
  isDeleted: boolean;
  isBanned: boolean;
  isProfileCompleted: boolean;
  isCarsRegistered: boolean;
  isNotificationRecievable: boolean;
  status: boolean;
  /** Smartcar Connect **user id** (UUID from Smartcar), denormalized. OAuth access/refresh tokens stay in `SmartcarAccount`. */
  smartcarToken?: string | null;
  /** Cached wallet balance (USD cents). Ledger is source of truth in WalletTransaction. */
  walletBalanceCents?: number;
  /** Stripe Customer id for saved cards & wallet PaymentIntents (cus_…). */
  stripeCustomerId?: string | null;
  /** Stripe Connect Express account for payouts (acct_…). */
  stripeAccountId?: string | null;
  stripeConnected?: boolean;
  stripeChargesEnabled?: boolean;
  stripeDetailsSubmitted?: boolean;
  /** RevenueCat app_user_id (same as Mongo _id when configured correctly). */
  revenueCatUserId?: string | null;
  subscriptionProvider?: string | null;
  subscriptionStatus?: SubscriptionStatus;
  activeEntitlement?: SubscriptionEntitlement | null;
  /** Raw store product id from RevenueCat (may include Google base-plan suffix). */
  subscriptionProductId?: string | null;
  subscriptionStartedAt?: Date | null;
  subscriptionExpiresAt?: Date | null;
  lastRevenueCatEventAt?: Date | null;
}