@extends('layouts.app')
@section('title', 'System Reset')

@section('content')
<x-ui.page-toolbar
    title="System Reset"
    description="Return the application to a clean factory state while preserving your administrator access and a recovery backup."
    :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Administration'],['label'=>'System Reset']]"
/>

@php
    $backupReady = ($summary['backups']['directory_writable'] ?? false) && ($summary['backups']['archive_supported'] ?? false);
@endphp

<div class="mx-auto max-w-5xl space-y-5">
    <div class="mf-alert mf-alert-danger">
        <div class="font-semibold">Factory reset permanently removes application data</div>
        <div class="mt-1 text-sm">
            Customers, loans, payments, branches, accounting records, staff accounts, settings, tasks, reports, notifications and uploaded application files will be removed. This action cannot be undone from the application interface.
        </div>
    </div>

    <div class="grid gap-3 sm:grid-cols-2 lg:grid-cols-5">
        <div class="mf-card mf-card-body">
            <div class="text-xs text-muted-foreground">Customers</div>
            <div class="mt-1 text-xl font-semibold">{{ number_format($summary['customers']) }}</div>
        </div>
        <div class="mf-card mf-card-body">
            <div class="text-xs text-muted-foreground">Loans</div>
            <div class="mt-1 text-xl font-semibold">{{ number_format($summary['loans']) }}</div>
        </div>
        <div class="mf-card mf-card-body">
            <div class="text-xs text-muted-foreground">Branches</div>
            <div class="mt-1 text-xl font-semibold">{{ number_format($summary['branches']) }}</div>
        </div>
        <div class="mf-card mf-card-body">
            <div class="text-xs text-muted-foreground">Staff accounts</div>
            <div class="mt-1 text-xl font-semibold">{{ number_format($summary['staff']) }}</div>
        </div>
        <div class="mf-card mf-card-body">
            <div class="text-xs text-muted-foreground">Audit records</div>
            <div class="mt-1 text-xl font-semibold">{{ number_format($summary['audit_logs']) }}</div>
        </div>
    </div>

    <section class="mf-card">
        <div class="mf-card-body border-b border-border">
            <h2 class="text-sm font-semibold">What the reset preserves</h2>
            <p class="mt-1 text-xs text-muted-foreground">Only the minimum recovery and access data is kept outside the reset.</p>
        </div>
        <div class="mf-card-body grid gap-3 md:grid-cols-2">
            <div class="rounded-md border border-border p-3">
                <div class="text-sm font-medium">Your administrator account</div>
                <div class="mt-1 text-xs text-muted-foreground">Your current login identity and password are recreated as the sole super-administrator so you can continue using the system.</div>
            </div>
            <div class="rounded-md border border-border p-3">
                <div class="text-sm font-medium">Recovery backups</div>
                <div class="mt-1 text-xs text-muted-foreground">A new full backup is created before the reset starts. Existing protected backups are also preserved.</div>
            </div>
            <div class="rounded-md border border-border p-3">
                <div class="text-sm font-medium">Laravel logs</div>
                <div class="mt-1 text-xs text-muted-foreground">Server application logs remain available in Laravel Logs for troubleshooting and accountability.</div>
            </div>
            <div class="rounded-md border border-border p-3">
                <div class="text-sm font-medium">Application source code</div>
                <div class="mt-1 text-xs text-muted-foreground">The deployed Laravel application and GitHub-managed source code are not modified by a factory reset.</div>
            </div>
        </div>
    </section>

    @if(!$backupReady)
        <div class="mf-alert mf-alert-danger">
            <div class="font-semibold">Factory reset is currently blocked</div>
            <div class="mt-1 text-sm">
                @if(!($summary['backups']['directory_writable'] ?? false))
                    The protected backup directory is not writable.
                @elseif(!($summary['backups']['archive_supported'] ?? false))
                    Full backups require the PHP Phar and zlib extensions.
                @endif
                A factory reset cannot start until a full recovery backup can be created safely.
            </div>
        </div>
    @endif

    <form method="POST" action="{{ route('admin.system-reset.reset') }}" class="mf-form" id="factory-reset-form">
        @csrf

        <x-ui.form-section
            title="Factory reset confirmation"
            description="All three confirmations are required. The system creates the full backup before entering maintenance mode or deleting any data."
        >
            <div class="space-y-4">
                <div>
                    <label class="mf-field-label" for="reset-current-password">Current password</label>
                    <input
                        id="reset-current-password"
                        type="password"
                        class="mf-input"
                        name="current_password"
                        autocomplete="current-password"
                        required
                    >
                    @error('current_password')<p class="mf-field-help text-destructive">{{ $message }}</p>@enderror
                </div>

                <div>
                    <label class="mf-field-label" for="reset-confirmation-phrase">Type {{ $confirmationPhrase }}</label>
                    <input
                        id="reset-confirmation-phrase"
                        class="mf-input"
                        name="confirmation_phrase"
                        value="{{ old('confirmation_phrase') }}"
                        autocomplete="off"
                        spellcheck="false"
                        required
                    >
                    <p class="mf-field-help">The phrase must match exactly.</p>
                    @error('confirmation_phrase')<p class="mf-field-help text-destructive">{{ $message }}</p>@enderror
                </div>

                <label class="mf-option w-full items-start">
                    <input type="checkbox" name="understand" value="1" required>
                    <span>
                        <span class="block font-medium">I understand this permanently removes the current system data</span>
                        <span class="block text-xs text-muted-foreground">I have reviewed the warning above and understand that recovery requires using the preserved backup.</span>
                    </span>
                </label>
                @error('understand')<p class="mf-field-help text-destructive">{{ $message }}</p>@enderror
            </div>

            <x-ui.form-actions>
                <a class="mf-btn" href="{{ route('admin.system-backups.index') }}">Review backups</a>
                <button
                    id="factory-reset-button"
                    class="mf-btn mf-btn-primary"
                    type="submit"
                    @disabled(!$backupReady)
                >Reset everything</button>
            </x-ui.form-actions>
        </x-ui.form-section>
    </form>
</div>

<script>
(() => {
    const form = document.getElementById('factory-reset-form');
    const button = document.getElementById('factory-reset-button');
    const phrase = document.getElementById('reset-confirmation-phrase');
    if (!form || !button || !phrase) return;

    form.addEventListener('submit', (event) => {
        if (phrase.value !== @json($confirmationPhrase)) {
            event.preventDefault();
            phrase.focus();
            return;
        }

        if (!confirm('Final confirmation: reset the entire system now? A full backup will be created first.')) {
            event.preventDefault();
            return;
        }

        button.disabled = true;
        button.textContent = 'Resetting…';
    });
})();
</script>
@endsection
