import { IStory, Story } from "../models/stories";
import { MongoClient } from "mongodb";
import mongoose from "mongoose";
import { uri, olduri } from "../lib/dbconfig";

import { Band } from "../models/band";
import { User } from "../models/user";

const cli = new MongoClient(olduri);

async function meep() {
	try {
		await cli.connect()
		await mongoose.connect(uri).then(() => console.log("connect'd"));

		const db = cli.db("rockfic_old")
		const col = db.collection("users");
		const cursor = col.find({});
		for await (const s of cursor) {
			const nu = new User({
				_id: s._id,
				username: s.username,
				email: s.email,
				auth: {
					emailVerified: s.emailIsVerified,
					activationKey: s.activationkey,
					passwordResetToken: s.passwordresettoken,
				},
				password: s.password,
				ts: {
					created: s.createdAt,
					updated: s.updatedAt
				},
				ipLog: [{
					lastAccess: s.lastvisit,
					ip: s.ip_address
				}],
				lastLogin: s.lastvisit,
				lastvisit: s.lastvisit,
				profile: {
					avatar: s.profile_metadata.avatar,
					isAdmin: s.isAdmin,
					nightMode: s.profile_metadata.isNightMode,
					bio: s.profile_metadata.bio,
					location: s.profile_metadata.location,
					occupation: s.profile_metadata.occupation,
					website: s.profile_metadata.website,
					blog: s.profile_metadata.blog,
					views: s.profile_metadata.pageviews,
					lastWhere: s.profile_metadata.isWhere,
					hidden: s.profile_metadata.isStatusHidden
				},
				biffno: {
					years: s.biffnoYears,
					wins: s.biffnoWins
				},
				favs: {
					authors: s.favs.favauthors,
					stories: (await db.collection("stories").find({_id: {$in: s.favs.favstories}}).toArray()).map(a => a.id)
				},
				subscriptions: {
					authors: s.subscriptions.author_subscriptions,
					band_subscriptions: (await Band.find({name: {$in: s.subscriptions.band_subscriptions}})).map(a => a._id),
					stories: (await db.collection("stories").find({_id: {$in: s.subscriptions.story_subscriptions}}).toArray()).map(a => a.id)
				},
				hiddenBands: (await Band.find({name: {$in: s.hidden_bands}})).map(a => a._id),
				hiddenAuthors: s.hidden_authors,
				blocked: s.blocklist,
				sessionId: s.sessionId,
				banned: s.banned,
				quickMenuConfig: []
			})
			await nu.save()
		}
	} finally {
		return 0
	}
}

// meep().then(() => process.exit())

export default meep
