@extends('layouts.dashboard')

@section('title', 'Edit User - ' . $user->name)

@section('content')
<div class="d-flex justify-content-between align-items-center mb-4">
    <h4 class="mb-0">Edit User: {{ $user->name }}</h4>
    <a href="{{ route('dashboard.user-details', $user->id) }}" class="btn btn-secondary">
        <i class="bi bi-arrow-left"></i> Back to User Details
    </a>
</div>

<div class="row">
    <div class="col-lg-8">
        <div class="card">
            <div class="card-body">
                <form action="{{ route('dashboard.users.update', $user->id) }}" method="POST">
                    @csrf
                    @method('PUT')

                    @if($errors->any())
                    <div class="alert alert-danger">
                        <ul class="mb-0">
                            @foreach($errors->all() as $error)
                            <li>{{ $error }}</li>
                            @endforeach
                        </ul>
                    </div>
                    @endif

                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="name" class="form-label">Full Name <span class="text-danger">*</span></label>
                            <input type="text" class="form-control @error('name') is-invalid @enderror"
                                id="name" name="name" value="{{ old('name', $user->name) }}" required>
                            @error('name')
                            <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-md-6 mb-3">
                            <label for="phone" class="form-label">Phone Number <span class="text-danger">*</span></label>
                            <input type="text" class="form-control @error('phone') is-invalid @enderror"
                                id="phone" name="phone" value="{{ old('phone', $user->phone) }}" required>
                            @error('phone')
                            <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="email" class="form-label">Email (Optional)</label>
                            <input type="email" class="form-control @error('email') is-invalid @enderror"
                                id="email" name="email" value="{{ old('email', $user->email) }}">
                            @error('email')
                            <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="col-md-6 mb-3">
                            <label for="password" class="form-label">New Password (Leave blank to keep current)</label>
                            <input type="password" class="form-control @error('password') is-invalid @enderror"
                                id="password" name="password">
                            @error('password')
                            <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                    </div>

                    @if($isSuperAdmin)
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="client_id" class="form-label">Organization</label>
                            <select class="form-select @error('client_id') is-invalid @enderror" id="client_id" name="client_id">
                                <option value="">No Organization</option>
                                @foreach($clients as $client)
                                <option value="{{ $client->id }}" {{ old('client_id', $user->client_id) == $client->id ? 'selected' : '' }}>
                                    {{ $client->name }}
                                </option>
                                @endforeach
                            </select>
                            <small class="text-muted">Assign this user to an organization</small>
                            @error('client_id')
                            <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                    </div>
                    @endif

                    <hr class="my-4">
                    <h6 class="mb-3">Account Status</h6>

                    <div class="row">
                        <div class="col-md-4 mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="is_active"
                                    name="is_active" value="1" {{ old('is_active', $user->is_active) ? 'checked' : '' }}>
                                <label class="form-check-label" for="is_active">
                                    Account Active
                                </label>
                            </div>
                        </div>

                        <div class="col-md-4 mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="sms_service_enabled"
                                    name="sms_service_enabled" value="1" {{ old('sms_service_enabled', $user->sms_service_enabled) ? 'checked' : '' }}>
                                <label class="form-check-label" for="sms_service_enabled">
                                    SMS Service Enabled
                                </label>
                            </div>
                        </div>

                        <div class="col-md-4 mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="promotions_enabled"
                                    name="promotions_enabled" value="1" {{ old('promotions_enabled', $user->promotions_enabled) ? 'checked' : '' }}>
                                <label class="form-check-label" for="promotions_enabled">
                                    Promotions Enabled
                                </label>
                            </div>
                        </div>
                    </div>

                    <hr class="my-4">
                    <h6 class="mb-3">Auto-Reply Settings</h6>

                    @php $settings = $user->settings; @endphp

                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="cooldown_hours" class="form-label">Cooldown Period (Hours)</label>
                            <input type="number" class="form-control" id="cooldown_hours" name="cooldown_hours"
                                value="{{ old('cooldown_hours', $settings->cooldown_hours ?? 24) }}" min="1" max="168">
                            <small class="text-muted">Time to wait before sending another SMS to the same number</small>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-4 mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="exclude_contacts"
                                    name="exclude_contacts" value="1" {{ old('exclude_contacts', $settings->exclude_contacts ?? false) ? 'checked' : '' }}>
                                <label class="form-check-label" for="exclude_contacts">
                                    <i class="bi bi-person-x"></i> Only Send to Unknown Numbers
                                </label>
                            </div>
                            <small class="text-muted d-block mt-1">When enabled, auto-replies and promotions will only be sent to phone numbers not saved in device contacts</small>
                        </div>
                    </div>

                    <hr class="my-4">
                    <h6 class="mb-3">Auto-Reply Settings</h6>

                    <div class="row">
                        <div class="col-md-4 mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="reply_to_incoming"
                                    name="reply_to_incoming" value="1" {{ old('reply_to_incoming', $settings->reply_to_incoming ?? true) ? 'checked' : '' }}>
                                <label class="form-check-label" for="reply_to_incoming">
                                    Reply to Incoming Calls
                                </label>
                            </div>
                        </div>

                        <div class="col-md-4 mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="reply_to_missed"
                                    name="reply_to_missed" value="1" {{ old('reply_to_missed', $settings->reply_to_missed ?? true) ? 'checked' : '' }}>
                                <label class="form-check-label" for="reply_to_missed">
                                    Reply to Missed Calls
                                </label>
                            </div>
                        </div>

                        <div class="col-md-4 mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="reply_to_outgoing"
                                    name="reply_to_outgoing" value="1" {{ old('reply_to_outgoing', $settings->reply_to_outgoing ?? false) ? 'checked' : '' }}>
                                <label class="form-check-label" for="reply_to_outgoing">
                                    Reply to Outgoing Calls
                                </label>
                            </div>
                        </div>
                    </div>

                    <hr class="my-4">
                    <h6 class="mb-3">Promotion Triggers</h6>

                    <div class="row">
                        <div class="col-md-4 mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="promotion_on_incoming"
                                    name="promotion_on_incoming" value="1" {{ old('promotion_on_incoming', $settings->promotion_on_incoming ?? false) ? 'checked' : '' }}>
                                <label class="form-check-label" for="promotion_on_incoming">
                                    Promotion on Incoming
                                </label>
                            </div>
                        </div>

                        <div class="col-md-4 mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="promotion_on_missed"
                                    name="promotion_on_missed" value="1" {{ old('promotion_on_missed', $settings->promotion_on_missed ?? false) ? 'checked' : '' }}>
                                <label class="form-check-label" for="promotion_on_missed">
                                    Promotion on Missed
                                </label>
                            </div>
                        </div>

                        <div class="col-md-4 mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="promotion_on_outgoing"
                                    name="promotion_on_outgoing" value="1" {{ old('promotion_on_outgoing', $settings->promotion_on_outgoing ?? false) ? 'checked' : '' }}>
                                <label class="form-check-label" for="promotion_on_outgoing">
                                    Promotion on Outgoing
                                </label>
                            </div>
                        </div>
                    </div>

                    <hr class="my-4">
                    <h6 class="mb-3"><i class="bi bi-whatsapp text-success"></i> WhatsApp Settings</h6>

                    <div class="row">
                        <div class="col-md-4 mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="whatsapp_enabled"
                                    name="whatsapp_enabled" value="1" {{ old('whatsapp_enabled', $settings->whatsapp_enabled ?? false) ? 'checked' : '' }}>
                                <label class="form-check-label" for="whatsapp_enabled">
                                    WhatsApp Enabled
                                </label>
                            </div>
                        </div>

                        <div class="col-md-4 mb-3">
                            <label for="preferred_channel" class="form-label">Preferred Channel</label>
                            <select class="form-select" id="preferred_channel" name="preferred_channel">
                                <option value="sms" {{ old('preferred_channel', $settings->preferred_channel ?? 'sms') == 'sms' ? 'selected' : '' }}>SMS Only</option>
                                <option value="whatsapp" {{ old('preferred_channel', $settings->preferred_channel ?? 'sms') == 'whatsapp' ? 'selected' : '' }}>WhatsApp Only</option>
                                <option value="both" {{ old('preferred_channel', $settings->preferred_channel ?? 'sms') == 'both' ? 'selected' : '' }}>Both (SMS + WhatsApp)</option>
                            </select>
                            <small class="text-muted">Which channel to use for auto-replies</small>
                        </div>

                        <div class="col-md-4 mb-3">
                            <label for="whatsapp_mode" class="form-label">WhatsApp Mode</label>
                            <select class="form-select" id="whatsapp_mode" name="whatsapp_mode">
                                <option value="manual" {{ old('whatsapp_mode', $settings->whatsapp_mode ?? 'manual') == 'manual' ? 'selected' : '' }}>Manual (Opens WhatsApp)</option>
                                <option value="full_auto" {{ old('whatsapp_mode', $settings->whatsapp_mode ?? 'manual') == 'full_auto' ? 'selected' : '' }}>Full Auto (Requires Accessibility)</option>
                            </select>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-4 mb-3">
                            <label for="whatsapp_cooldown_hours" class="form-label">WhatsApp Cooldown (Hours)</label>
                            <input type="number" class="form-control" id="whatsapp_cooldown_hours" name="whatsapp_cooldown_hours"
                                value="{{ old('whatsapp_cooldown_hours', $settings->whatsapp_cooldown_hours ?? 24) }}" min="1" max="168">
                        </div>

                        <div class="col-md-4 mb-3">
                            <div class="form-check form-switch mt-4">
                                <input class="form-check-input" type="checkbox" id="whatsapp_reply_to_incoming"
                                    name="whatsapp_reply_to_incoming" value="1" {{ old('whatsapp_reply_to_incoming', $settings->whatsapp_reply_to_incoming ?? false) ? 'checked' : '' }}>
                                <label class="form-check-label" for="whatsapp_reply_to_incoming">
                                    WA Reply to Incoming
                                </label>
                            </div>
                        </div>

                        <div class="col-md-4 mb-3">
                            <div class="form-check form-switch mt-4">
                                <input class="form-check-input" type="checkbox" id="whatsapp_reply_to_missed"
                                    name="whatsapp_reply_to_missed" value="1" {{ old('whatsapp_reply_to_missed', $settings->whatsapp_reply_to_missed ?? false) ? 'checked' : '' }}>
                                <label class="form-check-label" for="whatsapp_reply_to_missed">
                                    WA Reply to Missed
                                </label>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-4 mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="whatsapp_reply_to_outgoing"
                                    name="whatsapp_reply_to_outgoing" value="1" {{ old('whatsapp_reply_to_outgoing', $settings->whatsapp_reply_to_outgoing ?? false) ? 'checked' : '' }}>
                                <label class="form-check-label" for="whatsapp_reply_to_outgoing">
                                    WA Reply to Outgoing
                                </label>
                            </div>
                        </div>
                    </div>

                    <h6 class="mt-3 mb-3">WhatsApp Promotion Triggers</h6>
                    <div class="row">
                        <div class="col-md-4 mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="whatsapp_promotion_on_incoming"
                                    name="whatsapp_promotion_on_incoming" value="1" {{ old('whatsapp_promotion_on_incoming', $settings->whatsapp_promotion_on_incoming ?? false) ? 'checked' : '' }}>
                                <label class="form-check-label" for="whatsapp_promotion_on_incoming">
                                    WA Promo on Incoming
                                </label>
                            </div>
                        </div>

                        <div class="col-md-4 mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="whatsapp_promotion_on_missed"
                                    name="whatsapp_promotion_on_missed" value="1" {{ old('whatsapp_promotion_on_missed', $settings->whatsapp_promotion_on_missed ?? false) ? 'checked' : '' }}>
                                <label class="form-check-label" for="whatsapp_promotion_on_missed">
                                    WA Promo on Missed
                                </label>
                            </div>
                        </div>

                        <div class="col-md-4 mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="whatsapp_promotion_on_outgoing"
                                    name="whatsapp_promotion_on_outgoing" value="1" {{ old('whatsapp_promotion_on_outgoing', $settings->whatsapp_promotion_on_outgoing ?? false) ? 'checked' : '' }}>
                                <label class="form-check-label" for="whatsapp_promotion_on_outgoing">
                                    WA Promo on Outgoing
                                </label>
                            </div>
                        </div>
                    </div>

                    <div class="d-flex gap-2 mt-4">
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-check-lg"></i> Update User
                        </button>
                        <a href="{{ route('dashboard.user-details', $user->id) }}" class="btn btn-secondary">Cancel</a>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <div class="card mb-3">
            <div class="card-header">
                <h6 class="mb-0"><i class="bi bi-person"></i> User Info</h6>
            </div>
            <div class="card-body">
                <p class="mb-1"><strong>User ID:</strong> {{ $user->id }}</p>
                <p class="mb-1"><strong>Created:</strong> {{ $user->created_at->format('M d, Y H:i') }}</p>
                <p class="mb-1"><strong>Last Active:</strong> {{ $user->last_active_at?->diffForHumans() ?? 'Never' }}</p>
                <p class="mb-0"><strong>Session Device:</strong> {{ $user->session_device_id ? 'Active' : 'None' }}</p>
            </div>
        </div>

        <div class="card mb-3">
            <div class="card-header">
                <h6 class="mb-0"><i class="bi bi-shield-exclamation"></i> Quick Actions</h6>
            </div>
            <div class="card-body">
                <form action="{{ route('dashboard.users.force-logout', $user->id) }}" method="POST" class="mb-2">
                    @csrf
                    <button type="submit" class="btn btn-warning btn-sm w-100"
                        data-confirm="Force logout this user from their device?"
                        data-confirm-title="Force Logout"
                        data-confirm-btn="Force Logout"
                        data-confirm-class="btn-warning">
                        <i class="bi bi-box-arrow-right"></i> Force Logout
                    </button>
                </form>

                <button type="button" class="btn btn-info btn-sm w-100 mb-2" data-bs-toggle="modal" data-bs-target="#resetPasswordModal">
                    <i class="bi bi-key"></i> Reset Password
                </button>

                <form action="{{ route('dashboard.users.delete', $user->id) }}" method="POST">
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="btn btn-danger btn-sm w-100"
                        data-confirm="Are you sure you want to delete this user? This action cannot be undone."
                        data-confirm-title="Delete User"
                        data-confirm-btn="Delete User"
                        data-confirm-class="btn-danger">
                        <i class="bi bi-trash"></i> Delete User
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>

<!-- Reset Password Modal -->
<div class="modal fade" id="resetPasswordModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="{{ route('dashboard.users.reset-password', $user->id) }}" method="POST">
                @csrf
                <div class="modal-header">
                    <h5 class="modal-title">Reset Password for {{ $user->name }}</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="new_password" class="form-label">New Password</label>
                        <input type="password" class="form-control" id="new_password" name="new_password" required minlength="6">
                        <small class="text-muted">Minimum 6 characters. User will be logged out after reset.</small>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary">Reset Password</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection