<template>
    <div class="min-h-screen bg-base-100 p-2 md:p-6">
        <BreadcrumbHeader title="User Details" />

        <div class="max-w-3xl mx-auto space-y-4">
            <div class="card card-bordered bg-base-100 shadow-sm" v-if="user">
                <div class="card-body px-2 sm:px-8">
                    <div class="flex flex-col md:flex-row items-start md:items-center gap-6">
                        <div class="avatar placeholder" v-if="!user?.avatar_url">
                            <div class="glass bg-neutral text-neutral-content rounded-full w-16 sm:w-20">
                                <span class="text-2xl sm:text-3xl">{{ user?.username?.[0]?.toUpperCase() }}</span>
                            </div>
                        </div>
                        <div class="avatar" v-else>
                            <div class="w-16 sm:w-20 rounded-full ring ring-primary ring-offset-base-100 ring-offset-2">
                                <img :src="user?.avatar_url" :alt="user?.name" />
                            </div>
                        </div>

                        <div class="flex-grow">
                            <h2 class="text-xl font-semibold text-base-content">
                                {{ user?.name }}
                            </h2>
                            <p class="text-sm text-base-content/80">{{ user?.username }}</p>
                            <div class="flex items-center gap-2 mt-2">
                                <span class="badge badge-outline"
                                    :class="user.role > 0 ? 'badge-warning' : 'badge-success'">{{
                                        formatRole(user?.role) }}</span>
                            </div>
                        </div>

                        <div class="text-right sm:text-left">
                            <div class="flex items-center gap-2">
                                <span class="font-medium text-base-content/90">Status:</span>
                                <span class="badge badge-outline"
                                    :class="user.active ? 'badge-success' : 'badge-error'">
                                    {{ user.active ? 'Active' : 'Inactive' }}
                                </span>
                            </div>
                            <div class="mt-2">
                                <span class="font-medium text-base-content/90">Quota:</span>
                                <template v-if="user.unlimited_quota">
                                    <Infinity class="inline-block w-9 text-base-content/70" />
                                    <span class="text-sm text-base-content/70">({{ user.used_quota }} used)</span>
                                </template>
                                <template v-else>
                                    <span class="text-sm text-base-content/70">{{ user.used_quota }} / {{ user.quota
                                        }}</span>
                                </template>
                            </div>
                        </div>
                    </div>

                    <div class="divider mt-4 mb-0"></div>

                    <form @submit.prevent="confirmUpdateBasicInfo" class="space-y-4 mt-4">
                        <h3 class="text-base font-medium text-base-content mb-3 flex items-center gap-2">
                            <Bookmark class="h-5 w-5 text-base-content/80" /> Basic Information
                        </h3>
                        <div v-if="basicinfo_error" role="alert" class="alert shadow-lg bg-rose-100">
                            <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 shrink-0 stroke-current" fill="none"
                                viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                    d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z" />
                            </svg>
                            <div>
                                <span>{{ basicinfo_error }}</span>
                            </div>
                            <button class="btn btn-sm" @click="basicinfo_error = null">X</button>
                        </div>

                        <div class="grid grid-cols-1 md:grid-cols-2 gap-x-4 gap-y-3">
                            <div class="form-control w-full">
                                <label for="name" class="label pb-1">
                                    <span class="label-text text-sm font-medium text-base-content/80">Name</span>
                                </label>
                                <input id="name" type="text" v-model="basicinfo.name" placeholder="Full name"
                                    class="input input-bordered input-sm w-full" />
                            </div>

                            <div class="form-control w-full">
                                <label for="username" class="label pb-1">
                                    <span class="label-text text-sm font-medium text-base-content/80">
                                        Username <span class="text-red-500">*</span>
                                    </span>
                                </label>
                                <input id="username" type="text" v-model="basicinfo.username"
                                    placeholder="Select a username" class="input input-bordered input-sm w-full"
                                    required />
                            </div>

                            <div class="form-control w-full">
                                <label for="email" class="label pb-1">
                                    <span class="label-text text-sm font-medium text-base-content/80">Email
                                        Address</span>
                                </label>
                                <div class="relative">
                                    <input id="email" type="email" v-model="basicinfo.email"
                                        placeholder="email@example.com" class="input input-bordered input-sm w-full" />
                                    <button type="button" @click="toggleEmailVerify" tabindex="-1"
                                        class="absolute inset-y-0 right-0 px-3 flex items-center text-base-content/60 hover:text-base-content focus:outline-none rounded-r-md"
                                        aria-label="Toggle email verification">
                                        <BadgeCheck v-if="user.email_verified"
                                            class="w-4 h-4 bg-green-300 rounded-full" />
                                        <div v-else class="tooltip tooltip-top" data-tip="Send verification email">
                                            <Send class="w-4 h-4" />
                                        </div>
                                    </button>
                                </div>
                            </div>
                        </div>
                        <div class="card-actions justify-end pt-4">
                            <button type="submit" class="btn btn-outline btn-success btn-sm">
                                Update Information
                            </button>
                        </div>
                    </form>
                </div>
            </div>
            <div v-else class="max-w-3xl mx-auto">
                <div class="card card-bordered bg-base-100 shadow-sm">
                    <div class="card-body space-y-4 space-y-4 px-2 sm:px-8">
                        <div class="flex justify-center items-center py-10">
                            <span class="loading loading-spinner loading-lg"></span>
                        </div>
                    </div>
                </div>
            </div>

            <div class="card card-bordered bg-base-100 shadow-sm" v-if="user">
                <div class="card-body space-y-4 space-y-4 px-2 sm:px-8">
                    <h3 class="text-base font-medium text-base-content mb-3 flex items-center gap-2">
                        <Bookmark class="h-5 w-5 text-base-content/80" /> Password
                    </h3>
                    <div v-if="password_error" role="alert" class="alert shadow-lg bg-rose-100">
                        <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 shrink-0 stroke-current" fill="none"
                            viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z" />
                        </svg>
                        <div>
                            <span>{{ password_error }}</span>
                        </div>
                        <button class="btn btn-sm" @click="password_error = null">X</button>
                    </div>

                    <form @submit.prevent="updatePassword" class="space-y-3">
                        <div class="form-control">
                            <label for="old_password" class="label pb-1">
                                <span class="label-text text-sm font-medium text-base-content/80">Old Password</span>
                            </label>
                            <input id="old_password" type="password" v-model="passwordData.oldPassword"
                                placeholder="Enter current password" class="input input-bordered input-sm w-full" />
                        </div>
                        <div class="form-control">
                            <label for="new_password" class="label pb-1">
                                <span class="label-text text-sm font-medium text-base-content/80">New Password</span>
                            </label>
                            <div class="relative">
                                <input id="new_password" :type="isNewPasswordVisible ? 'text' : 'password'"
                                    v-model="passwordData.newPassword" placeholder="Enter new password"
                                    class="input input-bordered input-sm w-full pr-10" />
                                <button type="button" @click="toggleNewPasswordVisibility" tabindex="-1"
                                    class="absolute inset-y-0 right-0 px-3 flex items-center text-base-content/60 hover:text-base-content focus:outline-none rounded-r-md"
                                    aria-label="Toggle new password visibility">
                                    <EyeOff v-if="!isNewPasswordVisible" class="w-4 h-4" />
                                    <Eye v-else class="w-4 h-4" />
                                </button>
                            </div>
                        </div>
                        <div class="form-control">
                            <label for="confirm_password" class="label pb-1">
                                <span class="label-text text-sm font-medium text-base-content/80">Confirm New
                                    Password</span>
                            </label>
                            <div class="relative">
                                <input id="confirm_password" :type="isConfirmPasswordVisible ? 'text' : 'password'"
                                    v-model="passwordData.confirmPassword" placeholder="Confirm new password"
                                    class="input input-bordered input-sm w-full pr-10" />
                                <button type="button" @click="toggleConfirmPasswordVisibility" tabindex="-1"
                                    class="absolute inset-y-0 right-0 px-3 flex items-center text-base-content/60 hover:text-base-content focus:outline-none rounded-r-md"
                                    aria-label="Toggle confirm password visibility">
                                    <EyeOff v-if="!isConfirmPasswordVisible" class="w-4 h-4" />
                                    <Eye v-else class="w-4 h-4" />
                                </button>
                            </div>
                        </div>
                        <div class="card-actions justify-end pt-4">
                            <button type="submit" class="btn btn-outline btn-warning btn-sm">
                                Change Password
                            </button>
                        </div>
                    </form>
                </div>
            </div>

            <div class="card card-bordered bg-base-100 shadow-sm" v-if="user">
                <div class="card-body space-y-4 px-2 sm:px-8">
                    <h3 class="text-base font-medium text-base-content mb-3 flex items-center gap-2">
                        <Bookmark class="h-5 w-5 text-base-content/80" /> Passkeys
                    </h3>
                    
                    <p class="text-sm text-base-content/80">Manage your passkeys for secure and passwordless login.</p>
                    <div class="card card-bordered bg-base-100 shadow-sm mt-6" v-if="user">
                        <div class="card-body px-2 sm:px-8">
                            <div v-if="passkeys" class="overflow-x-auto -mx-6">
                                <table class="table table-sm w-full">
                                    <thead>
                                        <tr class="text-xs text-base-content/70 uppercase bg-base-200">
                                            <th class="px-2 py-3">Name</th>
                                            <th class="px-2 py-3">Create Time</th>
                                            <th class="px-2 py-3">SignCount</th>
                                            <th class="px-2 py-3">Remark</th>
                                            <th class="text-right px-2 py-3"></th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr v-for="passkey in passkeys" :key="passkey.id" class="hover">
                                            <td class="font-mono text-xs px-2 py-3">{{ passkey.name }}</td>
                                            <td class="font-mono text-xs px-2 py-3">{{ formatDateTime(passkey.created_at) }}</td>
                                            <td class="font-mono text-xs px-2 py-3">{{ passkey.sign_count }}</td>
                                            <td class="font-mono text-xs px-2 py-3">{{ passkey.device_type }}</td>
                                            <td class="text-right px-2 py-3">
                                                <button class="btn btn-ghost btn-xs btn-square text-error"
                                                    @click="confirmRmovePasskey(passkey)" aria-label="Revoke token">
                                                    <TrashIcon class="w-4 h-4" />
                                                </button>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>

                            <p v-else class="text-center text-base-content/70 py-4">No passkeys found</p>
                        </div>
                    </div>
                    <div class="card-actions justify-end pt-4">
                        <button class="btn btn-outline btn-primary btn-sm" @click="newpasskey" :disabled="!supportWebAuth">
                            Add Passkey
                        </button>
                    </div>
                </div>
            </div>

            <div class="card card-bordered bg-base-100 shadow-sm" v-if="user">
                <div class="card-body space-y-4 px-2 sm:px-8">
                    <h3 class="text-base font-medium text-base-content mb-3 flex items-center gap-2">
                        <Bookmark class="h-5 w-5 text-base-content/80" /> Linked Accounts (todo)
                    </h3>
                    <p class="text-sm text-base-content/80">Manage your linked social accounts for easier login.</p>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div class="border rounded-md p-4 flex items-center justify-between">
                            <div class="flex items-center gap-2">
                                <Github class="w-6 h-6 text-base-content/80" />
                                <span>GitHub</span>
                            </div>
                            <button class="btn btn-sm" :class="isGithubConnected ? 'btn-success' : 'btn-outline'">
                                {{ isGithubConnected ? 'Disconnect' : 'Connect' }}
                            </button>
                        </div>
                        <div class="border rounded-md p-4 flex items-center justify-between">
                            <div class="flex items-center gap-2">
                                <Send class="w-6 h-6 fill-current text-gray-500" />
                                <span>Telegram</span>
                            </div>
                            <button class="btn btn-sm" :class="isTelegramConnected ? 'btn-success' : 'btn-outline'">
                                {{ isTelegramConnected ? 'Disconnect' : 'Connect' }}
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, computed, onMounted, inject } from 'vue';
import { useRoute } from 'vue-router';
import { Eye, EyeOff, BadgeCheck, Send, CircleX, CircleCheckBig, TrashIcon, Bookmark, Infinity, Github, Info } from 'lucide-vue-next'; // Ensure lucide-vue-next is installed
import BreadcrumbHeader from '@/components/dashboard/BreadcrumbHeader.vue';
import { useAuthStore } from '../../stores/auth';
import { useWebAuthStore } from '../../stores/webauth';
import { formatDateTime} from '@/utils/format-date';


