/*
 * SPDX-FileCopyrightText: syuilo and other misskey contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import type { InstancesRepository } from '@/models/_.js';
import { UtilityService } from '@/core/UtilityService.js';
import { DI } from '@/di-symbols.js';
import { FederatedInstanceService } from '@/core/FederatedInstanceService.js';
import { ModerationLogService } from '@/core/ModerationLogService.js';

export const meta = {
	tags: ['admin'],

	requireCredential: true,
	requireModerator: true,
	kind: 'write:admin:federation',
} as const;

export const paramDef = {
	type: 'object',
	properties: {
		host: { type: 'string' },
		isSuspended: { type: 'boolean' },
		isNSFW: { type: 'boolean' },
	},
	required: ['host'],
} as const;

@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
	constructor(
		@Inject(DI.instancesRepository)
		private instancesRepository: InstancesRepository,

		private utilityService: UtilityService,
		private federatedInstanceService: FederatedInstanceService,
		private moderationLogService: ModerationLogService,
	) {
		super(meta, paramDef, async (ps, me) => {
			const instance = await this.instancesRepository.findOneBy({ host: this.utilityService.toPuny(ps.host) });

			if (instance == null) {
				throw new Error('instance not found');
			}

			if (ps.isSuspended != null) {
				await this.federatedInstanceService.update(instance.id, {
					isSuspended: ps.isSuspended,
				});

				if (instance.isSuspended !== ps.isSuspended) {
					if (ps.isSuspended) {
						this.moderationLogService.log(me, 'suspendRemoteInstance', {
							id: instance.id,
							host: instance.host,
						});
					} else {
						this.moderationLogService.log(me, 'unsuspendRemoteInstance', {
							id: instance.id,
							host: instance.host,
						});
					}
				}
			}

			if (ps.isNSFW != null) {
				await this.federatedInstanceService.update(instance.id, {
					isNSFW: ps.isNSFW,
				});
			}
		});
	}
}
