<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, useForm } from "@inertiajs/vue3";

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

const form = useForm({
    service_id: String(props.services[0]?.id ?? ""),
    name: "",
    domain: "",
    path_prefix: "/",
    tls_enabled: true,
});
</script>

<template>
    <Head title="Add Gateway Route" />

    <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: 'Add' },
        ]"
    >
        <form
            class="flex h-full max-w-2xl flex-1 flex-col gap-5 p-4"
            @submit.prevent="
                form.post(
                    route('gateway.routes.store', {
                        organisation: $page.props.organisation.id,
                        application: application.id,
                        environment: environment.id,
                    }),
                )
            "
        >
            <div>
                <h2 class="text-3xl font-bold tracking-tight">Add Gateway Route</h2>
                <p class="mt-1 text-sm text-muted-foreground">
                    Create a Caddy route slice for a domain and path prefix.
                </p>
            </div>

            <div class="grid gap-2">
                <Label for="service_id">Gateway service</Label>
                <Select v-model="form.service_id" required>
                    <SelectTrigger id="service_id">
                        <SelectValue placeholder="Select gateway service" />
                    </SelectTrigger>
                    <SelectContent>
                        <SelectItem
                            v-for="service in services"
                            :key="service.id"
                            :value="String(service.id)"
                        >
                            {{ service.name }}
                        </SelectItem>
                    </SelectContent>
                </Select>
                <InputError :message="form.errors.service_id" />
            </div>

            <div class="grid gap-2">
                <Label for="name">Route slice name</Label>
                <Input id="name" v-model="form.name" placeholder="billing_web" required />
                <InputError :message="form.errors.name" />
            </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="flex justify-end">
                <Button type="submit" :disabled="form.processing || services.length === 0">
                    Create route
                </Button>
            </div>
        </form>
    </AppLayout>
</template>
