import mongoose, {
	Schema,
	PopulatedDoc,
	Document,
	Model,
	model,
} from "mongoose";

import { IBand } from "@models/band";
import { IUser } from "@models/user";
import SequenceFactory from "mongoose-sequence";
import { hasMigrated } from "@dbconfig";

const AutoIncrement = SequenceFactory(mongoose);

export interface IFicmas {
	_id: number;
	kink: string;
	year: number;
	bands: PopulatedDoc<IBand>[];
	relationship: string;
	wisher: PopulatedDoc<IUser>;
	anniversary: boolean;
}

export const FicmasSchema = new mongoose.Schema<IFicmas>({
	_id: {
		type: Number,
	},
	kink: {
		type: String,
	},
	year: { type: Number },
	bands: [
		{
			type: Number,
			ref: "Band",
		},
	],
	relationship: { type: String },
	wisher: {
		type: Number,
		ref: "User",
	},
	anniversary: {
		type: Boolean,
		default: false,
	},
});

hasMigrated &&
	!mongoose.models.Ficmas &&
	FicmasSchema.plugin(AutoIncrement, { id: "ficmas_wishes", inc_field: "_id" });

export const Ficmas: Model<IFicmas> =
	mongoose.models.Ficmas || model("Ficmas", FicmasSchema, "ficmas_wishes");