const route = useRoute();
const authStore = useAuthStore();
const webAuthStore = useWebAuthStore();
const { setToast } = inject('toast');

const loading = computed(() => authStore.loading);
const user = computed(() => authStore.user);

const basicinfo_error = ref(null);
const password_error = ref(null);

const basicinfo = ref({
    name: user.value?.name || '',
    username: user.value?.username || '',
    email: user.value?.email || '',
});

const passwordData = ref({
    oldPassword: '',
    newPassword: '',
    confirmPassword: '',
});

const isNewPasswordVisible = ref(false);
const isConfirmPasswordVisible = ref(false);
const isGithubConnected = ref(false); // Replace with actual status
const isTelegramConnected = ref(false); // Replace with actual status

const supportWebAuth = ref(false);

onMounted(async () => {
    await authStore.refreshProfile();
    basicinfo.value = {
        name: user.value?.name || '',
        username: user.value?.username || '',
        email: user.value?.email || '',
    };
    await webAuthStore.getPasskeys();
    if (window.PublicKeyCredential) {
        console.log('WebAuthn is supported');
    } else {
        console.log('WebAuthn is not supported');
    }
    supportWebAuth.value = !!window.PublicKeyCredential;
});

const confirmUpdateBasicInfo = () => {
    updateBasicInfo();
};

