@extends('layouts.app-shell')
@section('title', 'Settings')
@section('sidebar')
    @include('admin._sidebar')
@endsection
@section('bottomnav')
    <a href="{{ route('admin.dashboard') }}" class="{{ request()->routeIs('admin.dashboard*') ? 'active' : '' }}"><x-icon name="home" :size="20" /><span>Home</span></a>
    <a href="{{ route('admin.bookings') }}" class="{{ request()->routeIs('admin.bookings*') ? 'active' : '' }}"><x-icon name="bookings" :size="20" /><span>Subs</span></a>
    <a href="{{ route('admin.partners') }}" class="{{ request()->routeIs('admin.partners*') ? 'active' : '' }}"><x-icon name="profile" :size="20" /><span>Partners</span></a>
    <a href="{{ route('admin.services.index') }}" class="{{ request()->routeIs('admin.services*') ? 'active' : '' }}"><x-icon name="settings" :size="20" /><span>Catalogue</span></a>
@endsection

@section('content')
@php
    $groupMeta = [
        'general' => ['title' => 'General', 'icon' => 'home', 'sub' => 'Site identity and contact info'],
        'billing' => ['title' => 'Billing', 'icon' => 'money', 'sub' => 'Tax and currency'],
        'commission' => ['title' => 'Commission', 'icon' => 'money', 'sub' => 'Platform commission on each visit & subscription'],
        'social'  => ['title' => 'Social & SEO', 'icon' => 'whatsapp', 'sub' => 'Social links and meta'],
        'policy'  => ['title' => 'Policies', 'icon' => 'shield', 'sub' => 'Terms and refund text'],
    ];
    $knownIcons = [
        'site_name' => 'home', 'site_tagline' => 'sparkle',
        'site_phone' => 'phone', 'site_email' => 'mail',
        'site_whatsapp' => 'whatsapp', 'site_address' => 'pin',
        'currency' => 'money', 'tax_percent' => 'money',
        'commission.platform_percent' => 'percent',
        'commission.first_month_bonus_paise' => 'gift',
        'commission.tds_percent' => 'shield',
        'commission.min_payout_paise' => 'wallet',
        'commission.policy_note' => 'shield',
    ];
@endphp

<div class="flex-between mb-3">
    <div>
        <h2 style="font-size: 22px; font-weight: 700;">Site settings</h2>
        <p class="muted" style="font-size: 13px;">Configure branding, billing, commissions and policies.</p>
    </div>
</div>

@if(session('success'))
    <div class="alert alert-success">{{ session('success') }}</div>
@endif

