@extends('layouts.app')

@section('title', 'Smart Storage')

@section('content')
<section class="content-header">
    <h1><i class="fa fa-hdd"></i> Smart Storage <small>Storage backends, encryption, OTP &amp; access tracking</small></h1>
</section>

<section class="content">
    @include('smartstorage::partials._nav')

    <form method="POST" action="{{ route('smart-storage.settings.update') }}">
        @csrf
        <div class="row">
            {{-- Backend selection --}}
            <div class="col-md-6">
                <div class="box box-primary">
                    <div class="box-header with-border"><h3 class="box-title">Storage backend</h3></div>
                    <div class="box-body">
                        <div class="form-group">
                            <label>Where files are stored</label>
                            <select name="active_backend" id="active_backend" class="form-control">
                                @foreach($backends as $key => $driver)
                                    <option value="{{ $key }}"
                                        @if($settings->active_backend === $key) selected @endif
                                        @if(!$driver->isAvailable()) disabled @endif>
                                        {{ $driver->label() }}@if(!$driver->isAvailable()) (unavailable) @endif
                                    </option>
                                @endforeach
                            </select>
                            <small class="text-muted">
                                "Laravel default" makes no changes to the app. Any other backend transparently
                                routes <em>all</em> uploads (main script + every module) through Smart Storage.
                            </small>
                        </div>

                        <div class="checkbox">
                            <label>
                                <input type="hidden" name="encryption_enabled" value="0">
                                <input type="checkbox" name="encryption_enabled" value="1" @if($settings->encryption_enabled) checked @endif>
                                Encrypt files at rest (AES-256-GCM)
                            </label>
                        </div>

                        <div class="checkbox">
                            <label>
                                <input type="hidden" name="track_writes" value="0">
                                <input type="checkbox" name="track_writes" value="1" @if($settings->track_writes) checked @endif>
                                Track uploads (writes)
                            </label>
                        </div>
                        <div class="checkbox">
                            <label>
                                <input type="hidden" name="track_reads" value="0">
                                <input type="checkbox" name="track_reads" value="1" @if($settings->track_reads) checked @endif>
                                Track every file/image view (reads)
                            </label>
                        </div>

                        <div class="form-group">
                            <label>Geolocation provider for logs</label>
                            <select name="geo_provider" class="form-control">
                                <option value="none" @if($settings->geo_provider==='none') selected @endif>None (offline)</option>
                                <option value="ip-api" @if($settings->geo_provider==='ip-api') selected @endif>ip-api.com (public IPs only)</option>
                            </select>
                        </div>

                        <button type="button" class="btn btn-default btn-sm" id="ss-test-backend">
                            <i class="fa fa-plug"></i> Test connection
                        </button>
                        <span id="ss-test-result" style="margin-left:8px;"></span>
                    </div>
                </div>

                {{-- OTP --}}
                <div class="box box-warning">
                    <div class="box-header with-border"><h3 class="box-title">OTP access gate</h3></div>
                    <div class="box-body">
                        <div class="checkbox">
                            <label>
                                <input type="hidden" name="otp_enabled" value="0">
                                <input type="checkbox" name="otp_enabled" value="1" @if($settings->otp_enabled) checked @endif>
                                Require a one-time code to open a storage-access session
                            </label>
                        </div>
                        <div class="form-group">
                            <label>Session lifetime (minutes)</label>
                            <input type="number" name="otp_ttl_minutes" class="form-control" value="{{ $settings->otp_ttl_minutes }}" min="1" max="1440">
                        </div>
                        <div class="form-group">
                            <label>Delivery channel</label>
                            <select name="otp_channel" class="form-control">
                                @foreach(['auto'=>'Auto (WhatsApp → e-mail → log)','whatsapp'=>'WhatsApp','mail'=>'E-mail','log'=>'Log only'] as $v=>$l)
                                    <option value="{{ $v }}" @if($settings->otp_channel===$v) selected @endif>{{ $l }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                </div>
            </div>

            {{-- Local sub-modes --}}
            <div class="col-md-6">
                <div class="box box-info ss-backend-panel" data-backend="local">
                    <div class="box-header with-border"><h3 class="box-title">Local storage access mode</h3></div>
                    <div class="box-body">
                        <div class="form-group">
                            <label>Who can reach the local store</label>
                            <select name="local_submode" class="form-control">
                                @foreach($submodes as $v => $l)
                                    <option value="{{ $v }}" @if($settings->local_submode===$v) selected @endif>{{ $l }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="form-group">
                            <label>LAN ranges (CIDR, comma-separated)</label>
                            <input type="text" name="lan_cidrs" class="form-control" value="{{ $settings->lan_cidrs }}">
                            <small class="text-muted">Used by "Local network only" and as the trusted side of "tunnel".</small>
                        </div>
                        <div class="checkbox">
                            <label>
                                <input type="checkbox" name="rebind_device" value="1">
                                Re-bind "this device only" to the next device that accesses storage
                            </label>
                        </div>
                        <hr>
                        <h4>Internet tunnel</h4>
                        <div class="form-group">
                            <label>Tunnel URL (informational)</label>
                            <input type="text" name="tunnel_url" class="form-control" value="{{ $settings->tunnel_url }}" placeholder="https://files.example.com">
                        </div>
                        <div class="form-group">
                            <label>Tunnel shared secret</label>
                            <input type="password" name="tunnel_secret" class="form-control" autocomplete="new-password" placeholder="{{ $settings->tunnel_secret ? '•••••••• (unchanged)' : 'set a secret' }}">
                            <small class="text-muted">Your tunnel/reverse-proxy must send this as the <code>X-Smart-Storage-Tunnel</code> header.</small>
                        </div>
                    </div>
                </div>

                {{-- S3 --}}
                <div class="box box-default ss-backend-panel" data-backend="s3">
                    <div class="box-header with-border"><h3 class="box-title">AWS S3 credentials</h3></div>
                    <div class="box-body">
                        <div class="form-group"><label>Access key</label><input type="text" name="s3_key" class="form-control" value="{{ $creds['s3']['key'] ?? '' }}"></div>
                        <div class="form-group"><label>Secret</label><input type="password" name="s3_secret" class="form-control" autocomplete="new-password" placeholder="{{ ($creds['s3']['secret'] ?? '') ? '•••••••• (unchanged)' : '' }}"></div>
                        <div class="form-group"><label>Region</label><input type="text" name="s3_region" class="form-control" value="{{ $creds['s3']['region'] ?? '' }}"></div>
                        <div class="form-group"><label>Bucket</label><input type="text" name="s3_bucket" class="form-control" value="{{ $creds['s3']['bucket'] ?? '' }}"></div>
                        <div class="form-group"><label>Endpoint (optional, S3-compatible)</label><input type="text" name="s3_endpoint" class="form-control" value="{{ $creds['s3']['endpoint'] ?? '' }}"></div>
                        <div class="form-group"><label>Path prefix (optional)</label><input type="text" name="s3_prefix" class="form-control" value="{{ $creds['s3']['prefix'] ?? '' }}"></div>
                    </div>
                </div>

                {{-- Dropbox --}}
                <div class="box box-default ss-backend-panel" data-backend="dropbox">
                    <div class="box-header with-border"><h3 class="box-title">Dropbox credentials</h3></div>
                    <div class="box-body">
                        <div class="form-group"><label>Access token</label><input type="password" name="dropbox_token" class="form-control" autocomplete="new-password" placeholder="{{ ($creds['dropbox']['token'] ?? '') ? '•••••••• (unchanged)' : '' }}"></div>
                        <div class="form-group"><label>Path prefix (optional)</label><input type="text" name="dropbox_prefix" class="form-control" value="{{ $creds['dropbox']['prefix'] ?? '' }}"></div>
                    </div>
                </div>

                {{-- Google Drive --}}
                <div class="box box-default ss-backend-panel" data-backend="gdrive">
                    <div class="box-header with-border"><h3 class="box-title">Google Drive credentials</h3></div>
                    <div class="box-body">
                        @if(!($backends['gdrive']->isAvailable() ?? false))
                            <p class="text-warning"><i class="fa fa-exclamation-triangle"></i>
                                Run <code>composer require masbug/flysystem-google-drive-ext</code> to enable this backend.</p>
                        @endif
                        <div class="form-group"><label>Client ID</label><input type="text" name="gdrive_client_id" class="form-control" value="{{ $creds['gdrive']['client_id'] ?? '' }}"></div>
                        <div class="form-group"><label>Client secret</label><input type="password" name="gdrive_client_secret" class="form-control" autocomplete="new-password" placeholder="{{ ($creds['gdrive']['client_secret'] ?? '') ? '•••••••• (unchanged)' : '' }}"></div>
                        <div class="form-group"><label>Refresh token</label><input type="password" name="gdrive_refresh_token" class="form-control" autocomplete="new-password" placeholder="{{ ($creds['gdrive']['refresh_token'] ?? '') ? '•••••••• (unchanged)' : '' }}"></div>
                        <div class="form-group"><label>Root folder ID (optional)</label><input type="text" name="gdrive_root_folder_id" class="form-control" value="{{ $creds['gdrive']['root_folder_id'] ?? '' }}"></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">
                <button type="submit" class="btn btn-primary"><i class="fa fa-save"></i> Save settings</button>
            </div>
        </div>
    </form>
</section>

@endsection

@section('javascript')
<script>
$(function () {
    function syncPanels() {
        var b = $('#active_backend').val();
        $('.ss-backend-panel').each(function () {
            $(this).toggle($(this).data('backend') === b);
        });
    }
    $('#active_backend').on('change', syncPanels);
    syncPanels();

    $('#ss-test-backend').on('click', function () {
        var $r = $('#ss-test-result').html('<i class="fa fa-spinner fa-spin"></i> testing…');
        $.post('{{ route('smart-storage.test') }}', {
            _token: '{{ csrf_token() }}',
            backend: $('#active_backend').val()
        }).done(function (res) {
            $r.html('<span class="text-' + (res.success ? 'success' : 'danger') + '">' + res.msg + '</span>');
        }).fail(function () {
            $r.html('<span class="text-danger">Request failed.</span>');
        });
    });
});
</script>
@endsection