const updateBasicInfo = async () => {
    if (!basicinfo.value) return;
    try {
        console.log('updateBasicInfo', basicinfo.value);
        const res = await authStore.updateProfile(basicinfo.value);
        if (res.data?.code == 200) {
            setToast('Basic information updated successfully', 'success');
        }

        await authStore.refreshProfile(); // Refresh user data
        basicinfo_error.value = null; // Clear error
    } catch (err) {
        console.log('Error updating basic info:', err);
        basicinfo_error.value = err || '更新失败';
        setToast('Failed to update basic information', 'error');
    }
};

const updatePassword = async () => {
    if (!user.value) return;
    if (passwordData.value.newPassword !== passwordData.value.confirmPassword) {
        setToast('新密码和确认密码不匹配', 'error');
        return;
    }
    try {
        const payload = {
            password: passwordData.value.oldPassword,
            newpassword: passwordData.value.newPassword,
        };
        console.log('payload', payload)
        const res = await authStore.updatePassword(payload);
        if (res.data?.code == 200) {
            setToast('Password updated successfully', 'success');
        }
        passwordData.value.oldPassword = '';
        passwordData.value.newPassword = '';
        passwordData.value.confirmPassword = '';
        password_error.value = null; // Clear error
    } catch (err) {
        console.error('Error updating password:', err);
        password_error.value = err || '更新失败';
    }
};

