<script setup lang="ts">
import type { Application, ApplicationWrite } from '~/services/dto/application'
import { getApplications, createApplication } from '~/services/applications'

const { t } = useI18n()
const router = useRouter()

const applications = ref<Application[]>([])
const loading = ref(true)
const showCreateModal = ref(false)
const createForm = ref<ApplicationWrite>({
    name: '',
    slug: '',
    registryImage: '',
    description: '',
    giteaUrl: '',
})
const isSubmitting = ref(false)

async function loadApplications() {
    loading.value = true
    try {
        applications.value = await getApplications()
    } finally {
        loading.value = false
    }
}

function openCreateModal() {
    createForm.value = { name: '', slug: '', registryImage: '', description: '', giteaUrl: '' }
    showCreateModal.value = true
}

async function handleCreate() {
    isSubmitting.value = true
    try {
        const created = await createApplication(createForm.value)
        showCreateModal.value = false
        router.push(`/applications/${created.slug}`)
    } finally {
        isSubmitting.value = false
    }
}

function generateSlug(name: string) {
    createForm.value.slug = name.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/(^-|-$)/g, '')
}

onMounted(loadApplications)
</script>

<template>
    <div class="px-4 py-8 sm:px-8 lg:px-16">
        <div class="flex items-center justify-between pb-6">
            <h1 class="text-2xl font-bold text-primary-500 sm:text-4xl">{{ t('applications.title') }}</h1>
            <MalioButton
                :label="t('applications.addButton')"
                icon-name="mdi:plus"
                icon-position="left"
                @click="openCreateModal"
            />
        </div>

        <!-- Loading -->
        <div v-if="loading" class="grid gap-6 [grid-template-columns:repeat(auto-fill,minmax(280px,1fr))]">
            <div v-for="i in 3" :key="i" class="rounded-lg bg-tertiary-500 p-5 animate-pulse">
                <div class="h-5 bg-neutral-300 rounded w-1/2 mb-3" />
                <div class="h-4 bg-neutral-300 rounded w-3/4 mb-2" />
                <div class="h-4 bg-neutral-300 rounded w-1/3" />
            </div>
        </div>

        <!-- Empty state -->
        <div v-else-if="applications.length === 0" class="rounded-lg border border-neutral-200 bg-white p-6 text-center">
            <h3 class="text-lg font-medium text-neutral-800">{{ t('applications.emptyTitle') }}</h3>
            <p class="text-neutral-500 mt-1">{{ t('applications.emptyDescription') }}</p>
        </div>

        <!-- Application cards -->
        <div v-else class="grid gap-6 [grid-template-columns:repeat(auto-fill,minmax(280px,1fr))]">
            <NuxtLink
                v-for="app in applications"
                :key="app.slug"
                :to="`/applications/${app.slug}`"
                class="rounded-lg bg-tertiary-500 p-5 transition hover:shadow-md"
            >
                <div class="flex items-start justify-between">
                    <h3 class="text-lg font-semibold text-neutral-900">{{ app.name }}</h3>
                    <a
                        v-if="app.giteaUrl"
                        :href="app.giteaUrl"
                        target="_blank"
                        class="text-neutral-400 hover:text-primary-500"
                        @click.stop
                    >
                        <Icon name="mdi:open-in-new" size="18" />
                    </a>
                </div>
                <p v-if="app.description" class="text-neutral-500 text-sm mt-2 line-clamp-2">{{ app.description }}</p>
                <p class="text-neutral-400 text-xs mt-4 flex items-center gap-1">
                    <Icon name="mdi:server" size="14" />
                    {{ app.environments?.length ?? 0 }} {{ t('applications.card.environments', app.environments?.length ?? 0) }}
                </p>
            </NuxtLink>
        </div>

        <!-- Create modal -->
        <AppModal
            v-model="showCreateModal"
            :submit-label="t('applications.form.save')"
            :cancel-label="t('applications.form.cancel')"
            :loading="isSubmitting"
            @submit="handleCreate"
        >
            <template #title>{{ t('applications.addButton') }}</template>

            <form @submit.prevent="handleCreate" class="space-y-4">
                <div class="grid grid-cols-1 gap-x-6 gap-y-4 sm:grid-cols-2">
                    <MalioInputText
                        v-model="createForm.name"
                        :label="t('applications.form.name')"
                        @update:model-value="generateSlug"
                        required
                    />
                    <MalioInputText
                        v-model="createForm.slug"
                        :label="t('applications.form.slug')"
                        required
                    />
                    <MalioInputText
                        v-model="createForm.registryImage"
                        :label="t('applications.form.registryImage')"
                        hint="Ex : gitea.malio.fr/malio-dev/sirh"
                        required
                    />
                    <MalioInputText
                        v-model="createForm.giteaUrl"
                        :label="t('applications.form.giteaUrl')"
                        hint="Ex : https://gitea.malio.fr/malio-dev/sirh"
                    />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium text-neutral-700">{{ t('applications.form.description') }}</label>
                    <textarea
                        v-model="createForm.description"
                        class="w-full rounded-md border border-neutral-300 px-3 py-2 text-base text-neutral-900 focus:border-primary-500 focus:ring-2 focus:ring-secondary-500/20"
                        rows="3"
                    />
                </div>
            </form>
        </AppModal>
    </div>
</template>
