import { Router } from 'express';

import promiseProps from '../../utils/promise-props.js';

import slugify from '../../utils/slugify.js';

import { getIdsBySlug } from '../cache.js';
import { HttpError } from '../errors.js';
import { parseActorIdentifier } from '../query.js';
import {
	createSceneRevision,
	fetchSceneRevisions,
	fetchScenes,
	fetchScenesById,
	reviewSceneRevision,
} from '../scenes.js';
import { fetchStashByUsernameAndSlug } from '../stashes.js';

export async function curateScenesQuery(query) {
	const splitYears = query.years?.split(',') || [];
	const splitTags = query.tags?.split(',') || [];
	const splitActors = query.actors?.split(',') || [];
	const splitEntities = query.e?.split(',') || [];
	const mainEntity = splitEntities.find((entity) => entity.charAt(0) !== '!');

	const {
		tagIds,
		notTagIds,
		entityId,
		notEntityIds,
	} = await promiseProps({
		tagIds: getIdsBySlug([query.tagSlug, ...splitTags.filter((tag) => tag.charAt(0) !== '!')], 'tags'),
		notTagIds: getIdsBySlug([...(query.tagFilter || []), ...(splitTags.filter((tag) => tag.charAt(0) === '!').map((tag) => tag.slice(1)) || [])].map((tag) => slugify(tag)), 'tags'),
		entityId: mainEntity ? getIdsBySlug([mainEntity], 'entities').then(([id]) => id) : query.entityId,
		notEntityIds: getIdsBySlug(splitEntities.filter((entity) => entity.charAt(0) === '!').map((entity) => entity.slice(1)), 'entities'),
	});

	return {
		scope: query.scope || 'latest',
		query: query.q,
		years: splitYears.map((year) => Number(year)).filter(Boolean) || [],
		actorIds: [query.actorId, ...splitActors.filter((actor) => actor.charAt(0) !== '!').map((identifier) => parseActorIdentifier(identifier)?.id)].filter(Boolean),
		notActorIds: splitActors.filter((actor) => actor.charAt(0) === '!').map((identifier) => parseActorIdentifier(identifier.slice(1))?.id).filter(Boolean),
		tagIds,
		notTagIds: notTagIds.filter((tagId) => !tagIds.includes(tagId)), // included tags get priority over excluded tags
		onlyActorTags: !!query.at,
		entityId,
		notEntityIds,
		movieId: Number(query.movieId) || null,
		serieId: Number(query.serieId) || null,
		stashId: Number(query.stashId) || null,
		isShowcased: typeof query.isShowcased === 'boolean' ? query.isShowcased : null,
	};
}

async function fetchScenesApi(req, res) {
	const {
		scenes,
		aggYears,
		aggActors,
		aggTags,
		aggActorTags,
		aggChannels,
		limit,
		total,
	} = await fetchScenes(await curateScenesQuery({
		...req.query,
		tagFilter: req.tagFilter,
	}), {
		page: Number(req.query.page) || 1,
		limit: Number(req.query.limit) || 30,
	}, req.user, {
		restriction: req.geo.restriction,
	});

	res.send({
		scenes,
		aggYears,
		aggActors,
		aggTags,
		aggActorTags,
		aggChannels,
		limit,
		total,
	});
}

export const scenesSchema = `
	extend type Query {
		scenes(
			query: String
			scope: String
			entities: [String!]
			actorIds: [String!]
			tags: [String!]
			movieId: Int
			stash: String
			limit: Int! = 30
			page: Int! = 1
		): ReleasesResult

		scene(
			id: Int!
		): Release

		scenesById(
			ids: [Int!]!
		): [Release]
	}

	type ReleasesAggregate {
		actors: [Actor!]
	}

	type ReleasesResult {
		nodes: [Release!]!
		total: Int
		aggregates: ReleasesAggregate
	}

	type Release {
		id: Int!
		title: String
		effectiveDate: Date
		date: Date
		duration: Int
		description: String
		createdAt: Date
		shootId: String
		channel: Entity
		network: Entity
		actors: [Actor!]!
		tags: [Tag!]!
		poster: Media
		trailer: Media
		photos: [Media!]!
		covers: [Media!]!
		movies: [Release!]!
		stashes: [Stash!]
		isStashed(stash: String!): Boolean
	}

	type Tag {
		id: Int!
		name: String
		slug: String
		priority: Int
	}

	type Media {
		id: String!
		path: String
		thumbnail: String
		lazy: String
		mime: String
		hash: String
		isS3: Boolean
		width: Int
		height: Int
		size: Int
		createdAt: Int
	}
`;

