/*
 * Copyright (C) 2026 Fluxer Contributors
 *
 * This file is part of Fluxer.
 *
 * Fluxer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Fluxer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
 */

import * as ModalActionCreators from '@app/actions/ModalActionCreators';
import * as UserActionCreators from '@app/actions/UserActionCreators';
import {Form} from '@app/components/form/Form';
import {FormErrorText} from '@app/components/form/FormErrorText';
import styles from '@app/components/modals/AccountDeleteModal.module.css';
import * as Modal from '@app/components/modals/Modal';
import {Button} from '@app/components/uikit/button/Button';
import {useFormSubmit} from '@app/hooks/useFormSubmit';
import * as RouterUtils from '@app/utils/RouterUtils';
import {Trans, useLingui} from '@lingui/react/macro';
import {observer} from 'mobx-react-lite';
import {useForm} from 'react-hook-form';

interface FormInputs {
	form: string;
}

export const AccountDeleteModal = observer(() => {
	const {t} = useLingui();
	const form = useForm<FormInputs>();

	const onSubmit = async () => {
		await UserActionCreators.deleteAccount();
		ModalActionCreators.pop();
		RouterUtils.transitionTo('/login');
	};

	const {handleSubmit, isSubmitting} = useFormSubmit({
		form,
		onSubmit,
		defaultErrorField: 'form',
	});

	return (
		<Modal.Root size="small" centered>
			<Form form={form} onSubmit={handleSubmit} aria-label={t`Delete account form`}>
				<Modal.Header title={t`Delete Account`} />
				<Modal.Content contentClassName={styles.content}>
					<div className={styles.infoSection}>
						<FormErrorText message={form.formState.errors.form?.message} />
						<p>
							<Trans>
								Are you sure you want to delete your account? This action will schedule your account for permanent
								deletion.
							</Trans>
						</p>
						<div className={styles.infoBox}>
							<p className={styles.infoBoxTitle}>
								<Trans>Important information:</Trans>
							</p>
							<ul className={styles.infoList}>
								<li>
									<Trans>You can cancel the deletion process within 14 days</Trans>
								</li>
								<li>
									<Trans>After 14 days, your account will be permanently deleted</Trans>
								</li>
								<li>
									<Trans>Once deletion is processed, you cannot recover access to your account</Trans>
								</li>
								<li>
									<Trans>You will not be able to delete your sent messages after your account is deleted</Trans>
								</li>
							</ul>
						</div>
						<p className={styles.disclaimer}>
							<Trans>
								If you want to export your data or delete your messages first, please visit the Privacy Dashboard
								section in User Settings before proceeding.
							</Trans>
						</p>
					</div>
				</Modal.Content>
				<Modal.Footer>
					<Button onClick={ModalActionCreators.pop} variant="secondary">
						<Trans>Cancel</Trans>
					</Button>
					<Button type="submit" submitting={isSubmitting} variant="danger-primary">
						<Trans>Delete Account</Trans>
					</Button>
				</Modal.Footer>
			</Form>
		</Modal.Root>
	);
});
