"use strict";

const Promise = require("bluebird");
const fs = require("fs").promises;
const path = require("path");
const dateFns = require("date-fns");
const slug = require("slug");

const compareStrings = require("../compare-strings");

let suffixRegex = /^(.+)\.js$/;
let filenameRegex = /^(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})_(.+)$/;
let templateFolder = path.resolve(__dirname, "../templates");

module.exports = function ({ configurationPath, configuration }) {
	let schemaUpdateFolder = path.resolve(path.dirname(configurationPath), configuration.schema.fs.root);

	return {
		create: function ({ description }) {
			return Promise.try(() => {
				let timestamp = dateFns.format(new Date(), "yyyy-MM-dd_hh-mm-ss");
				let destinationFilename = `${timestamp}_${slug(description)}.js`;

				let sourceTemplate = path.join(templateFolder, "schema-update.js");
				let destinationPath = path.join(schemaUpdateFolder, destinationFilename);

				return Promise.try(() => {
					return fs.copyFile(sourceTemplate, destinationPath);
				}).then(() => {
					return {
						statusMessage: `New schema update created at ${destinationPath}`
					};
				});
			});
		},
		getAll: function () {
			return Promise.try(() => {
				return fs.readdir(schemaUpdateFolder);
			}).then((schemaFiles) => {
				let seenDates = new Set();
	
				return schemaFiles
					.map((filename) => {
						let match = suffixRegex.exec(filename);
		
						if (match != null) {
							let basename = match[1];
							let parsed = filenameRegex.exec(basename);
		
							if (parsed != null) {
								let [ _, date, description ] = parsed;
								
								if (!seenDates.has(date)) {
									seenDates.add(date);
		
									return {
										filename: filename,
										timestamp: date,
										description: description
									};
								} else {
									// FIXME: Link to docs explaining this
									throw new Error(`Encountered timestamp prefix twice: ${date} -- this is not allowed, change one of the timestamps to indicate the desired order.`);
								}
							} else {
								// FIXME: Link to docs explaining this
								throw new Error(`Filename does not match the expected format: ${filename}`);
							}
						} else {
							throw new Error(`The schema folder must only contain .js files; encountered ${filename}`);
						}
					})
					.sort((a, b) => {
						return compareStrings(a.timestamp, b.timestamp);
					})
					.map((item) => {
						return {
							timestamp: item.timestamp, // NOTE: This is a unique sortable ID, that is used to identify the schema update in the internal schema state
							description: item.description,
							operations: require(path.join(schemaUpdateFolder, item.filename))
						};
					});
			});
			// FIXME: Error case handling
		}
	};
};
