<script setup lang="ts">
import InputError from "@/components/InputError.vue";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import AppLayout from "@/layouts/AppLayout.vue";
import { Head, router, useForm } from "@inertiajs/vue3";

const props = defineProps<{
    application: Record<string, any>;
    environment: Record<string, any>;
    routeAttachment: Record<string, any>;
}>();

const form = useForm({
    domain: props.routeAttachment.service_slice?.config?.domain ?? "",
    path_prefix: props.routeAttachment.service_slice?.config?.path_prefix ?? "/",
    tls_enabled: props.routeAttachment.service_slice?.config?.tls_enabled ?? true,
    certificate_status: props.routeAttachment.service_slice?.config?.certificate_status ?? "",
});

const destroyRoute = (): void => {
    if (!window.confirm(`Remove gateway route ${form.domain}?`)) {
        return;
    }

    router.delete(
        route("gateway.routes.destroy", {
            organisation: route().params.organisation,
            application: props.application.id,
            environment: props.environment.id,
            route: props.routeAttachment.id,
        }),
    );
};
</script>

<template>
    <Head :title="`Edit ${form.domain}`" />

    <AppLayout
        :breadcrumbs="[
            {
                title: 'Applications',
                href: route('applications.index', { organisation: $page.props.organisation.id }),
            },
            {
                title: application.name,
                href: route('applications.show', {
                    organisation: $page.props.organisation.id,
                    application: application.id,
                }),
            },
            {
                title: environment.name,
                href: route('environments.show', {
                    organisation: $page.props.organisation.id,
                    application: application.id,
                    environment: environment.id,
                }),
            },
            {
                title: 'Gateway routes',
                href: route('gateway.routes.index', {
                    organisation: $page.props.organisation.id,
                    application: application.id,
                    environment: environment.id,
                }),
            },
            { title: 'Edit' },
        ]"
    >
        <form
            class="flex h-full max-w-2xl flex-1 flex-col gap-5 p-4"
            @submit.prevent="
                form.put(
                    route('gateway.routes.update', {
                        organisation: $page.props.organisation.id,
                        application: application.id,
                        environment: environment.id,
                        route: routeAttachment.id,
                    }),
                )
            "
        >
            <div>
                <h2 class="text-3xl font-bold tracking-tight">Edit Gateway Route</h2>
                <p class="mt-1 text-sm text-muted-foreground">
                    {{ routeAttachment.service?.name }} ·
                    {{ routeAttachment.service_slice?.name ?? "route slice" }}
                </p>
            </div>

            <div class="grid gap-4 md:grid-cols-2">
                <div class="grid gap-2">
                    <Label for="domain">Domain</Label>
                    <Input id="domain" v-model="form.domain" placeholder="app.example.com" required />
                    <InputError :message="form.errors.domain" />
                </div>
                <div class="grid gap-2">
                    <Label for="path_prefix">Path prefix</Label>
                    <Input id="path_prefix" v-model="form.path_prefix" placeholder="/" required />
                    <InputError :message="form.errors.path_prefix" />
                </div>
            </div>

            <label class="flex items-center gap-2 text-sm">
                <input v-model="form.tls_enabled" type="checkbox" class="size-4" />
                TLS enabled
            </label>

            <div class="grid gap-2">
                <Label for="certificate_status">Certificate status</Label>
                <Input id="certificate_status" v-model="form.certificate_status" placeholder="pending" />
                <InputError :message="form.errors.certificate_status" />
            </div>

            <div class="flex flex-wrap justify-end gap-2">
                <Button type="button" variant="ghost" @click="destroyRoute">Remove</Button>
                <Button type="submit" :disabled="form.processing">Save route</Button>
            </div>
        </form>
    </AppLayout>
</template>
