<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 {
    Select,
    SelectContent,
    SelectItem,
    SelectTrigger,
    SelectValue,
} from "@/components/ui/select";
import AppLayout from "@/layouts/AppLayout.vue";
import { Head, router, useForm } from "@inertiajs/vue3";

const props = defineProps<{
    application: Record<string, any>;
    environment: Record<string, any>;
    attachment: Record<string, any>;
    roles: string[];
}>();

const form = useForm({
    role: props.attachment.role,
    env_prefix: props.attachment.env_prefix ?? "",
    is_primary: Boolean(props.attachment.is_primary),
    domain: props.attachment.service_slice?.config?.domain ?? "",
    path_prefix: props.attachment.service_slice?.config?.path_prefix ?? "/",
    tls_enabled: props.attachment.service_slice?.config?.tls_enabled ?? true,
    certificate_status: props.attachment.service_slice?.config?.certificate_status ?? "",
});

const detach = (): void => {
    if (!window.confirm("Detach this managed service?")) {
        return;
    }

    router.delete(
        route("environment-attachments.destroy", {
            organisation: props.application.organisation_id,
            application: props.application.id,
            environment: props.environment.id,
            attachment: props.attachment.id,
        }),
    );
};
</script>

<template>
    <Head title="Edit Attachment" />

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

            <div class="grid gap-2">
                <Label for="role">Role</Label>
                <Select v-model="form.role">
                    <SelectTrigger id="role">
                        <SelectValue placeholder="Select a role" />
                    </SelectTrigger>
                    <SelectContent>
                        <SelectItem v-for="role in roles" :key="role" :value="role">
                            {{ role.replace("_", " ") }}
                        </SelectItem>
                    </SelectContent>
                </Select>
                <InputError :message="form.errors.role" />
            </div>

            <div class="grid gap-2">
                <Label for="env_prefix">Env prefix</Label>
                <Input id="env_prefix" v-model="form.env_prefix" placeholder="READONLY" />
                <InputError :message="form.errors.env_prefix" />
            </div>

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

            <div v-if="form.role === 'gateway'" class="grid gap-4 rounded-md border p-3">
                <div class="grid gap-2">
                    <Label for="domain">Domain</Label>
                    <Input id="domain" v-model="form.domain" placeholder="app.example.com" />
                    <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="/" />
                    <InputError :message="form.errors.path_prefix" />
                </div>
                <label class="flex items-center gap-2 text-sm">
                    <input v-model="form.tls_enabled" type="checkbox" class="size-4" />
                    TLS enabled
                </label>
                <InputError :message="form.errors.tls_enabled" />
                <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>

            <div class="flex flex-wrap justify-between gap-2">
                <Button type="button" variant="destructive" @click="detach">Detach</Button>
                <Button type="submit" :disabled="form.processing">Save attachment</Button>
            </div>
        </form>
    </AppLayout>
</template>
