<!--
SPDX-FileCopyrightText: syuilo and misskey-project
SPDX-License-Identifier: AGPL-3.0-only
-->

<template>
<div class="_gaps_m">
	<!--
	<MkSwitch v-model="$i.injectFeaturedNote" @update:model-value="onChangeInjectFeaturedNote">
		<template #label>{{ i18n.ts.showFeaturedNotesInTimeline }}</template>
	</MkSwitch>
	-->

	<!--
	<MkSwitch v-model="reportError">{{ i18n.ts.sendErrorReports }}<template #caption>{{ i18n.ts.sendErrorReportsDescription }}</template></MkSwitch>
	-->

	<FormSection first>
		<div class="_gaps_s">
			<MkFolder>
				<template #icon><i class="ti ti-info-circle"></i></template>
				<template #label>{{ i18n.ts.accountInfo }}</template>

				<div class="_gaps_m">
					<MkKeyValue>
						<template #key>ID</template>
						<template #value><span class="_monospace">{{ $i.id }}</span></template>
					</MkKeyValue>

					<MkKeyValue>
						<template #key>{{ i18n.ts.registeredDate }}</template>
						<template #value><MkTime :time="$i.createdAt" mode="detail"/></template>
					</MkKeyValue>

					<FormLink to="/settings/account-stats"><template #icon><i class="ti ti-info-circle"/></template>{{ i18n.ts.statistics }}</FormLink>
				</div>
			</MkFolder>

			<MkFolder>
				<template #icon><i class="ti ti-code"></i></template>
				<template #label>{{ i18n.ts.developer }}</template>

				<div class="_gaps_m">
					<MkSwitch v-model="devMode">
						<template #label>{{ i18n.ts.devMode }}</template>
					</MkSwitch>
				</div>
			</MkFolder>
		</div>
	</FormSection>

	<FormSection>
		<FormLink to="/registry"><template #icon><i class="ti ti-adjustments"></i></template>{{ i18n.ts.registry }}</FormLink>
	</FormSection>

	<FormSection>
		<div class="_gaps_s">
			<MkSwitch v-model="defaultWithReplies">{{ i18n.ts.withRepliesByDefaultForNewlyFollowed }}</MkSwitch>
			<MkButton danger @click="updateRepliesAll(true)"><i class="ti ti-messages"></i> {{ i18n.ts.showRepliesToOthersInTimelineAll }}</MkButton>
			<MkButton danger @click="updateRepliesAll(false)"><i class="ti ti-messages-off"></i> {{ i18n.ts.hideRepliesToOthersInTimelineAll }}</MkButton>
		</div>
	</FormSection>

	<FormSection>
		<template #label><i class="ti ti-alert-circle"></i> {{ i18n.ts.dangerZone }}</template>
		<template #description>{{ i18n.ts.dangerZoneDescription }}</template>

		<div class="_gaps_s">
			<MkFolder>
				<template #icon><i class="ti ti-trash"></i></template>
				<template #label>{{ i18n.ts.closeAccount }}</template>

				<div v-if="$i.policies.canUseAccountRemoval && !$i.isRoot" class="_gaps_m">
					<MkInfo warn>{{ i18n.ts._accountDelete.mayTakeTime }}</MkInfo>
					<MkInfo>{{ i18n.ts._accountDelete.sendEmail }}</MkInfo>
					<MkButton v-if="!$i.isDeleted" danger @click="deleteAccount">{{ i18n.ts._accountDelete.requestAccountDelete }}</MkButton>
					<MkButton v-else disabled>{{ i18n.ts._accountDelete.inProgress }}</MkButton>
				</div>
				<div v-else-if="$i.isRoot === true" class="_gaps_m">
					<MkInfo>{{ i18n.ts._accountDelete.youAreRootAndCantUseThisTime }}</MkInfo>
				</div>
				<div v-else class="_gaps_m">
					<MkInfo>{{ i18n.ts._accountDelete.youCantUseThisTime }}</MkInfo>
				</div>
			</MkFolder>

			<MkFolder>
				<template #icon><i class="ti ti-bomb"></i></template>
				<template #label>{{ i18n.ts.truncateAccount }}</template>

				<div v-if="$i.policies.canUseAccountTruncate" class="_gaps_m">
					<MkInfo warn>{{ i18n.ts._accountTruncate.mayTakeTime }}</MkInfo>
					<MkSwitch v-model="purgeDrive">
						<template #label>{{ i18n.ts._accountTruncate.purgeDriveFiles }}</template>
					</MkSwitch>
					<MkButton v-if="!$i.isDeleted" danger @click="truncateAccount">{{ i18n.ts._accountTruncate.requestAccountTruncate }}</MkButton>
					<MkButton v-else disabled>{{ i18n.ts._accountTruncate.inProgress }}</MkButton>
				</div>
				<div v-else class="_gaps_m">
					<MkInfo>{{ i18n.ts._accountTruncate.youCantUseThisTime }}</MkInfo>
				</div>
			</MkFolder>
		</div>
	</FormSection>
</div>
</template>

<script lang="ts" setup>
import { computed, ref } from 'vue';
import MkSwitch from '@/components/MkSwitch.vue';
import FormLink from '@/components/form/link.vue';
import MkFolder from '@/components/MkFolder.vue';
import MkInfo from '@/components/MkInfo.vue';
import FormSection from '@/components/form/section.vue';
import MkInput from '@/components/MkInput.vue';
import MkKeyValue from '@/components/MkKeyValue.vue';
import MkButton from '@/components/MkButton.vue';
import * as os from '@/os.js';
import { misskeyApi } from '@/scripts/misskey-api.js';
import { defaultStore } from '@/store.js';
import { signout, signinRequired } from '@/account.js';
import { i18n } from '@/i18n.js';
import { definePageMetadata } from '@/scripts/page-metadata.js';

// const reportError = computed(defaultStore.makeGetterSetter('reportError'));
const $i = signinRequired();
const purgeDrive = ref<boolean>(false);
const devMode = computed(defaultStore.makeGetterSetter('devMode'));
const defaultWithReplies = computed(defaultStore.makeGetterSetter('defaultWithReplies'));

async function deleteAccount() {
	{
		const { canceled } = await os.confirm({
			type: 'warning',
			text: i18n.ts.deleteAccountConfirm,
		});
		if (canceled) return;
	}

	const auth = await os.authenticateDialog();
	if (auth.canceled) return;

	await os.apiWithDialog('i/delete-account', {
		password: auth.result.password,
		token: auth.result.token,
	});

	await os.alert({
		title: i18n.ts._accountDelete.started,
	});

	await signout();
}

async function truncateAccount() {
	{
		const { canceled } = await os.confirm({
			type: 'warning',
			text: i18n.ts.truncateAccountConfirm,
		});
		if (canceled) return;
	}

	const auth = await os.authenticateDialog();
	if (auth.canceled) return;

	await os.apiWithDialog('i/truncate-account', {
		password: auth.result.password,
		token: auth.result.token,
		purgeDrive: purgeDrive.value,
	});

	await os.alert({
		title: i18n.ts._accountTruncate.started,
	});
}

async function updateRepliesAll(withReplies: boolean) {
	const { canceled } = await os.confirm({
		type: 'warning',
		text: withReplies ? i18n.ts.confirmShowRepliesAll : i18n.ts.confirmHideRepliesAll,
	});
	if (canceled) return;

	misskeyApi('following/update-all', { withReplies });
}

const headerActions = computed(() => []);

const headerTabs = computed(() => []);

definePageMetadata(() => ({
	title: i18n.ts.other,
	icon: 'ti ti-dots',
}));
</script>
