@extends('layouts.user', ['heading' => 'Profile Settings', 'subheading' => 'Update account, security, and preferences'])

@section('content')
    @php
        $themePreference = auth()->user()?->theme_preference;
        if (! in_array($themePreference, ['light', 'dark', 'auto'], true)) {
            $themePreference = auth()->user()?->dark_mode_enabled ? 'dark' : 'light';
        }
    @endphp
    <div class="grid grid-cols-1 gap-4 xl:grid-cols-3">
        <div class="user-card p-4 xl:col-span-2">
            <div class="mb-3 text-sm font-semibold">Profile Information</div>
            <form method="POST" action="{{ route('user.profile.update') }}" class="grid grid-cols-1 gap-3 md:grid-cols-2">
                @csrf
                <input name="name" value="{{ old('name', $member->name) }}" placeholder="Full Name" class="rounded border border-[var(--user-border)] p-2" required>
                <input name="mobile" value="{{ old('mobile', $member->mobile) }}" placeholder="Mobile Number" class="rounded border border-[var(--user-border)] p-2" required>
                <input name="email" value="{{ old('email', $member->email) }}" placeholder="Email Address" class="rounded border border-[var(--user-border)] p-2 md:col-span-2">
                <input name="city" value="{{ old('city', $member->city) }}" placeholder="City" class="rounded border border-[var(--user-border)] p-2">
                <input name="state" value="{{ old('state', $member->state) }}" placeholder="State" class="rounded border border-[var(--user-border)] p-2">
                <input name="pincode" value="{{ old('pincode', $member->pincode) }}" placeholder="Pincode" class="rounded border border-[var(--user-border)] p-2">
                <textarea name="address" rows="3" placeholder="Address" class="rounded border border-[var(--user-border)] p-2 md:col-span-2">{{ old('address', $member->address) }}</textarea>
                <div class="md:col-span-2">
                    <button class="rounded bg-[var(--user-primary)] px-4 py-2 text-sm font-semibold text-white">Save Profile</button>
                </div>
            </form>
        </div>

        <div class="space-y-4">
            <div class="user-card p-4">
                <div class="text-sm font-semibold">KYC Status</div>
                <div class="mt-2 text-sm text-[var(--user-muted)]">
                    Status: <span class="font-semibold">{{ ucfirst($member->userKyc?->status ?? 'pending') }}</span>
                </div>
                <a href="{{ route('user.kyc') }}" class="mt-3 inline-block rounded border border-[var(--user-border)] px-3 py-2 text-xs">Open KYC Page</a>
            </div>

            <div class="user-card p-4">
                <div class="text-sm font-semibold">Security</div>
                <form method="POST" action="{{ route('user.profile.security') }}" class="mt-3 space-y-2">
                    @csrf
                    <input type="password" name="current_password" placeholder="Current Password" class="w-full rounded border border-[var(--user-border)] p-2 text-sm">
                    <input type="password" name="new_password" placeholder="New Password" class="w-full rounded border border-[var(--user-border)] p-2 text-sm">
                    <input type="password" name="new_password_confirmation" placeholder="Confirm New Password" class="w-full rounded border border-[var(--user-border)] p-2 text-sm">
                    <label class="flex items-center gap-2 text-xs text-[var(--user-muted)]">
                        <input type="checkbox" name="login_otp_enabled" value="1" {{ $member->login_otp_enabled ? 'checked' : '' }}>
                        Enable login OTP verification
                    </label>
                    <button class="rounded bg-[var(--user-primary)] px-3 py-2 text-xs font-semibold text-white">Update Security</button>
                </form>
            </div>

            <div class="user-card p-4">
                <div class="text-sm font-semibold">Appearance</div>
                <div class="mt-3">
                    <label class="text-xs text-[var(--user-muted)]">Theme</label>
                    <select data-theme-select class="mt-1 w-full rounded border border-[var(--user-border)] p-2 text-sm">
                        <option value="light" @selected($themePreference === 'light')>Light</option>
                        <option value="dark" @selected($themePreference === 'dark')>Dark</option>
                        <option value="auto" @selected($themePreference === 'auto')>Auto (System)</option>
                    </select>
                </div>
                <div class="mt-2 text-xs text-[var(--user-muted)]">This updates your panel theme preference.</div>
            </div>
        </div>
    </div>

    <div class="mt-4 grid grid-cols-1 gap-4 xl:grid-cols-2">
        <div class="user-card p-4">
            <div class="mb-2 text-sm font-semibold">Recent Devices</div>
            <div class="overflow-x-auto">
                <table class="min-w-full text-sm">
                    <thead class="bg-slate-100 text-left">
                        <tr>
                            <th class="p-2">Device</th>
                            <th class="p-2">Platform</th>
                            <th class="p-2">IP</th>
                            <th class="p-2">Last Seen</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($member->devices as $device)
                            <tr class="border-t">
                                <td class="p-2">{{ $device->device_name ?: 'Browser' }}</td>
                                <td class="p-2">{{ $device->platform ?: '-' }}</td>
                                <td class="p-2">{{ $device->ip_address ?: '-' }}</td>
                                <td class="p-2">{{ optional($device->last_seen_at)->format('d M Y H:i') ?: '-' }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="4" class="p-3 text-center text-[var(--user-muted)]">No devices tracked.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        <div class="user-card p-4">
            <div class="mb-2 text-sm font-semibold">Recent Login Activity</div>
            <div class="overflow-x-auto">
                <table class="min-w-full text-sm">
                    <thead class="bg-slate-100 text-left">
                        <tr>
                            <th class="p-2">Action</th>
                            <th class="p-2">IP</th>
                            <th class="p-2">Time</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($member->ipLogs as $log)
                            <tr class="border-t">
                                <td class="p-2">{{ $log->action }}</td>
                                <td class="p-2">{{ $log->ip_address }}</td>
                                <td class="p-2">{{ optional($log->created_at)->format('d M Y H:i') }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="3" class="p-3 text-center text-[var(--user-muted)]">No activity found.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection
