<x-admin-layout title="Company Settings">
    <form method="POST" action="{{ route('admin.settings.update') }}">
        @csrf
        @method('PUT')

        <div class="card mb-4">
            <div class="card-body">
                <h5 class="fw-bold mb-4">Company Profile</h5>
                <div class="row g-3">
                    <div class="col-md-6">
                        <label for="name" class="form-label fw-semibold">Company Name</label>
                        <input id="name" name="name" type="text" value="{{ old('name', $company->name) }}" required
                               class="form-control @error('name') is-invalid @enderror">
                        @error('name')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-md-6">
                        <label for="address" class="form-label fw-semibold">Address</label>
                        <input id="address" name="address" type="text" value="{{ old('address', $company->address) }}"
                               class="form-control @error('address') is-invalid @enderror">
                        @error('address')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-md-4">
                        <label for="contact_person" class="form-label fw-semibold">Contact Person</label>
                        <input id="contact_person" name="contact_person" type="text" value="{{ old('contact_person', $company->contact_person) }}"
                               class="form-control @error('contact_person') is-invalid @enderror">
                        @error('contact_person')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-md-4">
                        <label for="contact_phone" class="form-label fw-semibold">Contact Phone</label>
                        <input id="contact_phone" name="contact_phone" type="text" value="{{ old('contact_phone', $company->contact_phone) }}"
                               class="form-control @error('contact_phone') is-invalid @enderror">
                        @error('contact_phone')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-md-4">
                        <label for="contact_email" class="form-label fw-semibold">Contact Email</label>
                        <input id="contact_email" name="contact_email" type="email" value="{{ old('contact_email', $company->contact_email) }}"
                               class="form-control @error('contact_email') is-invalid @enderror">
                        @error('contact_email')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                </div>
            </div>
        </div>

        <div class="card mb-4">
            <div class="card-body">
                <h5 class="fw-bold mb-1">Localization &amp; Map</h5>
                <p class="text-muted small mb-4">Currency/distance unit drive fare calculations; latitude/longitude center the live fleet map.</p>
                <div class="row g-3">
                    <div class="col-md-4">
                        <label for="country_id" class="form-label fw-semibold">Country</label>
                        <select id="country_id" name="country_id" class="form-select @error('country_id') is-invalid @enderror">
                            <option value="">— Select —</option>
                            @foreach ($countries as $country)
                                <option value="{{ $country->id }}" @selected(old('country_id', $company->country_id) == $country->id)>{{ $country->name }}</option>
                            @endforeach
                        </select>
                        @error('country_id')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-md-4">
                        <label for="currency_id" class="form-label fw-semibold">Currency</label>
                        <select id="currency_id" name="currency_id" class="form-select @error('currency_id') is-invalid @enderror">
                            <option value="">— Select —</option>
                            @foreach ($currencies as $currency)
                                <option value="{{ $currency->id }}" @selected(old('currency_id', $company->currency_id) == $currency->id)>
                                    {{ $currency->name }} ({{ $currency->code }})
                                </option>
                            @endforeach
                        </select>
                        @error('currency_id')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-md-4">
                        <label for="distance_unit" class="form-label fw-semibold">Distance Unit</label>
                        <select id="distance_unit" name="distance_unit" class="form-select @error('distance_unit') is-invalid @enderror">
                            <option value="mi" @selected(old('distance_unit', $company->distance_unit) == 'mi')>Miles</option>
                            <option value="km" @selected(old('distance_unit', $company->distance_unit) == 'km')>Kilometers</option>
                        </select>
                        @error('distance_unit')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-md-4">
                        <label for="timezone" class="form-label fw-semibold">Timezone</label>
                        <input id="timezone" name="timezone" type="text" value="{{ old('timezone', $company->timezone) }}" required
                               class="form-control @error('timezone') is-invalid @enderror">
                        @error('timezone')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-md-4">
                        <label for="latitude" class="form-label fw-semibold">Map Center — Latitude</label>
                        <input id="latitude" name="latitude" type="number" step="0.0000001" value="{{ old('latitude', $company->latitude) }}"
                               class="form-control @error('latitude') is-invalid @enderror">
                        @error('latitude')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-md-4">
                        <label for="longitude" class="form-label fw-semibold">Map Center — Longitude</label>
                        <input id="longitude" name="longitude" type="number" step="0.0000001" value="{{ old('longitude', $company->longitude) }}"
                               class="form-control @error('longitude') is-invalid @enderror">
                        @error('longitude')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                </div>
            </div>
        </div>

        <div class="card mb-4">
            <div class="card-body">
                <h5 class="fw-bold mb-1">SMS &amp; Voice (Twilio)</h5>
                <p class="text-muted small mb-4">
                    Texts customers a link to their booking and powers click-to-call in the dispatch console.
                    API Key is <code>ACCOUNT_SID:AUTH_TOKEN</code> — leave it blank when saving to keep the current one.
                </p>
                <div class="row g-3">
                    <div class="col-md-4">
                        <label for="sms_provider" class="form-label fw-semibold">Provider</label>
                        <select id="sms_provider" name="sms_provider" class="form-select @error('sms_provider') is-invalid @enderror">
                            <option value="">— Not set —</option>
                            <option value="twilio" @selected(old('sms_provider', $company->sms_provider) === 'twilio')>Twilio</option>
                        </select>
                        @error('sms_provider')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-md-4">
                        <label for="sms_sender_id" class="form-label fw-semibold">From Number</label>
                        <input id="sms_sender_id" name="sms_sender_id" type="text" maxlength="20" placeholder="+15551234567" value="{{ old('sms_sender_id', $company->sms_sender_id) }}"
                               class="form-control @error('sms_sender_id') is-invalid @enderror">
                        @error('sms_sender_id')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-md-4">
                        <label for="sms_api_key" class="form-label fw-semibold">API Key</label>
                        <input id="sms_api_key" name="sms_api_key" type="password" autocomplete="off"
                               placeholder="{{ $company->sms_api_key ? '••••••••  (set — leave blank to keep)' : 'ACxxxx:authtoken' }}"
                               class="form-control @error('sms_api_key') is-invalid @enderror">
                        @error('sms_api_key')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-md-6">
                        <label for="twiml_app_sid" class="form-label fw-semibold">TwiML App SID <span class="text-muted fw-normal">(for click-to-call)</span></label>
                        <input id="twiml_app_sid" name="twiml_app_sid" type="text" value="{{ old('twiml_app_sid', $company->twiml_app_sid) }}"
                               class="form-control @error('twiml_app_sid') is-invalid @enderror">
                        @error('twiml_app_sid')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-md-6">
                        <label for="whatsapp_sender_id" class="form-label fw-semibold">WhatsApp Number <span class="text-muted fw-normal">(optional)</span></label>
                        <input id="whatsapp_sender_id" name="whatsapp_sender_id" type="text" maxlength="30" placeholder="+15551234567"
                               value="{{ old('whatsapp_sender_id', $company->whatsapp_sender_id) }}"
                               class="form-control @error('whatsapp_sender_id') is-invalid @enderror">
                        <div class="form-text">A WhatsApp-approved Twilio sender — set this and booking confirmations go over WhatsApp instead of SMS.</div>
                        @error('whatsapp_sender_id')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-12">
                        <label for="sms_template" class="form-label fw-semibold">Customer SMS Template</label>
                        <input id="sms_template" name="sms_template" type="text" maxlength="500"
                               value="{{ old('sms_template', $company->sms_template) }}"
                               placeholder="Hi {customer_name}, your booking {booking_number} is confirmed: {link}"
                               class="form-control @error('sms_template') is-invalid @enderror">
                        <div class="form-text">Placeholders: {{ '{customer_name}' }}, {{ '{driver_name}' }}, {{ '{booking_number}' }}, {{ '{link}' }}</div>
                        @error('sms_template')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                </div>
            </div>
        </div>

        <div class="card mb-4">
            <div class="card-body">
                <h5 class="fw-bold mb-1">Card Payments (Stripe)</h5>
                <p class="text-muted small mb-4">
                    Lets passengers pay by card in the mobile app. Card details are collected by Stripe's own SDK and
                    never reach this server — only a payment reference does. Leave blank to keep the current value.
                </p>
                <div class="row g-3">
                    <div class="col-md-4">
                        <label for="stripe_publishable_key" class="form-label fw-semibold">Publishable Key</label>
                        <input id="stripe_publishable_key" name="stripe_publishable_key" type="text"
                               value="{{ old('stripe_publishable_key', $company->stripe_publishable_key) }}"
                               placeholder="pk_live_..." class="form-control @error('stripe_publishable_key') is-invalid @enderror">
                        @error('stripe_publishable_key')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-md-4">
                        <label for="stripe_secret_key" class="form-label fw-semibold">Secret Key</label>
                        <input id="stripe_secret_key" name="stripe_secret_key" type="password" autocomplete="off"
                               placeholder="{{ $company->stripe_secret_key ? '••••••••  (set — leave blank to keep)' : 'sk_live_...' }}"
                               class="form-control @error('stripe_secret_key') is-invalid @enderror">
                        @error('stripe_secret_key')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-md-4">
                        <label for="stripe_webhook_secret" class="form-label fw-semibold">Webhook Signing Secret</label>
                        <input id="stripe_webhook_secret" name="stripe_webhook_secret" type="password" autocomplete="off"
                               placeholder="{{ $company->stripe_webhook_secret ? '••••••••  (set — leave blank to keep)' : 'whsec_...' }}"
                               class="form-control @error('stripe_webhook_secret') is-invalid @enderror">
                        <div class="form-text">From your Stripe Dashboard's webhook endpoint for <code>{{ url('/webhooks/stripe/'.$company->id) }}</code>.</div>
                        @error('stripe_webhook_secret')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                </div>
            </div>
        </div>

        <div class="card mb-4">
            <div class="card-body">
                <h5 class="fw-bold mb-1">Outbound Webhooks</h5>
                <p class="text-muted small mb-4">
                    Set a URL and every booking status change (assigned, arrived, completed, cancelled — anything)
                    gets POSTed there as JSON, so a corporate account's own systems can react without polling.
                </p>
                <div class="row g-3">
                    <div class="col-md-6">
                        <label for="webhook_url" class="form-label fw-semibold">Webhook URL</label>
                        <input id="webhook_url" name="webhook_url" type="url"
                               value="{{ old('webhook_url', $company->webhook_url) }}"
                               placeholder="https://your-system.example.com/webhooks/dispatch"
                               class="form-control @error('webhook_url') is-invalid @enderror">
                        @error('webhook_url')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-md-6">
                        <label class="form-label fw-semibold">Signing Secret</label>
                        @if ($company->webhook_secret)
                            <input type="text" readonly class="form-control" value="{{ $company->webhook_secret }}" onclick="this.select()">
                            <div class="form-check mt-2">
                                <input class="form-check-input" type="checkbox" name="regenerate_webhook_secret" value="1" id="regenerate_webhook_secret">
                                <label class="form-check-label small" for="regenerate_webhook_secret">Regenerate on save (invalidates the current secret)</label>
                            </div>
                        @else
                            <input type="text" readonly class="form-control text-muted" value="Generated automatically once a URL is saved">
                        @endif
                        <div class="form-text">
                            Every request carries an <code>X-Webhook-Signature</code> header — HMAC-SHA256 of the raw
                            JSON body, using this secret — so your endpoint can verify it actually came from here.
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <button type="submit" class="btn btn-dark">Save Settings</button>
    </form>
</x-admin-layout>
