import { HeroBattleConfig } from "@/types"; import { Schema, Document, models, model, Model } from "mongoose"; // Define an interface for our document to ensure type safety. // This matches the BattleSetup type from types/index.ts export interface IBattleSetup extends HeroBattleConfig, Document { createdAt: Date; } // REFACTOR: Instead of a single JSON string, we define the schema's structure. // This leverages Mongoose's validation, prevents JSON parsing errors, // and makes the data more robust and queryable. const BattleSetupSchema = new Schema({ type: { type: String, required: true, enum: ["HERO_BATTLE", "CUSTOM_BATTLE"], }, // We use Schema.Types.Mixed for the config object because its structure // depends on the `type` field. Mongoose will store the object directly. config: { type: Schema.Types.Mixed, required: true, }, createdAt: { type: Date, default: Date.now, }, }); // Use a type-safe model definition. const BattleSetupModel: Model = models.BattleSetup || model("BattleSetup", BattleSetupSchema); export default BattleSetupModel;