function getScope(query) {
	if (query.scope) {
		return query.scope;
	}

	if (query.query) {
		return 'results';
	}

	if (query.stash) {
		return 'stashed';
	}

	return 'latest';
}

function attachResolvers(scene) {
	return {
		...scene,
		isStashed(args) {
			if (!scene.stashes) {
				return null;
			}

			return scene.stashes.some((stash) => stash.slug === args.stash) || false;
		},
	};
}

export async function fetchScenesGraphql(query, req) {
	const mainEntity = query.entities?.find((entity) => entity.charAt(0) !== '!');

	const stash = query.stash && req.user
		? await fetchStashByUsernameAndSlug(req.user.id, query.stash, req.user)
		: null;

	if (query.stash && !stash) {
		throw new HttpError(`Could not find stash '${query.stash}'`, 404);
	}

	const {
		tagIds,
		notTagIds,
		entityId,
		notEntityIds,
	} = await promiseProps({
		tagIds: getIdsBySlug(query.tags?.filter((tag) => tag.charAt(0) !== '!'), 'tags'),
		notTagIds: getIdsBySlug(query.tags?.filter((tag) => tag.charAt(0) === '!').map((tag) => tag.slice(1)).map((tag) => slugify(tag)), 'tags'),
		entityId: getIdsBySlug([mainEntity], 'entities').then(([id]) => id),
		notEntityIds: getIdsBySlug(query.entities?.filter((entity) => entity.charAt(0) === '!').map((entity) => entity.slice(1)), 'entities'),
	});

	const {
		scenes,
		total,
		/*
		aggActors,
		aggTags,
		aggChannels,
		*/
	} = await fetchScenes({
		query: query.query, // query query query query
		tagIds,
		notTagIds,
		entityId,
		notEntityIds,
		actorIds: query.actorIds?.filter((actorId) => actorId.charAt(0) !== '!').map((actorId) => Number(actorId)),
		notActorIds: query.actorIds?.filter((actorId) => actorId.charAt(0) === '!').map((actorId) => Number(actorId.slice(1))),
		movieId: query.movieId,
		stashId: stash?.id,
		scope: getScope(query),
		isShowcased: null,
	}, {
		page: query.page || 1,
		limit: query.limit || 30,
		aggregate: false,
	}, req.user);

	return {
		nodes: scenes.map((scene) => attachResolvers(scene)),
		total,
		/* restrict until deemed essential for 3rd party apps
		aggregates: {
			actors: aggActors,
			tags: aggTags,
			channels: aggChannels,
		},
		*/
	};
}

async function fetchSceneApi(req, res) {
	const [scene] = await fetchScenesById([Number(req.params.sceneId)], { reqUser: req.user }, { restriction: req.geo.restriction });

	if (!scene) {
		throw new HttpError(`No scene with ID ${req.params.sceneId} found`, 404);
	}

	res.send(scene);
}

export async function fetchScenesByIdGraphql(query, req) {
	const scenes = await fetchScenesById([].concat(query.id, query.ids).filter(Boolean), {
		reqUser: req.user,
		includePartOf: true,
		restriction: req.geo.restriction,
	});

	if (query.ids) {
		return scenes.map((scene) => attachResolvers(scene));
	}

	return attachResolvers(scenes[0]);
}

async function fetchSceneRevisionsApi(req, res) {
	const revisions = await fetchSceneRevisions(Number(req.params.revisionId) || null, req.query, req.user);

	res.send(revisions);
}

async function createSceneRevisionApi(req, res) {
	await createSceneRevision(Number(req.body.sceneId), req.body, req.user);

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

async function reviewSceneRevisionApi(req, res) {
	await reviewSceneRevision(Number(req.params.revisionId), req.body.isApproved, req.body, req.user);

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

export const scenesRouter = Router();

scenesRouter.get('/api/scenes', fetchScenesApi);
scenesRouter.get('/api/scenes/:sceneId', fetchSceneApi);

scenesRouter.get('/api/revisions/scenes', fetchSceneRevisionsApi);
scenesRouter.get('/api/revisions/scenes/:revisionId', fetchSceneRevisionsApi);
scenesRouter.post('/api/revisions/scenes', createSceneRevisionApi);
scenesRouter.post('/api/revisions/scenes/:revisionId/reviews', reviewSceneRevisionApi);
