<script setup>
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import AppLayout from '@/layouts/AppLayout.vue';
import { Head, Link } from '@inertiajs/vue3';
import { PencilIcon } from 'lucide-vue-next';

const props = defineProps({
    server: { type: Object, required: true },
    service: { type: Object, required: true },
});
</script>

<template>
    <Head :title="service.name" />

    <AppLayout
        :breadcrumbs="[
            { title: 'Servers', href: route('servers.index', { organisation: $page.props.organisation.id }) },
            { title: server.name, href: route('servers.show', { organisation: $page.props.organisation.id, server: server.id }) },
            { title: service.name },
        ]"
    >
        <div class="flex h-full flex-1 flex-col gap-4 p-4">
            <div class="flex flex-wrap items-center justify-between gap-3">
                <div>
                    <div class="flex items-center gap-2">
                        <h2 class="text-3xl font-bold tracking-tight">{{ service.name }}</h2>
                        <Badge variant="outline">{{ service.type }}</Badge>
                    </div>
                    <p class="mt-1 text-sm text-muted-foreground">{{ service.category }} · {{ service.version }}</p>
                </div>
                <Button
                    :as="Link"
                    variant="secondary"
                    :href="route('services.edit', { organisation: $page.props.organisation.id, server: server.id, service: service.id })"
                >
                    <PencilIcon class="size-4" />
                    Edit
                </Button>
            </div>

            <div class="grid gap-4 lg:grid-cols-3">
                <Card>
                    <CardHeader>
                        <CardTitle>Replicas</CardTitle>
                        <CardDescription>Desired: {{ service.desired_replicas }}</CardDescription>
                    </CardHeader>
                    <CardContent class="grid gap-2">
                        <div v-for="replica in service.replicas" :key="replica.id" class="rounded-md border p-3 text-sm">
                            <div class="font-medium">{{ replica.container_name }}</div>
                            <div class="text-muted-foreground">{{ replica.status }} · {{ replica.health_status }}</div>
                        </div>
                    </CardContent>
                </Card>

                <Card>
                    <CardHeader>
                        <CardTitle>Slices</CardTitle>
                    </CardHeader>
                    <CardContent class="grid gap-2">
                        <div v-for="slice in service.slices" :key="slice.id" class="rounded-md border p-3 text-sm">
                            <div class="font-medium">{{ slice.name }}</div>
                            <div class="text-muted-foreground">{{ slice.type }}</div>
                        </div>
                    </CardContent>
                </Card>

                <Card>
                    <CardHeader>
                        <CardTitle>Operations</CardTitle>
                    </CardHeader>
                    <CardContent class="grid gap-2">
                        <div v-for="operation in service.operations" :key="operation.id" class="flex items-center justify-between rounded-md border p-3 text-sm">
                            <span>{{ operation.kind.replace('_', ' ') }}</span>
                            <Badge :variant="operation.status === 'completed' ? 'success' : 'secondary'">{{ operation.status.replace('_', ' ') }}</Badge>
                        </div>
                    </CardContent>
                </Card>
            </div>
        </div>
    </AppLayout>
</template>
