<template>
    <AppDrawer v-model="isOpen" :title="isEditing ? $t('tasks.editTask') : $t('tasks.addTask')">
        <form @submit.prevent="handleSubmit" class="flex flex-col gap-2">
            <MalioInputText
                v-model="form.title"
                label="Titre"
                input-class="w-full"
                :error="touched.title && !form.title.trim() ? 'Le titre est requis' : ''"
                @blur="touched.title = true"
            />
            <MalioInputTextArea
                v-model="form.description"
                label="Description"
                :size="3"
            />
            <MalioSelect
                v-model="form.statusId"
                :options="statusOptions"
                label="Statut"
                empty-option-label="Aucun statut"
                min-width="w-full"
            />
            <MalioSelect
                v-model="form.effortId"
                :options="effortOptions"
                label="Effort"
                empty-option-label="Aucun effort"
                min-width="w-full"
            />
            <MalioSelect
                v-model="form.priorityId"
                :options="priorityOptions"
                label="Priorité"
                empty-option-label="Aucune priorité"
                min-width="w-full"
            />
            <MalioSelect
                v-model="form.assigneeId"
                :options="userOptions"
                label="User"
                empty-option-label="Aucun utilisateur"
                min-width="w-full"
            />
            <MalioSelect
                v-model="form.groupId"
                :options="groupOptions"
                label="Groupe"
                empty-option-label="Aucun groupe"
                min-width="w-full"
            />

            <div class="mt-4">
                <p class="mb-2 text-sm font-medium text-neutral-700">Tags</p>
                <div class="flex flex-wrap gap-2">
                    <label
                        v-for="tag in tags"
                        :key="tag.id"
                        class="cursor-pointer rounded-full px-3 py-1 text-xs font-semibold transition"
                        :class="form.tagIds.includes(tag.id)
                            ? 'text-white'
                            : 'bg-neutral-100 text-neutral-600 hover:bg-neutral-200'"
                        :style="form.tagIds.includes(tag.id) ? { backgroundColor: tag.color } : {}"
                    >
                        <input
                            type="checkbox"
                            class="hidden"
                            :value="tag.id"
                            :checked="form.tagIds.includes(tag.id)"
                            @change="toggleTag(tag.id)"
                        />
                        {{ tag.label }}
                    </label>
                </div>
            </div>

            <div class="mt-6 flex items-center" :class="isEditing ? 'justify-between' : 'justify-end'">
                <button
                    v-if="isEditing"
                    type="button"
                    class="rounded-md bg-red-500 px-4 py-2 text-sm font-semibold text-white hover:bg-red-600 disabled:cursor-not-allowed disabled:opacity-50"
                    :disabled="isSubmitting"
                    @click="confirmDeleteOpen = true"
                >
                    Supprimer
                </button>
                <div class="flex gap-2">
                    <button
                        v-if="canArchive"
                        type="button"
                        class="rounded-md bg-neutral-500 px-4 py-2 text-sm font-semibold text-white hover:bg-neutral-600 disabled:cursor-not-allowed disabled:opacity-50"
                        :disabled="isSubmitting"
                        @click="handleArchive"
                    >
                        {{ $t('archive.archiveButton') }}
                    </button>
                    <button
                        v-if="canUnarchive"
                        type="button"
                        class="rounded-md bg-neutral-500 px-4 py-2 text-sm font-semibold text-white hover:bg-neutral-600 disabled:cursor-not-allowed disabled:opacity-50"
                        :disabled="isSubmitting"
                        @click="handleUnarchive"
                    >
                        {{ $t('archive.unarchiveButton') }}
                    </button>
                    <button
                        type="submit"
                        class="rounded-md bg-primary-500 px-6 py-2 text-sm font-semibold text-white hover:bg-secondary-500 disabled:cursor-not-allowed disabled:opacity-50"
                        :disabled="isSubmitting"
                    >
                        Enregistrer
                    </button>
                </div>
            </div>
        </form>

        <ConfirmDeleteTaskModal
            v-model="confirmDeleteOpen"
            @confirm="handleDelete"
        />
    </AppDrawer>
</template>

<script setup lang="ts">
import type { Task, TaskWrite } from '~/services/dto/task'
import type { TaskStatus } from '~/services/dto/task-status'
import type { TaskEffort } from '~/services/dto/task-effort'
import type { TaskPriority } from '~/services/dto/task-priority'
import type { TaskTag } from '~/services/dto/task-tag'
import type { TaskGroup } from '~/services/dto/task-group'
import type { UserData } from '~/services/dto/user-data'
import { useTaskService } from '~/services/tasks'

const props = defineProps<{
    modelValue: boolean
    task: Task | null
    projectId: number
    statuses: TaskStatus[]
    efforts: TaskEffort[]
    priorities: TaskPriority[]
    tags: TaskTag[]
    groups: TaskGroup[]
    users: UserData[]
}>()

const emit = defineEmits<{
    (e: 'update:modelValue', value: boolean): void
    (e: 'saved'): void
}>()

const isOpen = computed({
    get: () => props.modelValue,
    set: (v) => emit('update:modelValue', v),
})

const isEditing = computed(() => !!props.task)
const isSubmitting = ref(false)
const confirmDeleteOpen = ref(false)

const form = reactive({
    title: '',
    description: '',
    statusId: null as number | null,
    effortId: null as number | null,
    priorityId: null as number | null,
    assigneeId: null as number | null,
    groupId: null as number | null,
    tagIds: [] as number[],
})

const touched = reactive({
    title: false,
})

const statusOptions = computed(() =>
    props.statuses.map(s => ({ label: s.label, value: s.id }))
)

const effortOptions = computed(() =>
    props.efforts.map(e => ({ label: e.label, value: e.id }))
)

const priorityOptions = computed(() =>
    props.priorities.map(p => ({ label: p.label, value: p.id }))
)

const userOptions = computed(() =>
    props.users.map(u => ({ label: u.username, value: u.id }))
)

const groupOptions = computed(() =>
    props.groups.map(g => ({ label: g.title, value: g.id }))
)

const canArchive = computed(() => {
    if (!isEditing.value || !props.task) return false
    if (props.task.archived) return false
    const status = props.statuses.find(s => s.id === props.task?.status?.id)
    return !!status?.isFinal
})

const canUnarchive = computed(() => {
    return isEditing.value && !!props.task?.archived
})

function toggleTag(id: number) {
    const idx = form.tagIds.indexOf(id)
    if (idx >= 0) {
        form.tagIds.splice(idx, 1)
    } else {
        form.tagIds.push(id)
    }
}

function populateForm(task: Task | null) {
    if (task) {
        form.title = task.title ?? ''
        form.description = task.description ?? ''
        form.statusId = task.status?.id ?? null
        form.effortId = task.effort?.id ?? null
        form.priorityId = task.priority?.id ?? null
        form.assigneeId = task.assignee?.id ?? null
        form.groupId = task.group?.id ?? null
        form.tagIds = task.tags.map(t => t.id)
    } else {
        form.title = ''
        form.description = ''
        form.statusId = null
        form.effortId = null
        form.priorityId = null
        form.assigneeId = null
        form.groupId = null
        form.tagIds = []
    }
    touched.title = false
}

watch(() => props.modelValue, (open) => {
    if (open) {
        populateForm(props.task)
    }
})

watch(() => props.task, (task) => {
    if (props.modelValue) {
        populateForm(task)
    }
})

const { create, update, remove } = useTaskService()

async function handleDelete() {
    if (!props.task) return
    isSubmitting.value = true
    try {
        await remove(props.task.id)
        confirmDeleteOpen.value = false
        emit('saved')
        isOpen.value = false
    } finally {
        isSubmitting.value = false
    }
}

async function handleArchive() {
    if (!props.task) return
    const timerStore = useTimerStore()
    if (timerStore.activeEntry?.task) {
        const taskIri = typeof timerStore.activeEntry.task === 'string'
            ? timerStore.activeEntry.task
            : (timerStore.activeEntry.task as Task)?.['@id'] ?? `/api/tasks/${(timerStore.activeEntry.task as Task)?.id}`
        if (taskIri === `/api/tasks/${props.task.id}`) {
            await timerStore.stop()
        }
    }
    isSubmitting.value = true
    try {
        await update(props.task.id, { archived: true })
        emit('saved')
        isOpen.value = false
    } finally {
        isSubmitting.value = false
    }
}

async function handleUnarchive() {
    if (!props.task) return
    isSubmitting.value = true
    try {
        await update(props.task.id, { archived: false })
        emit('saved')
        isOpen.value = false
    } finally {
        isSubmitting.value = false
    }
}

async function handleSubmit() {
    touched.title = true
    if (!form.title.trim()) return

    isSubmitting.value = true
    try {
        const payload: TaskWrite = {
            title: form.title.trim(),
            description: form.description.trim() || null,
            status: form.statusId ? `/api/task_statuses/${form.statusId}` : null,
            effort: form.effortId ? `/api/task_efforts/${form.effortId}` : null,
            priority: form.priorityId ? `/api/task_priorities/${form.priorityId}` : null,
            assignee: form.assigneeId ? `/api/users/${form.assigneeId}` : null,
            group: form.groupId ? `/api/task_groups/${form.groupId}` : null,
            project: `/api/projects/${props.projectId}`,
            tags: form.tagIds.map(id => `/api/task_tags/${id}`),
        }

        if (isEditing.value && props.task) {
            await update(props.task.id, payload)
        } else {
            await create(payload)
        }

        emit('saved')
        isOpen.value = false
    } finally {
        isSubmitting.value = false
    }
}
</script>
