import mongoose from 'mongoose';

//
// ✅ PURE MONGOOSE SCHEMAS FOR TESTING
//

export const SalonSchema = new mongoose.Schema(
  {
    name: { type: String, required: true },
    timezone: { type: String, required: true },
    address: {
      city: String,
      country: String,
    },
    working_hours: mongoose.Schema.Types.Mixed,
    holidays: Array,
  },
  { timestamps: true }
);

export const ServiceSchema = new mongoose.Schema(
  {
    name: { type: String, required: true },
    category: { type: String, required: true },
    salonId: { type: mongoose.Schema.Types.ObjectId, ref: 'Salon', required: true },
    price: { type: Number, required: true },
    duration: { type: Number, required: true },
    active: { type: Boolean, default: true },
  },
  { timestamps: true }
);

export const StylistSchema = new mongoose.Schema(
  {
    name: { type: String, required: true },
    salonId: { type: mongoose.Schema.Types.ObjectId, ref: 'Salon', required: true },
    services: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Service' }],
    active: { type: Boolean, default: true },
  },
  { timestamps: true }
);

export const ClientSchema = new mongoose.Schema(
  {
    // Basic identity
    name: { type: String, required: true },
    firstName: String,
    lastName: String,
    dateOfBirth: Date,
    gender: String,
    profilePhoto: String,

    // Contact info
    email: String,
    phone: String,
    alternatePhone: String,
    address: { type: Object }, // { street, city, state, zip, country }
    timezone: String,

    // Salon reference
    salonId: { type: mongoose.Schema.Types.ObjectId, ref: 'Salon' },

    // Client status
    status: {
      type: String,
      enum: ['new', 'active', 'inactive', 'VIP'],
      default: 'new',
    },

    // Booking & loyalty
    totalVisits: { type: Number, default: 0 },
    lastVisit: Date,
    favoriteServices: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Service' }],
    preferredStylist: { type: mongoose.Schema.Types.ObjectId, ref: 'Stylist' },
    loyaltyPoints: { type: Number, default: 0 },

    // Offers & marketing
    appliedOffers: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Offer' }],
    couponHistory: [String],
    newsletterSubscribed: { type: Boolean, default: true },
    smsSubscribed: { type: Boolean, default: true },

    // System & analytics
    tags: [String],
    notes: String,
  },
  { timestamps: true }
);

export const OfferSchema = new mongoose.Schema(
  {
    salonId: { type: mongoose.Schema.Types.ObjectId, ref: 'Salon', required: true, index: true },

    title: { type: String, required: true },
    description: String,

    type: {
      type: String,
      enum: ['nth_visit', 'birthday', 'coupon', 'salon_day', 'spend_threshold', 'gift'],
      required: true,
    },

    discountType: {
      type: String,
      enum: ['percentage', 'fixed', 'gift'],
    },

    discountValue: Number, // % or ₹ depending on discountType
    giftItem: String,

    triggerValue: { type: Object }, 
    // examples:
    // - nth_visit => 5
    // - birthday => 7 (days window)
    // - salon_day => '2025-03-08'
    // - spend_threshold => 1500

    minSpend: Number,
    couponCode: { type: String, uppercase: true, sparse: true },

    isActive: { type: Boolean, default: true },
    autoApply: { type: Boolean, default: false },
    maxUses: Number,
    usedCount: { type: Number, default: 0 },
  },
  { collection: 'offers', timestamps: true }
);

export const WorkingHourSchema = new mongoose.Schema({
  stylistId: { type: mongoose.Schema.Types.ObjectId, ref: 'Stylist' },
  salonId: { type: mongoose.Schema.Types.ObjectId, ref: 'Salon' },
  dayOfWeek: Number, // 1=Monday
  startTime: String,
  endTime: String,
  breaks: [{ start: String, end: String }],
});

export const TimeOffSchema = new mongoose.Schema({
  stylistId: { type: mongoose.Schema.Types.ObjectId, ref: 'Stylist' },
  salonId: { type: mongoose.Schema.Types.ObjectId, ref: 'Salon' },
  date: String, // "YYYY-MM-DD"
  type: String,
  reason: String,
});

export const AppointmentSchema = new mongoose.Schema(
  {
    salonId: { type: mongoose.Schema.Types.ObjectId, ref: 'Salon' },
    stylistId: { type: mongoose.Schema.Types.ObjectId, ref: 'Stylist' },
    serviceId: { type: mongoose.Schema.Types.ObjectId, ref: 'Service' },
    clientId: { type: mongoose.Schema.Types.ObjectId, ref: 'Client' },
    start: Date,
    end: Date,
    status: { type: String, default: 'booked' },
  },
  { timestamps: true }
);


