import { v4 } from "uuid";
import { IChapter } from "@models/stories/chapter";
import { IBand } from "@models/band";

export interface FormChapter {
	id?: number;
	chapterTitle: string;
	index: number;
	summary: string;
	notes: string;
	genre: string[];
	bands: number[];
	characters: string[];
	relationships: string[][];
	nsfw: boolean;
	loggedInOnly: boolean;
	hidden: boolean;
	content: string;
	file?: string;
	uuidKey: string;
}

export interface FormStory {
	title: string;
	coAuthor: number | null;
	chapters: FormChapter[];
	ficmas?: number | null;
	challenge?: number | null;
	completed: boolean;
}

export const defaultChapter: FormChapter = {
	chapterTitle: "",
	index: 1,
	summary: "",
	notes: "",
	genre: [],
	bands: [],
	characters: [],
	relationships: [],
	nsfw: false,
	loggedInOnly: true,
	hidden: false,
	content: "",
	uuidKey: v4(),
};

export const defaultStory: FormStory = {
	title: "",
	coAuthor: null,
	chapters: [],
	ficmas: null,
	challenge: null,
	completed: false,
};

export function toFormChapter(chap: IChapter & { text: string }): FormChapter {
	return {
		chapterTitle: chap.title,
		index: 1,
		summary: chap.summary,
		notes: chap.notes,
		genre: chap.genre,
		bands: (chap.bands as IBand[]).map((a) => a._id),
		characters: chap.characters,
		relationships: chap.relationships,
		nsfw: chap.nsfw,
		loggedInOnly: chap.loggedInOnly,
		hidden: chap.hidden,
		content: chap.text,
		uuidKey: v4(),
	};
}
