import { model, models, Schema, type HydratedDocument, type InferSchemaType, type Model, } from "mongoose"; /** Hane ayarları (Faz 0 kararları: bütçe ayı başlangıcı, taksit ve günlük plan tercihleri). */ const householdSettingsSchema = new Schema( { /** Bütçe ayının başlangıç günü (1 = takvim ayı). */ budgetMonthStartDay: { type: Number, min: 1, max: 28, default: 1 }, /** Taksitlerin bütçe etkisi: her taksit kendi ayına | tamamı harcama ayına. */ installmentBudgetMode: { type: String, enum: ["per_installment", "purchase_month"], default: "per_installment", }, /** Günlük plan: değişken bütçe eşit dağıtılır | önceki ayın örüntüsüne göre. */ dailyPlanMode: { type: String, enum: ["even", "previous_month_pattern"], default: "even" }, /** "Yalnızca ben" hesapları hane toplamlarına dahil edilsin mi? */ includePrivateAccountsInTotals: { type: Boolean, default: true }, /** Akıllı mağaza önerisi: ek mağaza ancak tasarruf bu tutarı aşarsa (kuruş). */ extraStoreSavingThresholdMinor: { type: Number, min: 0, default: 10_000 }, }, { _id: false }, ); const householdSchema = new Schema( { name: { type: String, required: true, trim: true, minlength: 2, maxlength: 80 }, currency: { type: String, enum: ["TRY"], default: "TRY" }, timezone: { type: String, default: "Europe/Istanbul" }, settings: { type: householdSettingsSchema, default: () => ({}) }, archivedAt: { type: Date, default: null }, createdBy: { type: Schema.Types.ObjectId, required: true }, updatedBy: { type: Schema.Types.ObjectId, required: true }, }, { timestamps: true, collection: "households" }, ); householdSchema.index({ name: 1 }); export type Household = InferSchemaType; export type HouseholdDoc = HydratedDocument; /** HMR'de modelin yeniden derlenmesini önler. */ export const HouseholdModel: Model = (models.Household as Model | undefined) ?? model("Household", householdSchema);