// 格式化角色
const formatRole = (role) => {
    switch (true) {
        case role > 10:
            return 'Root';
        case role > 0:
            return 'Admin';
        default:
            return 'User';
    }
};


const toggleEmailVerify = () => {
    if (user.value && !user.value.email_verified) {
        // todo: Implement logic to send verification email
        setToast('todo,Sending verification email...', 'info');
        return;
    }
}

const toggleNewPasswordVisibility = () => {
    isNewPasswordVisible.value = !isNewPasswordVisible.value;
};

const toggleConfirmPasswordVisibility = () => {
    isConfirmPasswordVisible.value = !isConfirmPasswordVisible.value;
};

const toggleGithubConnection = () => {
    isGithubConnected.value = !isGithubConnected.value;
    setToast(`GitHub ${isGithubConnected.value ? 'connected' : 'disconnected'}`, 'info');
    // Implement actual connection/disconnection logic here
};

const toggleTelegramConnection = () => {
    isTelegramConnected.value = !isTelegramConnected.value;
    setToast(`Telegram ${isTelegramConnected.value ? 'connected' : 'disconnected'}`, 'info');
    // Implement actual connection/disconnection logic here
};

const newpasskey = async () => {
    try {
        let res = await webAuthStore.addPasskey();
        if (res.data?.code == 200) {
            await getPasskeys();
        }
    } catch (err) {
        console.log('err', err);

    }
};

const passkeys = computed(() => webAuthStore.passkeys);

const getPasskeys = async () => {
    try {
        await webAuthStore.getPasskeys();
    } catch (err) {
        console.log('err', err);
    }
}

const confirmRmovePasskey = async (passkey) => {
    if(confirm(`确认删除 ${passkey.name}?`)) {
        await removePasskey(passkey.id)
    }
}
const removePasskey = async (id) => {
    try {
        const res = await webAuthStore.deletePasskey(id);
        if (res.data?.code == 200) {
            setToast('Passkey removed successfully', 'success');
        }
        await getPasskeys();
    } catch (err) {
        console.log('err', err);
    }
}
</script>