<form method="POST" action="{{ route('admin.settings.save') }}">
    @csrf

    @foreach($groups as $group => $items)
        @php $meta = $groupMeta[$group] ?? ['title' => ucfirst($group), 'icon' => 'settings', 'sub' => '']; @endphp
        <div class="panel">
            <div class="panel-head">
                <h3>
                    <span class="ic"><x-icon :name="$meta['icon']" :size="14" /></span>
                    {{ $meta['title'] }}
                </h3>
                <span class="meta">{{ $items->count() }} key{{ $items->count() === 1 ? '' : 's' }} · {{ $meta['sub'] }}</span>
            </div>

            @if($group === 'commission')
                {{-- Special commission UI with side-by-side breakdown preview --}}
                @php
                    $pp = (float) ($items->firstWhere('key', 'commission.platform_percent')?->value ?? 15);
                    $tds = (float) ($items->firstWhere('key', 'commission.tds_percent')?->value ?? 0);
                    $minPayout = (int) ($items->firstWhere('key', 'commission.min_payout_paise')?->value ?? 10000);
                    $firstMonthBonus = (int) ($items->firstWhere('key', 'commission.first_month_bonus_paise')?->value ?? 0);
                    $policyNote = $items->firstWhere('key', 'commission.policy_note')?->value ?? '';
                @endphp
                <div style="display: grid; grid-template-columns: 1.2fr 1fr; gap: 22px;">
                    <div class="app-form">
                        <div class="form-grid">
                            <div class="field">
                                <label>
                                    <span style="display:inline-flex; align-items:center; gap:6px;">
                                        <x-icon name="percent" :size="13" style="color: var(--bfk-text-3);" />
                                        Platform commission %
                                    </span>
                                </label>
                                <input type="number" step="0.5" min="0" max="100" name="settings[commission.platform_percent]" value="{{ $pp }}" required>
                                <span class="hint">Default cut on every booking & visit. Per-partner overrides win over this.</span>
                            </div>
                            <div class="field">
                                <label>
                                    <span style="display:inline-flex; align-items:center; gap:6px;">
                                        <x-icon name="shield" :size="13" style="color: var(--bfk-text-3);" />
                                        TDS %
                                    </span>
                                </label>
                                <input type="number" step="0.1" min="0" max="30" name="settings[commission.tds_percent]" value="{{ $tds }}" required>
                                <span class="hint">Withheld from partner share (per India tax rules).</span>
                            </div>
                            <div class="field">
                                <label>
                                    <span style="display:inline-flex; align-items:center; gap:6px;">
                                        <x-icon name="wallet" :size="13" style="color: var(--bfk-text-3);" />
                                        Minimum payout (₹)
                                    </span>
                                </label>
                                <input type="number" step="1" min="0" name="settings[commission.min_payout_paise]" value="{{ (int) round($minPayout/100) }}" required>
                                <span class="hint">Minimum partner balance before they can request payout.</span>
                            </div>
                            <div class="field">
                                <label>
                                    <span style="display:inline-flex; align-items:center; gap:6px;">
                                        <x-icon name="gift" :size="13" style="color: var(--bfk-text-3);" />
                                        First-month bonus (₹)
                                    </span>
                                </label>
                                <input type="number" step="1" min="0" name="settings[commission.first_month_bonus_paise]" value="{{ (int) round($firstMonthBonus/100) }}" required>
                                <span class="hint">Bonus added to partner net during their first month.</span>
                            </div>
                        </div>
                        <div class="field">
                            <label>Policy note (shown to partners)</label>
                            <textarea name="settings[commission.policy_note]" rows="2">{{ $policyNote }}</textarea>
                        </div>
                    </div>

                    {{-- Live breakdown preview --}}
                    <div style="background: linear-gradient(135deg, #fff7f3, #ffe7d2); border-radius: 14px; padding: 18px; border: 1px solid #ffd9b8;">
                        <div style="display:flex; gap:8px; align-items:center; margin-bottom: 14px;">
                            <span style="width: 32px; height: 32px; border-radius: 9px; background: var(--bfk-grad); display: grid; place-items: center; color: #fff;">
                                <x-icon name="money" :size="14" />
                            </span>
                            <strong style="font-size: 14px;">Live breakdown preview</strong>
                        </div>
                        @php
                            $examples = [1000, 3000, 10000];
                        @endphp
                        @foreach($examples as $ex)
                            @php
                                $platform = (int) round($ex * ($pp / 100));
                                $partnerShare = $ex - $platform;
                                $tdsAmt = (int) round($partnerShare * ($tds / 100));
                                $partnerNet = $partnerShare - $tdsAmt;
                            @endphp
                            <div style="background: #fff; border-radius: 10px; padding: 12px; margin-bottom: 8px; border: 1px solid #fde0c2;">
                                <div style="font-size: 11px; color: var(--bfk-text-3); text-transform: uppercase; letter-spacing: 0.06em; font-weight: 700; margin-bottom: 6px;">On ₹{{ number_format($ex, 0) }} subscription</div>
                                <div style="display: grid; grid-template-columns: 1fr auto 1fr auto 1fr; gap: 6px; align-items: center; font-size: 12px;">
                                    <div><strong style="display:block;">₹{{ number_format($ex, 0) }}</strong><span style="color: var(--bfk-text-3); font-size: 10px;">Gross</span></div>
                                    <span style="color: var(--bfk-text-3);">→</span>
                                    <div><strong style="display:block; color: var(--bfk-primary);">−₹{{ number_format($platform, 0) }}</strong><span style="color: var(--bfk-text-3); font-size: 10px;">Platform ({{ number_format($pp, 1) }}%)</span></div>
                                    @if($tds > 0)
                                        <span style="color: var(--bfk-text-3);">→</span>
                                        <div><strong style="display:block; color: var(--bfk-red);">−₹{{ number_format($tdsAmt, 0) }}</strong><span style="color: var(--bfk-text-3); font-size: 10px;">TDS ({{ number_format($tds, 1) }}%)</span></div>
                                    @else
                                        <span style="color: var(--bfk-text-3);">→</span>
                                        <div><strong style="display:block; color: var(--bfk-green);">₹{{ number_format($partnerNet, 0) }}</strong><span style="color: var(--bfk-text-3); font-size: 10px;">Partner net</span></div>
                                    @endif
                                </div>
                                @if($tds > 0)
                                    <div style="margin-top: 6px; padding-top: 6px; border-top: 1px dashed #fde0c2; font-size: 11px;">
                                        <span style="color: var(--bfk-text-3);">Partner net after TDS: </span>
                                        <strong style="color: var(--bfk-green);">₹{{ number_format($partnerNet, 0) }}</strong>
                                    </div>
                                @endif
                            </div>
                        @endforeach
                        <p style="font-size: 11px; color: var(--bfk-text-3); margin-top: 8px; margin-bottom: 0;">Partners see this breakdown live on each booking.</p>
                    </div>
                </div>
            @else
                <div class="form-grid">
                    @foreach($items as $s)
                        @php
                            $ic = $knownIcons[$s->key] ?? 'settings';
                            $val = $s->value ?? '';
                            $isLong = strlen($val) > 80;
                            $isNum = in_array($s->key, ['tax_percent']);
                            $humanKey = ucwords(str_replace(['site_', '_'], ['', ' '], $s->key));
                        @endphp
                        <div class="field">
                            <label>
                                <span style="display:inline-flex; align-items:center; gap:6px;">
                                    <x-icon :name="$ic" :size="13" style="color: var(--bfk-text-3);" />
                                    {{ $humanKey }}
                                </span>
                            </label>
                            @if($isLong || $s->key === 'site_address')
                                <textarea name="settings[{{ $s->key }}]" rows="2">{{ $val }}</textarea>
                            @elseif($isNum)
                                <input type="number" step="0.01" min="0" name="settings[{{ $s->key }}]" value="{{ $val }}">
                            @else
                                <input name="settings[{{ $s->key }}]" value="{{ $val }}">
                            @endif
                            <span class="hint">Key: <code style="background:var(--bfk-bg-soft); padding:1px 5px; border-radius:4px; font-size:11px;">{{ $s->key }}</code></span>
                        </div>
                    @endforeach
                </div>
            @endif
        </div>
    @endforeach

    <div class="panel" style="display: flex; gap: 10px; align-items: center; justify-content: flex-end; background: var(--bfk-bg-soft); border-style: dashed;">
        <a href="{{ route('admin.dashboard') }}" class="btn btn-ghost">Cancel</a>
        <button class="btn btn-primary"><x-icon name="check" :size="14" /> Save all settings</button>
    </div>
</form>
@endsection