<script setup lang="ts">
import { Head, Link, router } from '@inertiajs/vue3';
import { Building2 } from 'lucide-vue-next';
import Heading from '@/components/Heading.vue';
import Pagination from '@/components/Pagination.vue';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import AppLayout from '@/layouts/AppLayout.vue';

type Workspace = {
    id: number;
    name: string;
    slug: string;
    users_count: number;
    showUrl: string;
    editUrl: string;
    destroyUrl: string;
};

type PaginatedData<T> = {
    data: T[];
    from: number | null;
    to: number | null;
    total: number;
    current_page: number;
    last_page: number;
    per_page: number;
    path: string;
    first_page_url: string;
    prev_page_url: string | null;
    next_page_url: string | null;
    last_page_url: string;
};

type Props = {
    workspaces: PaginatedData<Workspace>;
    createUrl: string;
};

defineProps<Props>();

function destroy(workspace: Workspace) {
    if (
        window.confirm(
            `Are you sure you want to delete "${workspace.name}"? All users will be unassigned.`,
        )
    ) {
        router.delete(workspace.destroyUrl);
    }
}
</script>

<template>
    <AppLayout :breadcrumbs="[{ title: 'Workspaces' }]">
        <Head title="Workspaces" />

        <div class="flex flex-col space-y-6 p-4">
            <div class="flex items-center justify-between">
                <Heading
                    variant="small"
                    title="Workspaces"
                    description="Manage accounting firm workspaces (cabinets comptables)"
                />
                <Button as-child>
                    <Link :href="createUrl">Create workspace</Link>
                </Button>
            </div>

            <div
                class="overflow-hidden rounded-xl border border-sidebar-border/70 dark:border-sidebar-border"
            >
                <div class="overflow-x-auto">
                    <table class="w-full text-sm">
                        <thead
                            class="border-b border-sidebar-border/70 bg-muted/50"
                        >
                            <tr>
                                <th
                                    class="h-10 px-4 text-left align-middle font-medium"
                                >
                                    Name
                                </th>
                                <th
                                    class="h-10 px-4 text-left align-middle font-medium"
                                >
                                    Slug
                                </th>
                                <th
                                    class="h-10 px-4 text-left align-middle font-medium"
                                >
                                    Users
                                </th>
                                <th
                                    class="h-10 px-4 text-right align-middle font-medium"
                                >
                                    Actions
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr
                                v-for="workspace in workspaces.data"
                                :key="workspace.id"
                                class="border-b border-sidebar-border/50 last:border-0"
                            >
                                <td class="px-4 py-3 font-medium">
                                    <Link
                                        :href="workspace.showUrl"
                                        class="hover:underline"
                                    >
                                        {{ workspace.name }}
                                    </Link>
                                </td>
                                <td class="px-4 py-3 text-muted-foreground">
                                    {{ workspace.slug }}
                                </td>
                                <td class="px-4 py-3">
                                    <Badge variant="secondary">
                                        {{ workspace.users_count }} user{{
                                            workspace.users_count !== 1
                                                ? 's'
                                                : ''
                                        }}
                                    </Badge>
                                </td>
                                <td class="space-x-2 px-4 py-3 text-right">
                                    <Button
                                        variant="outline"
                                        size="sm"
                                        as-child
                                    >
                                        <Link :href="workspace.showUrl"
                                            >View</Link
                                        >
                                    </Button>
                                    <Button
                                        variant="outline"
                                        size="sm"
                                        as-child
                                    >
                                        <Link :href="workspace.editUrl"
                                            >Edit</Link
                                        >
                                    </Button>
                                    <Button
                                        variant="destructive"
                                        size="sm"
                                        @click="destroy(workspace)"
                                    >
                                        Delete
                                    </Button>
                                </td>
                            </tr>
                            <tr v-if="!workspaces.data.length">
                                <td
                                    colspan="4"
                                    class="px-4 py-8 text-center text-muted-foreground"
                                >
                                    <div
                                        class="flex flex-col items-center gap-2"
                                    >
                                        <Building2 class="h-10 w-10" />
                                        <p>No workspaces yet.</p>
                                        <Button as-child>
                                            <Link :href="createUrl"
                                                >Create your first
                                                workspace</Link
                                            >
                                        </Button>
                                    </div>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
            <Pagination
                :pagination="{
                    from: workspaces.from ?? 0,
                    to: workspaces.to ?? 0,
                    total: workspaces.total,
                    current_page: workspaces.current_page,
                    last_page: workspaces.last_page,
                    per_page: workspaces.per_page,
                }"
            />
        </div>
    </AppLayout>
</template>
