@extends('layouts.app')
@section('title','Reminder Settings')
@section('content')
<x-ui.form-shell title="Reminder Settings" description="Configure repayment reminder timing, messaging providers, channels and message templates." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Reminders & Notifications','url'=>route('notifications.index')],['label'=>'Settings']]">
<form method="POST" action="{{ route('notification-settings.update') }}" class="mf-form">@csrf @method('PUT')
<x-ui.form-section title="Reminder schedule" description="The scheduler is retry-safe: the same installment, reminder date and channel cannot be generated twice.">
<div class="mf-form-grid">
<div class="mf-col-4"><label class="flex items-center gap-2 text-sm"><input type="checkbox" name="reminders_enabled" value="1" @checked(old('reminders_enabled',$business->reminders_enabled))> Automated reminders enabled</label></div>
<div class="mf-col-4"><label class="mf-field-label">Pre-due days</label><input class="mf-input" name="reminder_pre_due_days" value="{{ old('reminder_pre_due_days',implode(',', $business->reminder_pre_due_days ?? [3,1])) }}" placeholder="3,1"><p class="mt-1 text-xs text-muted-foreground">Comma-separated days before due date, from 1 to 30.</p>@error('reminder_pre_due_days')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
<div class="mf-col-4"><label class="mf-field-label">Continued overdue frequency</label><select class="mf-input" name="reminder_overdue_frequency">@foreach(['daily'=>'Daily','every_2_days'=>'Every 2 days','every_3_days'=>'Every 3 days','weekly'=>'Weekly'] as $value=>$label)<option value="{{ $value }}" @selected(old('reminder_overdue_frequency',$business->reminder_overdue_frequency ?? 'daily')===$value)>{{ $label }}</option>@endforeach</select></div>
<div class="mf-col-4"><label class="flex items-center gap-2 text-sm"><input type="checkbox" name="reminder_due_date_enabled" value="1" @checked(old('reminder_due_date_enabled',$business->reminder_due_date_enabled))> Send on due date</label></div>
<div class="mf-col-4"><label class="flex items-center gap-2 text-sm"><input type="checkbox" name="reminder_overdue_enabled" value="1" @checked(old('reminder_overdue_enabled',$business->reminder_overdue_enabled))> Send overdue reminders</label></div>
</div>
</x-ui.form-section>

<x-ui.form-section title="NextSMS" description="Configure the SMS gateway used by repayment reminders and future system SMS notifications.">
<div class="mb-3 flex items-center justify-between gap-3">
<div><div class="text-sm font-medium">Provider status</div><p class="mt-1 text-xs text-muted-foreground">Both the API key and registered Sender ID are required before NextSMS can send.</p></div>
<div class="flex items-center gap-2"><span class="mf-badge {{ $channelEnabled['sms'] ? 'mf-badge-success' : 'mf-badge-muted' }}">{{ $channelEnabled['sms'] ? 'Module On' : 'Module Off' }}</span><span class="mf-badge {{ $nextSmsConfigured ? 'mf-badge-success' : 'mf-badge-muted' }}">{{ $nextSmsConfigured ? 'Configured' : 'Not configured' }}</span></div>
</div>
<div class="mf-form-grid">
<div class="mf-col-7"><label class="mf-field-label">API key</label><input type="password" class="mf-input" name="nextsms_api_key" value="" autocomplete="new-password" placeholder="{{ filled($business->nextsms_api_key) ? 'Configured — leave blank to keep current key' : 'Enter NextSMS API key' }}"><p class="mf-field-help">The key is stored encrypted. Leave this field blank to keep the current key.</p>@error('nextsms_api_key')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
<div class="mf-col-5"><label class="mf-field-label">Sender ID</label><input class="mf-input" name="nextsms_sender_id" value="{{ old('nextsms_sender_id',$business->nextsms_sender_id) }}" placeholder="Example: MYFINANCE"><p class="mf-field-help">Use the Sender ID registered and approved on your NextSMS account.</p>@error('nextsms_sender_id')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
@if(filled($business->nextsms_api_key))
<div class="mf-col-12"><label class="flex items-center gap-2 text-xs text-muted-foreground"><input type="checkbox" name="clear_nextsms_api_key" value="1"> Remove the currently saved NextSMS API key</label></div>
@endif
</div>
@if(!$channelEnabled['sms'])<p class="mf-field-help mt-2">SMS Messaging is currently disabled under Administration → System Settings → Modules. Credentials are preserved, but SMS will not be generated or retried.</p>@endif
</x-ui.form-section>

<x-ui.form-section title="WhatsApp Messaging" description="Configure the HTTP API used to deliver WhatsApp reminders and future system WhatsApp messages.">
<div class="mb-3 flex items-center justify-between gap-3">
<div><div class="text-sm font-medium">Provider status</div><p class="mt-1 text-xs text-muted-foreground">The endpoint is required. A bearer token can be stored when your WhatsApp provider requires authentication.</p></div>
<div class="flex items-center gap-2"><span class="mf-badge {{ $channelEnabled['whatsapp'] ? 'mf-badge-success' : 'mf-badge-muted' }}">{{ $channelEnabled['whatsapp'] ? 'Module On' : 'Module Off' }}</span><span class="mf-badge {{ $whatsAppConfigured ? 'mf-badge-success' : 'mf-badge-muted' }}">{{ $whatsAppConfigured ? 'Configured' : 'Not configured' }}</span></div>
</div>
<div class="mf-form-grid">
<div class="mf-col-7"><label class="mf-field-label">API endpoint</label><input class="mf-input" name="whatsapp_api_endpoint" value="{{ old('whatsapp_api_endpoint',$business->whatsapp_api_endpoint) }}" placeholder="https://provider.example/api/send"><p class="mf-field-help">The system POSTs the recipient, message, subject and loan/reminder references to this endpoint.</p>@error('whatsapp_api_endpoint')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
<div class="mf-col-5"><label class="mf-field-label">API token</label><input type="password" class="mf-input" name="whatsapp_api_token" value="" autocomplete="new-password" placeholder="{{ filled($business->whatsapp_api_token) ? 'Configured — leave blank to keep current token' : 'Optional bearer token' }}"><p class="mf-field-help">Stored encrypted. Leave blank to keep the current token.</p>@error('whatsapp_api_token')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
@if(filled($business->whatsapp_api_token))
<div class="mf-col-12"><label class="flex items-center gap-2 text-xs text-muted-foreground"><input type="checkbox" name="clear_whatsapp_api_token" value="1"> Remove the currently saved WhatsApp API token</label></div>
@endif
</div>
@if(!$channelEnabled['whatsapp'])<p class="mf-field-help mt-2">WhatsApp Messaging is currently disabled under Administration → System Settings → Modules. Credentials are preserved, but WhatsApp messages will not be generated or retried.</p>@endif
</x-ui.form-section>

<x-ui.form-section title="Delivery channels" description="Only enabled modules can be selected. Only actual provider success is marked Delivered.">
<div class="grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
@php($selected=old('reminder_channels',$business->reminder_channels ?? ['in_system']))
@foreach(['in_system'=>'In-system','sms'=>'SMS','whatsapp'=>'WhatsApp','email'=>'Email'] as $value=>$label)
<label class="border p-3"><div class="flex items-center justify-between gap-2"><span class="flex items-center gap-2 text-sm">@if($channelEnabled[$value] ?? true)<input type="checkbox" name="reminder_channels[]" value="{{ $value }}" @checked(in_array($value,$selected ?? [],true))>@endif {{ $label }}</span><span class="mf-badge {{ !($channelEnabled[$value] ?? true) ? 'mf-badge-muted' : ($providerStatus[$value] ? 'mf-badge-success' : 'mf-badge-muted') }}">{{ !($channelEnabled[$value] ?? true) ? 'Module Off' : ($providerStatus[$value] ? 'Ready' : 'Not configured') }}</span></div></label>
@endforeach
</div>@error('reminder_channels')<p class="mf-field-error mt-2">{{ $message }}</p>@enderror
<p class="mt-3 text-xs text-muted-foreground">SMS uses NextSMS when its API key and Sender ID are configured. WhatsApp uses the provider endpoint/token above, with the legacy environment configuration retained as fallback. Email requires a real Laravel mail transport. In-system delivery uses the customer database notification store.</p>
</x-ui.form-section>
<x-ui.form-actions><a class="mf-btn" href="{{ route('notifications.index') }}">Back</a><button class="mf-btn mf-btn-primary">Save reminder settings</button></x-ui.form-actions>
</form>

<div class="mt-5 border-t pt-5">
<div class="mb-3"><h2 class="text-sm font-semibold">Notification templates</h2><p class="mt-1 text-xs text-muted-foreground">Available tokens: {customer_name}, {customer_number}, {loan_number}, {installment_number}, {amount_due}, {due_date}, {days_overdue}, {business_name}, {branch_name}. Templates for disabled messaging modules remain stored and reappear when the module is enabled.</p></div>
@foreach($templates as $template)
@continue(isset($channelEnabled[$template->channel]) && !$channelEnabled[$template->channel])
<form method="POST" action="{{ route('notification-templates.update',$template) }}" class="border-t py-4 first:border-t-0">@csrf @method('PUT')
<div class="grid gap-3 lg:grid-cols-12 lg:items-start">
<div class="lg:col-span-2"><div class="text-sm font-medium">{{ str($template->event_type)->replace('_',' ')->title() }}</div><div class="text-xs text-muted-foreground">{{ str($template->channel)->replace('_',' ')->title() }}</div><label class="mt-2 flex items-center gap-2 text-xs"><input type="checkbox" name="active" value="1" @checked($template->active)> Active</label></div>
<div class="lg:col-span-3"><label class="mf-field-label">Subject</label><input class="mf-input" name="subject" value="{{ old('subject',$template->subject) }}" placeholder="Used mainly for email"></div>
<div class="lg:col-span-6"><label class="mf-field-label">Message *</label><textarea class="mf-input" name="body" rows="3" required>{{ old('body',$template->body) }}</textarea></div>
<div class="lg:col-span-1 lg:pt-5"><button class="mf-btn w-full">Save</button></div>
</div>
</form>
@endforeach
</div>
</x-ui.form-shell>
@endsection
