@extends('saas-admin.layout')

@section('title', 'Backups')

@section('content')
    <h4 class="mb-3">{{ __('Backups') }}</h4>

    <div class="row g-3">
        <div class="col-md-7">
            <div class="card shadow-sm h-100">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center mb-3">
                        <h6 class="text-uppercase text-muted small m-0">{{ __('Existing backups') }}</h6>
                        <form action="{{ route('saas-admin.backups.store') }}" method="POST">
                            @csrf
                            <button type="submit" class="btn btn-primary btn-sm">{{ __('Create backup now') }}</button>
                        </form>
                    </div>

                    <div class="table-responsive">
                        <table class="table table-sm align-middle m-0">
                            <thead>
                                <tr>
                                    <th>{{ __('File') }}</th>
                                    <th>{{ __('Size') }}</th>
                                    <th>{{ __('Created') }}</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse ($backups as $backup)
                                    <tr>
                                        <td><code>{{ $backup['name'] }}</code></td>
                                        <td>{{ number_format($backup['size'] / 1024, 1) }} KB</td>
                                        <td>{{ $backup['modified']->format('M j, Y H:i') }}</td>
                                        <td class="text-end">
                                            <a href="{{ route('saas-admin.backups.download', $backup['name']) }}" class="btn btn-sm btn-outline-secondary">
                                                {{ __('Download') }}
                                            </a>
                                            <form action="{{ route('saas-admin.backups.destroy', $backup['name']) }}" method="POST" class="d-inline"
                                                onsubmit="return confirm('{{ __('Delete this backup file?') }}')">
                                                @csrf
                                                @method('DELETE')
                                                <button type="submit" class="btn btn-sm btn-outline-danger">{{ __('Delete') }}</button>
                                            </form>
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="4" class="text-center text-muted py-4">{{ __('No backups yet.') }}</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-md-5">
            <div class="card shadow-sm mb-3">
                <div class="card-body">
                    <h6 class="text-uppercase text-muted small mb-3">{{ __('Automated backups') }}</h6>
                    <form action="{{ route('saas-admin.backups.settings') }}" method="POST">
                        @csrf
                        @method('PUT')
                        <div class="form-check form-switch mb-3">
                            <input type="checkbox" name="is_automated_enabled" value="1" class="form-check-input" id="automatedEnabled"
                                {{ $settings->is_automated_enabled ? 'checked' : '' }}>
                            <label class="form-check-label" for="automatedEnabled">{{ __('Enabled') }}</label>
                        </div>
                        <label class="form-label">{{ __('Frequency') }}</label>
                        <select name="frequency" class="form-select mb-3">
                            @foreach (['daily' => 'Daily', 'weekly' => 'Weekly', 'monthly' => 'Monthly'] as $value => $label)
                                <option value="{{ $value }}" @selected($settings->frequency === $value)>{{ __($label) }}</option>
                            @endforeach
                        </select>
                        <button type="submit" class="btn btn-primary btn-sm">{{ __('Save') }}</button>
                    </form>
                    <p class="form-text mt-3 mb-0">
                        {{ __('Runs automatically once your server\'s scheduler is set up — see deploy/README.md. Last ran:') }}
                        {{ $settings->last_run_at?->format('M j, Y H:i') ?? __('never') }}
                    </p>
                </div>
            </div>

            <div class="card shadow-sm">
                <div class="card-body">
                    <h6 class="text-uppercase text-muted small mb-3">{{ __('Restore from a backup file') }}</h6>
                    <div class="alert alert-warning small">
                        {{ __('Replaces the entire database. A safety copy of the current state is saved automatically first, but there\'s no confirmation beyond this warning — make sure this is the file you mean.') }}
                    </div>
                    <form action="{{ route('saas-admin.backups.restore') }}" method="POST" enctype="multipart/form-data"
                        onsubmit="return confirm('{{ __('This will replace the entire live database. Continue?') }}')">
                        @csrf
                        <input type="file" name="backup_file" class="form-control mb-3" accept=".sqlite,.sql" required>
                        @error('backup_file')
                            <p class="text-danger small mt-1">{{ $message }}</p>
                        @enderror
                        <button type="submit" class="btn btn-danger btn-sm">{{ __('Upload and restore') }}</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection
