<template>
    <div class="flex h-full flex-col gap-6 pb-10">
        <section class="flex flex-col gap-2">
            <h1 class="text-2xl font-bold text-neutral-900 sm:text-3xl">
                {{ t('applications.title') }}
            </h1>
            <p class="max-w-3xl text-sm leading-6 text-neutral-500 sm:text-base">
                {{ t('applications.description') }}
            </p>
        </section>

        <section class="min-h-0 rounded-3xl border border-primary-500/15 bg-white shadow-sm">
            <header class="flex flex-col gap-4 border-b border-primary-500/10 px-6 py-5 sm:flex-row sm:items-center sm:justify-between">
                <div>
                    <h2 class="text-xl font-bold text-primary-500">
                        {{ t('applications.listTitle') }}
                    </h2>
                    <p class="mt-1 text-sm text-neutral-500">
                        {{ t('applications.listDescription') }}
                    </p>
                </div>

                <button
                    class="inline-flex items-center justify-center rounded-xl border border-primary-500/20 px-4 py-2 text-sm font-semibold text-primary-500 transition-colors hover:bg-tertiary-500 disabled:cursor-not-allowed disabled:opacity-60"
                    :disabled="loading"
                    @click="loadApplications()"
                >
                    <Icon
                        name="mdi:refresh"
                        size="18"
                        class="mr-2"
                        :class="loading ? 'animate-spin' : ''"
                    />
                    {{ t('applications.actions.refresh') }}
                </button>
            </header>

            <div v-if="loading" class="grid gap-4 p-6 xl:grid-cols-2">
                <div
                    v-for="index in 4"
                    :key="index"
                    class="animate-pulse rounded-2xl border border-primary-500/10 p-5"
                >
                    <div class="h-4 w-24 rounded bg-neutral-200" />
                    <div class="mt-4 h-7 w-40 rounded bg-neutral-200" />
                    <div class="mt-3 h-4 w-full rounded bg-neutral-100" />
                    <div class="mt-6 h-11 w-44 rounded-xl bg-neutral-200" />
                </div>
            </div>

            <div v-else-if="applications.length === 0" class="px-6 py-12 text-center">
                <div class="mx-auto flex h-14 w-14 items-center justify-center rounded-full bg-tertiary-500 text-primary-500">
                    <Icon name="mdi:server-off" size="28" />
                </div>
                <h3 class="mt-4 text-lg font-bold text-primary-500">
                    {{ t('applications.emptyTitle') }}
                </h3>
                <p class="mt-2 text-sm text-neutral-500">
                    {{ t('applications.emptyDescription') }}
                </p>
            </div>

            <div v-else class="grid gap-4 p-6 xl:grid-cols-2">
                <article
                    v-for="application in applications"
                    :key="application.slug"
                    class="flex h-full min-w-0 flex-col rounded-2xl border p-5 transition-colors"
                    :class="application.maintenance ? 'border-red-200 bg-red-50/60' : 'border-emerald-200 bg-emerald-50/60'"
                >
                    <div class="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
                        <div class="min-w-0">
                            <p class="text-xs font-semibold uppercase tracking-[0.18em] text-neutral-500">
                                {{ application.slug }}
                            </p>
                            <h3 class="mt-2 break-words text-xl font-bold text-primary-500">
                                {{ application.name }}
                            </h3>
                        </div>

                        <span
                            class="inline-flex w-fit items-center rounded-full px-3 py-1 text-xs font-bold"
                            :class="application.maintenance ? 'bg-red-100 text-red-700' : 'bg-emerald-100 text-emerald-700'"
                        >
                            <span
                                class="mr-2 h-2.5 w-2.5 rounded-full"
                                :class="application.maintenance ? 'bg-red-500' : 'bg-emerald-500'"
                            />
                            {{ application.maintenance ? t('applications.status.active') : t('applications.status.inactive') }}
                        </span>
                    </div>

                    <p class="mt-4 flex-1 text-sm leading-6 text-neutral-600">
                        {{
                            application.maintenance
                                ? t('applications.card.activeDescription')
                                : t('applications.card.inactiveDescription')
                        }}
                    </p>

                    <div class="mt-6 flex flex-col gap-3 border-t border-black/5 pt-4 sm:flex-row sm:items-center sm:justify-between">
                        <p class="text-xs font-medium uppercase tracking-[0.14em] text-neutral-400">
                            {{ t('applications.card.triggerFile') }}
                        </p>

                        <button
                            class="inline-flex w-full items-center justify-center rounded-xl px-4 py-3 text-sm font-semibold text-white transition-opacity disabled:cursor-not-allowed disabled:opacity-60 sm:w-auto sm:min-w-[220px]"
                            :class="application.maintenance ? 'bg-red-600 hover:bg-red-700' : 'bg-primary-500 hover:bg-primary-600'"
                            :disabled="Boolean(pendingBySlug[application.slug])"
                            @click="toggleMaintenance(application)"
                        >
                            <Icon
                                :name="pendingBySlug[application.slug] ? 'mdi:loading' : (application.maintenance ? 'mdi:shield-check-outline' : 'mdi:alert-outline')"
                                size="18"
                                class="mr-2"
                                :class="pendingBySlug[application.slug] ? 'animate-spin' : ''"
                            />
                            {{
                                pendingBySlug[application.slug]
                                    ? t('applications.actions.pending')
                                    : application.maintenance
                                        ? t('applications.actions.deactivate')
                                        : t('applications.actions.activate')
                            }}
                        </button>
                    </div>
                </article>
            </div>
        </section>
    </div>
</template>

<script setup lang="ts">
import type { ManagedApplication } from '~/services/dto/managed-application'
import { getManagedApplications, setApplicationMaintenance } from '~/services/managed-applications'

const { t } = useI18n()

const applications = ref<ManagedApplication[]>([])
const loading = ref(true)
const pendingBySlug = ref<Record<string, boolean>>({})

async function loadApplications() {
    loading.value = true

    try {
        applications.value = await getManagedApplications()
    } finally {
        loading.value = false
    }
}

async function toggleMaintenance(application: ManagedApplication) {
    pendingBySlug.value = {
        ...pendingBySlug.value,
        [application.slug]: true
    }

    try {
        const updatedApplication = await setApplicationMaintenance(application.slug, !application.maintenance)
        applications.value = applications.value.map((item) => item.slug === updatedApplication.slug ? updatedApplication : item)
    } finally {
        pendingBySlug.value = {
            ...pendingBySlug.value,
            [application.slug]: false
        }
    }
}

onMounted(() => {
    loadApplications()
})

useHead({
    title: 'Applications'
})
</script>
