import { Router } from 'express';

import verifyAbility from '../../utils/verify-ability.js';

import {
	syncActors,
	syncMovies,
	syncQueue,
	syncScenes,
	syncStashes,
} from '../sync.js';

export const syncRouter = Router();

async function syncScenesApi(req, res) {
	verifyAbility(req.user, 'sync', null, { throwError: true });

	await syncScenes(req.body.sceneIds);

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

async function syncMoviesApi(req, res) {
	verifyAbility(req.user, 'sync', null, { throwError: true });

	await syncMovies(req.body.movieIds);

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

async function syncActorsApi(req, res) {
	verifyAbility(req.user, 'sync', null, { throwError: true });

	await syncActors(req.body.actorIds);

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

async function syncStashesApi(req, res) {
	verifyAbility(req.user, 'sync', null, { throwError: true });

	await syncStashes(req.body.stashIds);

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

async function syncQueueApi(req, res) {
	verifyAbility(req.user, 'sync', null, { throwError: true });

	await syncQueue();

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

syncRouter.post('/api/sync/scenes', syncScenesApi);
syncRouter.post('/api/sync/movies', syncMoviesApi);
syncRouter.post('/api/sync/actors', syncActorsApi);
syncRouter.post('/api/sync/stashes', syncStashesApi);
syncRouter.post('/api/sync', syncQueueApi);
