<script setup>
import { Head, Link, useForm, usePage } from '@inertiajs/vue3';
import { mdiFolderMultiple, mdiPlus, mdiSquareEditOutline, mdiTrashCan, mdiAlertBoxOutline } from '@mdi/js';
import { computed, ref } from 'vue';
import LayoutAuthenticated from '@/Layouts/LayoutAuthenticated.vue';
import SectionMain from '@/Components/SectionMain.vue';
import SectionTitleLineWithButton from '@/Components/SectionTitleLineWithButton.vue';
import BaseButton from '@/Components/BaseButton.vue';
import CardBox from '@/Components/CardBox.vue';
import BaseButtons from '@/Components/BaseButtons.vue';
import NotificationBar from '@/Components/NotificationBar.vue';
import CardBoxModal from '@/Components/CardBoxModal.vue';
import { stardust } from '@eidellev/adonis-stardust/client';

const isModalDangerActive = ref(false);
const deleteId = ref();

const props = defineProps({
    projects: {
        type: Array,
        default: () => [],
    },
    can: {
        type: Object,
        default: () => ({}),
    },
});

const flash = computed(() => usePage().props.flash);

const projectCount = computed(() => props.projects.length);

const formDelete = useForm({});

const destroy = (id) => {
    deleteId.value = id;
    isModalDangerActive.value = true;
};

const onConfirm = async (id) => {
    await formDelete.delete(stardust.route('settings.project.destroy', [id]));
    deleteId.value = null;
};

const onCancel = () => {
    deleteId.value = null;
};

const truncate = (text, length = 30) => {
    if (!text) return '';
    return text.length > length ? text.substring(0, length) + '...' : text;
};

const getProjectColor = (index) => {
    const colors = [
        'bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-300',
        'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300',
        'bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-300',
        'bg-orange-100 text-orange-800 dark:bg-orange-900 dark:text-orange-300',
        'bg-pink-100 text-pink-800 dark:bg-pink-900 dark:text-pink-300',
        'bg-indigo-100 text-indigo-800 dark:bg-indigo-900 dark:text-indigo-300',
    ];
    return colors[index % colors.length];
};
</script>

<template>
    <CardBoxModal
        v-model="isModalDangerActive"
        :delete-id="deleteId"
        large-title="Delete Project"
        button="danger"
        button-label="Delete"
        has-cancel
        @confirm="onConfirm"
        @cancel="onCancel"
    >
        <p>Are you sure you want to delete this project?</p>
        <p>This action cannot be undone.</p>
    </CardBoxModal>

    <LayoutAuthenticated>
        <Head title="Projects" />
        <SectionMain>
            <SectionTitleLineWithButton :icon="mdiFolderMultiple" title="Projects" main>
                <div class="flex items-center gap-3">
                    <span class="text-sm text-gray-500 dark:text-gray-400 font-medium">
                        {{ projectCount }} {{ projectCount === 1 ? 'project' : 'projects' }}
                    </span>
                    <BaseButton
                        v-if="can.create"
                        :route-name="stardust.route('settings.project.create')"
                        :icon="mdiPlus"
                        label="New Project"
                        color="info"
                        rounded-full
                        small
                        class="shadow-md hover:shadow-lg transition-shadow"
                    />
                </div>
            </SectionTitleLineWithButton>
            
            <NotificationBar v-if="flash.message" color="success" :icon="mdiAlertBoxOutline">
                {{ flash.message }}
            </NotificationBar>

            <CardBox class="mb-6" has-table>
                <table>
                    <thead>
                        <tr>
                            <th>Label</th>
                            <th>Name</th>
                            <th v-if="can.edit || can.delete">Actions</th>
                        </tr>
                    </thead>

                    <tbody>
                        <tr v-if="projects.length === 0">
                            <td colspan="3" class="text-center py-12">
                                <div class="flex flex-col items-center justify-center text-gray-500 dark:text-gray-400">
                                    <mdiFolderMultiple class="w-16 h-16 mb-4 opacity-50" />
                                    <p class="text-lg font-medium mb-2">No projects yet</p>
                                    <p class="text-sm mb-4">Get started by creating your first project</p>
                                    <BaseButton
                                        v-if="can.create"
                                        :route-name="stardust.route('settings.project.create')"
                                        :icon="mdiPlus"
                                        label="Create Project"
                                        color="info"
                                        small
                                    />
                                </div>
                            </td>
                        </tr>
                        <tr v-for="(project, index) in projects" :key="project.id" class="hover:bg-gray-50 dark:hover:bg-slate-800 transition-colors">
                            <td data-label="Label">
                                <!-- <Link
                                    :href="stardust.route('settings.project.show', [project.id])"
                                    class="no-underline hover:underline"
                                > -->
                                    <span 
                                        class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium transition-all hover:shadow-md"
                                        :class="getProjectColor(index)"
                                        :title="project.label"
                                    >
                                        {{ truncate(project.label, 25) }}
                                    </span>
                                <!-- </Link> -->
                            </td>
                            <td data-label="Name">
                                <span 
                                    class="text-gray-700 dark:text-gray-300 font-mono text-sm"
                                    :title="project.name"
                                >
                                    {{ truncate(project.name, 30) }}
                                </span>
                            </td>
                            <td v-if="can.edit || can.delete" class="before:hidden lg:w-1 whitespace-nowrap">
                                <BaseButtons type="justify-start lg:justify-end" no-wrap>
                                    <BaseButton
                                        v-if="can.edit"
                                        :route-name="stardust.route('settings.project.edit', [project.id])"
                                        color="info"
                                        :icon="mdiSquareEditOutline"
                                        small
                                    />
                                    <BaseButton
                                        v-if="can.delete"
                                        color="danger"
                                        :icon="mdiTrashCan"
                                        small
                                        @click="destroy(project.id)"
                                    />
                                </BaseButtons>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </CardBox>
        </SectionMain>
    </LayoutAuthenticated>
</template>