import { hasMigrated } from "../../lib/dbconfig";
import { IBand } from "../../models/band";
import { IFicmas } from "../../models/challenges/ficmas";
import { IChallenge } from "../../models/challenges/gen";
import { IUser } from "../../models/user";
import mongoose, {Schema, PopulatedDoc, Document, Model} from "mongoose";
import SequenceFactory from "mongoose-sequence";

const AutoIncrement = SequenceFactory(mongoose);

export interface IDraft {
	_id?: number,
	title: string;
	chaptertitle: string;
	updatedAt: Date;
	author: PopulatedDoc<IUser & Document>;
	notes: string;
	genre: string[];
	bands: PopulatedDoc<IBand & Document>[];
	characters: string[];
	relationships: string[];
	summary: string;
	nsfw: boolean;
	hidden: boolean;
	loggedInOnly: boolean;
	oneshot: boolean;
	challenge: PopulatedDoc<IChallenge & Document>;
	ficmas: PopulatedDoc<IFicmas & Document>;
}

const DraftSchema = new Schema<IDraft>({
	title: {
		type: String
	},
	_id: {
		type: Number
	},
	chaptertitle: {
		type: String,
	},
	updatedAt: {
		type: Date,
		default: new Date()
	},
	author: {
		type: Number,
		ref: "User"
	},
	notes: {
		type: String
	},
	genre: [{
		type: String
	}],
	bands: [{
		type: Number,
		ref: "Band"
	}],
	characters: [{
		type: String
	}],
	relationships: [{
		type: String
	}],
	nsfw: {
		type: Boolean
	},
	summary: {
		type: String
	},
	hidden: {
		type: Boolean,
		default: false
	},
	loggedInOnly: {
		type: Boolean
	},
	oneshot: {
		type: Boolean,
		default: false
	},
	challenge: {
		type: Number,
		ref: "Challenge",
		default: null
	},
	ficmas: {
		type: Number,
		ref: "Ficmas",
		default: null
	}
})

hasMigrated && DraftSchema.plugin(AutoIncrement, {id: "drafts"})

export const Draft: Model<IDraft> = /* mongoose.models.Draft || */ mongoose.model("Draft", DraftSchema, "drafts")