import mongoose, { Document, Schema } from 'mongoose';

export interface IGeneralSettings extends Document {
  appName: string;
  phoneNumber: string;
  email: string;
  location: string;

  logo?: string;
  footerLogo?: string;
  favicon?: string;

  facebookLink?: string;
  instagramLink?: string;
  youtubeLink?: string;
  printerestLink?: string;
  statementContent?: string;
  whatsappNumber?: string;

  isActive: boolean;

  createdAt: Date;
  updatedAt: Date;
}

const GeneralSettingsSchema: Schema = new Schema(
  {
    appName: {
      type: String,
      required: [true, 'App name is required'],
      trim: true,
      maxlength: [100, 'App name cannot exceed 100 characters']
    },

    phoneNumber: {
      type: String,
      required: [true, 'Phone number is required'],
      trim: true
    },

    email: {
      type: String,
      required: [true, 'Email is required'],
      lowercase: true,
      trim: true,
      match: [/^\S+@\S+\.\S+$/, 'Please use a valid email address']
    },

    location: {
      type: String,
      required: [true, 'Location is required'],
      trim: true,
      maxlength: [200, 'Location cannot exceed 200 characters']
    },

    // ✅ Images / Assets
    logo: {
      type: String,
      trim: true
    },

    footerLogo: {
      type: String,
      trim: true
    },

    favicon: {
      type: String,
      trim: true
    },

    // ✅ Social Links
    facebookLink: {
      type: String,
      trim: true
    },

    instagramLink: {
      type: String,
      trim: true
    },
    youtubeLink: {
      type: String,
      trim: true
    },
    printerestLink: {
      type: String,
      trim: true
    },
    statementContent: {
      type: String,
      trim: true
    },
    whatsappNumber: {
      type: String,
      trim: true
    },

    isActive: {
      type: Boolean,
      default: true
    }
  },
  {
    timestamps: true
  }
);

// ✅ Indexes
GeneralSettingsSchema.index({ isActive: 1 });
GeneralSettingsSchema.index({ email: 1 });

// ✅ Only one active settings (optional but recommended)
GeneralSettingsSchema.index(
  { isActive: 1 },
  { unique: true, partialFilterExpression: { isActive: true } }
);

export default mongoose.models.GeneralSettings ||
  mongoose.model<IGeneralSettings>('GeneralSettings', GeneralSettingsSchema);