@extends('layouts.app')

@section('title', __('whatsapp::lang.whatsapp') . ' — ' . __('whatsapp::lang.settings'))

@section('content')
<section class="content-header">
    <h1><i class="fab fa-whatsapp"></i> {{ __('whatsapp::lang.whatsapp') }}
        <small>{{ __('whatsapp::lang.settings') }}</small></h1>
</section>

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

    <form method="POST" action="{{ route('whatsapp.settings.update') }}">
        @csrf @method('PUT')

        <div class="row">
            <div class="col-md-6">
                <div class="box box-primary">
                    <div class="box-header with-border"><h3 class="box-title">{{ __('whatsapp::lang.settings') }}</h3></div>
                    <div class="box-body">
                        <div class="checkbox">
                            <label><input type="checkbox" name="enabled" value="1" {{ ($settings['enabled'] ?? '1') == '1' ? 'checked' : '' }}>
                                {{ __('whatsapp::lang.enabled') }}</label>
                        </div>

                        <div class="form-group">
                            <label>{{ __('whatsapp::lang.default_channel') }}</label>
                            <select name="default_channel" class="form-control">
                                <option value="">— none —</option>
                                @foreach($channels as $id => $label)
                                    <option value="{{ $id }}" {{ (string)($settings['default_channel'] ?? '') === (string)$id ? 'selected' : '' }}>{{ $label }}</option>
                                @endforeach
                            </select>
                        </div>

                        <div class="form-group">
                            <label>{{ __('whatsapp::lang.notify_emails') }}</label>
                            <input type="text" name="notify_emails" class="form-control" value="{{ $settings['notify_emails'] ?? '' }}" placeholder="ops@example.com, admin@example.com">
                            <small class="text-muted">Alerted when a number disconnects and needs a QR re-scan.</small>
                        </div>

                        <div class="form-group">
                            <label>{{ __('whatsapp::lang.opt_out_keyword') }}</label>
                            <input type="text" name="opt_out_keyword" class="form-control" value="{{ $settings['opt_out_keyword'] ?? 'STOP' }}">
                        </div>

                        <div class="checkbox">
                            <label><input type="checkbox" name="ai_enabled" value="1" {{ ($settings['ai_enabled'] ?? '0') == '1' ? 'checked' : '' }}>
                                {{ __('whatsapp::lang.ai_enabled') }}</label>
                        </div>
                        <div class="checkbox" style="margin-left:20px;">
                            <label><input type="checkbox" name="ai_can_book" value="1" {{ ($settings['ai_can_book'] ?? '0') == '1' ? 'checked' : '' }}>
                                {{ __('whatsapp::lang.ai_can_book') }}</label>
                            <br><small class="text-muted" style="margin-left:20px;">{{ __('whatsapp::lang.ai_can_book_hint') }}</small>
                        </div>

                        <div class="checkbox">
                            <label><input type="checkbox" name="powered_by" value="1" {{ ($settings['powered_by'] ?? '1') == '1' ? 'checked' : '' }}>
                                {{ __('whatsapp::lang.powered_by') }}</label>
                        </div>
                    </div>
                </div>

                <div class="box box-default">
                    <div class="box-header with-border"><h3 class="box-title"><i class="fa fa-boxes"></i> Inventory alerts</h3></div>
                    <div class="box-body">
                        <div class="checkbox">
                            <label><input type="checkbox" name="low_stock_enabled" value="1" {{ ($settings['low_stock_enabled'] ?? '0') == '1' ? 'checked' : '' }}>
                                Daily low-stock WhatsApp alert</label>
                            <br><small class="text-muted">Each morning, WhatsApp a summary of products at or below their reorder (alert) level.</small>
                        </div>
                        <div class="form-group">
                            <label>Send alerts to (WhatsApp number)</label>
                            <input type="text" name="low_stock_number" class="form-control" value="{{ $settings['low_stock_number'] ?? '' }}" placeholder="+201234567890">
                            <small class="text-muted">The manager / purchasing number that should receive the low-stock summary.</small>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-md-6">
                @if(!is_null($aiUsage))
                <div class="box box-success">
                    <div class="box-header with-border">
                        <h3 class="box-title"><i class="fa fa-robot"></i> {{ __('whatsapp::lang.ai_usage_title') }} — {{ $aiUsage['month'] }}</h3>
                    </div>
                    <div class="box-body">
                        <div class="row text-center">
                            <div class="col-xs-4">
                                <div style="font-size:20px;font-weight:600;">{{ number_format($aiUsage['calls']) }}</div>
                                <small class="text-muted">{{ __('whatsapp::lang.ai_calls') }}</small>
                            </div>
                            <div class="col-xs-4">
                                <div style="font-size:20px;font-weight:600;">{{ number_format($aiUsage['tokens']) }}</div>
                                <small class="text-muted">{{ __('whatsapp::lang.ai_tokens') }}</small>
                            </div>
                            <div class="col-xs-4">
                                <div style="font-size:20px;font-weight:600;color:#00a65a;">${{ number_format($aiUsage['cost'], 2) }}</div>
                                <small class="text-muted">{{ __('whatsapp::lang.ai_est_cost') }}</small>
                            </div>
                        </div>
                        @if($aiUsage['calls'] > 0)
                            <hr style="margin:10px 0;">
                            <table class="table table-condensed" style="margin-bottom:4px;">
                                <thead><tr><th>{{ __('whatsapp::lang.ai_model') }}</th><th class="text-right">{{ __('whatsapp::lang.ai_calls') }}</th><th class="text-right">In</th><th class="text-right">Out</th><th class="text-right">$</th></tr></thead>
                                <tbody>
                                @foreach($aiUsage['models'] as $m)
                                    <tr>
                                        <td><small>{{ $m['provider'] }} / {{ $m['model'] }}</small></td>
                                        <td class="text-right">{{ number_format($m['calls']) }}</td>
                                        <td class="text-right">{{ number_format($m['tin']) }}</td>
                                        <td class="text-right">{{ number_format($m['tout']) }}</td>
                                        <td class="text-right">${{ number_format($m['cost'], 2) }}</td>
                                    </tr>
                                @endforeach
                                </tbody>
                            </table>
                            @if($aiUsage['failed'] > 0)
                                <small class="text-red">{{ $aiUsage['failed'] }} {{ __('whatsapp::lang.ai_failed_calls') }}</small>
                            @endif
                        @else
                            <p class="text-muted text-center" style="margin:8px 0 0;">{{ __('whatsapp::lang.ai_no_usage') }}</p>
                        @endif
                        <p class="text-muted" style="margin:8px 0 0;font-size:11px;">{{ __('whatsapp::lang.ai_cost_note') }}</p>
                    </div>
                </div>
                @endif

                <div class="box box-warning">
                    <div class="box-header with-border"><h3 class="box-title"><i class="fa fa-shield-alt"></i> {{ __('whatsapp::lang.anti_ban_title') }}</h3></div>
                    <div class="box-body">
                        <p class="text-muted">{{ __('whatsapp::lang.anti_ban_hint') }}</p>
                        <div class="row">
                            <div class="col-xs-6 form-group">
                                <label>{{ __('whatsapp::lang.gap_min') }}</label>
                                <input type="number" min="0" name="gap_min" class="form-control" value="{{ $settings['gap_min'] ?? 4 }}">
                            </div>
                            <div class="col-xs-6 form-group">
                                <label>{{ __('whatsapp::lang.gap_max') }}</label>
                                <input type="number" min="0" name="gap_max" class="form-control" value="{{ $settings['gap_max'] ?? 12 }}">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-xs-4 form-group">
                                <label>{{ __('whatsapp::lang.typing_per_char') }}</label>
                                <input type="number" min="0" name="typing_per_char_ms" class="form-control" value="{{ $settings['typing_per_char_ms'] ?? 55 }}">
                            </div>
                            <div class="col-xs-4 form-group">
                                <label>{{ __('whatsapp::lang.typing_min') }}</label>
                                <input type="number" min="0" name="typing_min_ms" class="form-control" value="{{ $settings['typing_min_ms'] ?? 900 }}">
                            </div>
                            <div class="col-xs-4 form-group">
                                <label>{{ __('whatsapp::lang.typing_max') }}</label>
                                <input type="number" min="0" name="typing_max_ms" class="form-control" value="{{ $settings['typing_max_ms'] ?? 9000 }}">
                            </div>
                        </div>
                        <p class="text-muted" style="margin-bottom:0;">{{ __('whatsapp::lang.anti_ban_note') }}</p>
                    </div>
                </div>

                <div class="box box-info">
                    <div class="box-header with-border"><h3 class="box-title">Worker &amp; cron</h3></div>
                    <div class="box-body">
                        <p>WhatsApp-Web (QR) channels deliver via the Node bridge — no worker needed. For <strong>Meta Cloud API</strong> channels and the auto-resend safety net, run a worker:</p>
                        <pre>php artisan queue:work {{ config('whatsapp.queue.connection') }} --queue={{ config('whatsapp.queue.queue') }} --tries=1 --timeout=120</pre>
                        <p class="text-muted">Cron jobs (<code>whatsapp:sweep</code>, <code>whatsapp:health-check</code>, <code>whatsapp:appointment-reminders</code>) are auto-registered by the module — just ensure Laravel's scheduler runs.</p>
                    </div>
                </div>
            </div>
        </div>

        {{-- Automated notifications matrix --}}
        <div class="box box-warning">
            <div class="box-header with-border">
                <h3 class="box-title"><i class="fa fa-bolt"></i> {{ __('whatsapp::lang.automated_title') }}</h3>
            </div>
            <div class="box-body table-responsive">
                <p class="text-muted">{{ __('whatsapp::lang.automated_hint') }}</p>
                @foreach($events as $group => $items)
                    <h4 style="border-bottom:1px solid #eee;padding-bottom:4px;">{{ $group }}</h4>
                    <table class="table table-condensed" style="margin-bottom:18px;">
                        <thead><tr>
                            <th style="width:60px;">On</th>
                            <th>{{ __('whatsapp::lang.event') }}</th>
                            <th style="width:40%;">{{ __('whatsapp::lang.template') }}</th>
                        </tr></thead>
                        <tbody>
                        @foreach($items as $event)
                            <tr>
                                <td class="text-center">
                                    <input type="checkbox" name="{{ $event['toggle'] }}" value="1"
                                        {{ ($settings[$event['toggle']] ?? '1') == '1' ? 'checked' : '' }}>
                                </td>
                                <td>{{ $event['label'] }}</td>
                                <td>
                                    <select name="{{ $event['template'] }}" class="form-control input-sm">
                                        @foreach($templates as $name)
                                            <option value="{{ $name }}" {{ ($settings[$event['template']] ?? $event['default']) === $name ? 'selected' : '' }}>{{ $name }}</option>
                                        @endforeach
                                    </select>
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                @endforeach
            </div>
            <div class="box-footer">
                <button type="submit" class="btn btn-primary"><i class="fa fa-save"></i> Save all settings</button>
            </div>
        </div>
    </form>
</section>
@endsection
