import { Router } from 'express';

import {
	createStash,
	fetchStashByUsernameAndSlug,
	fetchUserStashes,
	removeStash,
	stashActor,
	stashMovie,
	stashScene,
	unstashActor,
	unstashMovie,
	unstashScene,
	updateStash,
} from '../stashes.js';

export const stashesSchema = `
	extend type Query {
		stashes(username: String): [Stash]

		stash(
			username: String
			stash: String!
		): Stash
	}

	extend type Mutation {
		createStash(
			name: String!
			isPublic: Boolean
			comment: String
			meta: JSON
		): Stash

		updateStash(
			stash: String!
			name: String
			isPublic: Boolean
			comment: String
			meta: JSON
		): Stash

		removeStash(
			stash: String!
		): Stash

		stashScene(
			sceneId: Int!
			stash: String!
		): Stashed

		unstashScene(
			sceneId: Int!
			stash: String!
		): Stashed

		stashActor(
			actorId: Int!
			stash: String!
		): Stashed

		unstashActor(
			actorId: Int!
			stash: String!
		): Stashed

		stashMovie(
			movieId: Int!
			stash: String!
		): Stashed

		unstashMovie(
			movieId: Int!
			stash: String!
		): Stashed
	}

	type Stash {
		id: Int
		name: String
		slug: String
		isPublic: Boolean
		isPrimary: Boolean
		createdAt: Date
		comment: String
		meta: JSON
	}

	type Stashed {
		id: Int
		stashId: Int
		sceneId: Int
		actorId: Int
		movieId: Int
		serieId: Int
		createdAt: Date
	}
`;

export async function fetchUserStashesApi(req, res) {
	const stashes = await fetchUserStashes(req.user.id, req.user);

	res.send(stashes);
}

export async function fetchUserStashesGraphql(query, req) {
	const stashes = await fetchUserStashes(query.username || req.user.id, req.user);

	return stashes;
}

export async function fetchStashGraphql(query, req) {
	const stashes = await fetchStashByUsernameAndSlug(query.username || req.user.id, query.stash, req.user);

	return stashes;
}

export async function createStashApi(req, res) {
	const stash = await createStash(req.body, req.user);

	res.send(stash);
}

export async function createStashGraphql(query, req) {
	const stash = await createStash(query, req.user);

	return stash;
}

export async function updateStashApi(req, res) {
	const stash = await updateStash(Number(req.params.stashId), req.body, req.user);

	res.send(stash);
}

export async function updateStashGraphql(query, req) {
	const stash = await updateStash(query.stash, query, req.user);

	return stash;
}

export async function removeStashApi(req, res) {
	await removeStash(Number(req.params.stashId) || req.params.stashId, req.user);

	res.status(204).send();
}

export async function removeStashGraphql(query, req) {
	const removedId = await removeStash(query.stash, req.user);

	return removedId;
}

export async function stashActorApi(req, res) {
	const stashed = await stashActor(req.body.actorId, Number(req.params.stashId), req.user);

	res.send(stashed);
}

export async function stashActorGraphql(query, req) {
	const stashed = await stashActor(query.actorId, query.stash, req.user);

	return stashed;
}

export async function unstashActorApi(req, res) {
	const unstashed = await unstashActor(Number(req.params.actorId), Number(req.params.stashId), req.user);

	res.send(unstashed);
}

export async function unstashActorGraphql(query, req) {
	const stashes = await unstashActor(query.actorId, query.stash, req.user);

	return stashes;
}

export async function stashSceneApi(req, res) {
	const stashed = await stashScene(req.body.sceneId, Number(req.params.stashId), req.user);

	res.send(stashed);
}

export async function stashSceneGraphql(query, req) {
	const stashed = await stashScene(query.sceneId, query.stash, req.user);

	return stashed;
}

export async function unstashSceneApi(req, res) {
	const unstashed = await unstashScene(Number(req.params.sceneId), Number(req.params.stashId), req.user);

	res.send(unstashed);
}

export async function unstashSceneGraphql(query, req) {
	const unstashed = await unstashScene(query.sceneId, query.stash, req.user);

	return unstashed;
}

export async function stashMovieApi(req, res) {
	const stashed = await stashMovie(req.body.movieId, Number(req.params.stashId), req.user);

	res.send(stashed);
}

export async function stashMovieGraphql(query, req) {
	const stashed = await stashMovie(query.movieId, query.stash, req.user);

	return stashed;
}

export async function unstashMovieApi(req, res) {
	const unstashed = await unstashMovie(Number(req.params.movieId), Number(req.params.stashId), req.user);

	res.send(unstashed);
}

export async function unstashMovieGraphql(query, req) {
	const unstashed = await unstashMovie(query.movieId, query.stash, req.user);

	return unstashed;
}

export const router = Router();

router.get('/api/users/:userId/stashes', fetchUserStashesApi);
router.post('/api/stashes', createStashApi);
router.patch('/api/stashes/:stashId', updateStashApi);
router.delete('/api/stashes/:stashId', removeStashApi);

router.post('/api/stashes/:stashId/actors', stashActorApi);
router.post('/api/stashes/:stashId/scenes', stashSceneApi);
router.post('/api/stashes/:stashId/movies', stashMovieApi);

router.delete('/api/stashes/:stashId/actors/:actorId', unstashActorApi);
router.delete('/api/stashes/:stashId/scenes/:sceneId', unstashSceneApi);
router.delete('/api/stashes/:stashId/movies/:movieId', unstashMovieApi);
