<template>
    <Teleport v-if="isOpen" to="body">
        <Transition name="task-modal" appear>
            <div class="fixed inset-0 z-50 flex items-center justify-center p-4">
                <!-- Backdrop -->
                <div
                    class="absolute inset-0 bg-slate-900/40 backdrop-blur-sm"
                    @click="close"
                />

                <!-- Modal -->
                <div
                    class="relative z-10 flex w-full max-w-2xl flex-col overflow-hidden rounded-2xl bg-white shadow-2xl ring-1 ring-black/5"
                    style="max-height: min(90vh, 900px)"
                >
                    <!-- Header -->
                    <div class="border-b border-neutral-100 bg-neutral-50/80 px-4 py-4 sm:px-8 sm:py-5">
                        <div class="flex items-center justify-between">
                            <div class="flex items-center gap-3">
                                <span
                                    v-if="isEditing && task?.project?.code && task?.number"
                                    class="rounded-md bg-primary-500 px-2.5 py-1 text-xs font-bold tracking-wide text-white"
                                >
                                    {{ task.project.code }}-{{ task.number }}
                                </span>
                                <h2 class="text-lg font-bold tracking-tight text-neutral-900">
                                    {{ isEditing ? $t('tasks.editTask') : $t('tasks.addTask') }}
                                </h2>
                            </div>
                            <MalioButtonIcon
                                icon="mdi:close"
                                aria-label="Fermer"
                                variant="ghost"
                                icon-size="20"
                                @click="close"
                            />
                        </div>
                    </div>

                    <!-- Body -->
                    <form @submit.prevent="handleSubmit" class="min-h-0 flex-1 overflow-y-auto px-4 py-4 sm:px-8 sm:py-6">
                        <!-- Tabs -->
                        <div class="border-b border-neutral-100 -mx-4 px-4 sm:-mx-8 sm:px-8 mb-4">
                            <nav class="flex gap-6">
                                <button
                                    v-for="tab in availableTabs"
                                    :key="tab"
                                    type="button"
                                    class="px-1 pb-3 text-sm font-semibold transition"
                                    :class="activeTab === tab
                                        ? 'border-b-2 border-primary-500 text-primary-500'
                                        : 'text-neutral-500 hover:text-neutral-700'"
                                    @click="activeTab = tab as 'details' | 'planning' | 'mails'"
                                >
                                    {{ tab === 'mails' ? $t('mail.taskTab.title') : $t(`tasks.${tab}Tab`) }}
                                </button>
                            </nav>
                        </div>

                        <div v-show="activeTab === 'details'">
                        <!-- Title -->
                        <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"
                        />

                        <!-- Project select (create mode with project list) -->
                        <div v-if="showProjectSelect" class="mt-4">
                            <MalioSelect
                                v-model="form.projectId"
                                :options="projectOptions"
                                label="Projet *"
                                empty-option-label="Sélectionner un projet"
                                group-class="w-full"
                            />
                            <p v-if="touched.project && !form.projectId" class="mt-1 text-xs text-red-500">
                                Le projet est requis
                            </p>
                        </div>

                        <!-- Two-column selects -->
                        <div class="mt-4 grid grid-cols-1 gap-x-6 gap-y-4 sm:grid-cols-2">
                            <MalioSelect
                                v-model="form.statusId"
                                :options="statusOptions"
                                label="Statut"
                                empty-option-label="Aucun statut"
                                group-class="w-full"
                            />
                            <MalioSelect
                                v-model="form.assigneeId"
                                :options="userOptions"
                                label="User"
                                empty-option-label="Aucun utilisateur"
                                group-class="w-full"
                            />
                            <MalioSelect
                                v-model="form.effortId"
                                :options="effortOptions"
                                label="Effort"
                                empty-option-label="Aucun effort"
                                group-class="w-full"
                            />
                            <MalioSelect
                                v-model="form.priorityId"
                                :options="priorityOptions"
                                label="Priorité"
                                empty-option-label="Aucune priorité"
                                group-class="w-full"
                            />
                            <MalioSelect
                                v-model="form.groupId"
                                :options="groupOptions"
                                label="Groupe"
                                empty-option-label="Aucun groupe"
                                group-class="w-full"
                            />
                        </div>

                        <!-- Tags -->
                        <div v-if="tags.length" class="mt-5">
                            <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-all"
                                    :class="form.tagIds.includes(tag.id)
                                        ? 'text-white shadow-sm'
                                        : '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>

                        <!-- Collaborators -->
                        <div v-if="collaboratorOptions.length" class="mt-5">
                            <p class="mb-2 text-sm font-medium text-neutral-700">Collaborateurs</p>
                            <div class="flex flex-wrap gap-2">
                                <label
                                    v-for="user in collaboratorOptions"
                                    :key="user.value"
                                    class="cursor-pointer rounded-full px-3 py-1 text-xs font-semibold transition-all"
                                    :class="form.collaboratorIds.includes(user.value)
                                        ? 'bg-primary-500 text-white shadow-sm'
                                        : 'bg-neutral-100 text-neutral-600 hover:bg-neutral-200'"
                                >
                                    <input
                                        type="checkbox"
                                        class="hidden"
                                        :value="user.value"
                                        :checked="form.collaboratorIds.includes(user.value)"
                                        @change="toggleCollaborator(user.value)"
                                    />
                                    {{ user.label }}
                                </label>
                            </div>
                        </div>

                        <!-- Description -->
                        <div class="mt-5">
                            <MalioInputRichText
                                v-model="form.description"
                                label="Description"
                                min-height="180px"
                            />
                        </div>

                        <!-- Documents -->
                        <TaskDocumentUpload
                            v-if="isEditing && task && isAdmin"
                            :task-id="task.id"
                            @uploaded="handleDocumentUploaded"
                        />
                        <div v-if="isEditing && task && isAdmin && shareEnabled" class="mt-2">
                            <MalioButton
                                variant="tertiary"
                                :label="$t('taskDocuments.linkShareButton')"
                                button-class="w-auto px-3"
                                @click="showShareLinker = true"
                            />
                        </div>
                        <TaskDocumentShareLinker
                            v-if="isEditing && task && isAdmin"
                            v-model="showShareLinker"
                            :task-id="task.id"
                            @linked="handleDocumentUploaded"
                        />
                        <TaskDocumentList
                            v-if="isEditing && task"
                            :documents="localDocuments"
                            :is-admin="isAdmin"
                            @preview="openPreview"
                            @delete="handleDeleteDocument"
                        />

                        <!-- Document preview modal -->
                        <TaskDocumentPreview
                            :document="previewDoc"
                            :has-prev="previewIndex > 0"
                            :has-next="previewIndex < localDocuments.length - 1"
                            @close="previewDoc = null"
                            @prev="prevPreview"
                            @next="nextPreview"
                        />

                        <!-- Git section -->
                        <TaskGitSection
                            v-if="hasGitea && isEditing && task"
                            :task="task"
                            :gitea-url="giteaUrl"
                        />

                        <!-- BookStack links -->
                        <TaskBookStackLinks
                            v-if="hasBookStack && isEditing && task"
                            :task-id="task.id"
                        />
                        </div>

                        <div v-show="activeTab === 'planning'" class="space-y-6">
                            <!-- Dates section -->
                            <div>
                                <h3 class="mb-3 text-sm font-semibold text-neutral-700">{{ $t('tasks.planning.dates') }}</h3>
                                <div class="grid grid-cols-1 gap-4 sm:grid-cols-2">
                                    <div>
                                        <label class="mb-1 block text-sm font-medium text-neutral-700">{{ $t('tasks.planning.scheduledStart') }}</label>
                                        <input
                                            v-model="form.scheduledStart"
                                            type="datetime-local"
                                            class="w-full rounded-md border border-neutral-300 px-3 py-2 text-sm focus:border-primary-500 focus:outline-none focus:ring-1 focus:ring-primary-500"
                                        />
                                    </div>
                                    <div>
                                        <label class="mb-1 block text-sm font-medium text-neutral-700">{{ $t('tasks.planning.scheduledEnd') }}</label>
                                        <input
                                            v-model="form.scheduledEnd"
                                            type="datetime-local"
                                            class="w-full rounded-md border border-neutral-300 px-3 py-2 text-sm focus:border-primary-500 focus:outline-none focus:ring-1 focus:ring-primary-500"
                                        />
                                    </div>
                                </div>
                                <div class="mt-4">
                                    <div class="sm:w-1/2">
                                        <label class="mb-1 block text-sm font-medium text-neutral-700">{{ $t('tasks.planning.deadline') }}</label>
                                        <input
                                            v-model="form.deadline"
                                            type="date"
                                            class="w-full rounded-md border border-neutral-300 px-3 py-2 text-sm focus:border-primary-500 focus:outline-none focus:ring-1 focus:ring-primary-500"
                                        />
                                    </div>
                                </div>
                            </div>

                            <!-- Calendar sync -->
                            <div class="rounded-lg border border-neutral-200 p-4">
                                <h3 class="mb-3 text-sm font-semibold text-neutral-700">{{ $t('tasks.planning.calendar') }}</h3>
                                <label class="flex items-center gap-3 cursor-pointer">
                                    <input
                                        v-model="form.syncToCalendar"
                                        type="checkbox"
                                        class="rounded border-neutral-300"
                                    />
                                    <span class="text-sm">{{ $t('tasks.planning.syncToCalendar') }}</span>
                                </label>
                                <div v-if="isEditing && task?.syncToCalendar" class="mt-3 flex items-center gap-2">
                                    <Icon
                                        :name="task.calendarSyncError ? 'mdi:alert-circle' : 'mdi:check-circle'"
                                        :class="task.calendarSyncError ? 'text-red-500' : 'text-green-500'"
                                        size="18"
                                    />
                                    <span class="text-xs" :class="task.calendarSyncError ? 'text-red-600' : 'text-green-600'">
                                        {{ task.calendarSyncError || $t('tasks.planning.syncOk') }}
                                    </span>
                                </div>
                            </div>

                            <!-- Recurrence -->
                            <div class="rounded-lg border border-neutral-200 p-4">
                                <h3 class="mb-3 text-sm font-semibold text-neutral-700">{{ $t('tasks.planning.recurrence') }}</h3>
                                <label class="flex items-center gap-3 cursor-pointer">
                                    <input
                                        v-model="form.isRecurring"
                                        type="checkbox"
                                        class="rounded border-neutral-300"
                                    />
                                    <span class="text-sm">{{ $t('tasks.planning.isRecurring') }}</span>
                                </label>

                                <div v-if="form.isRecurring" class="mt-4 space-y-4">
                                    <!-- Type -->
                                    <div>
                                        <label class="mb-1 block text-sm font-medium text-neutral-700">{{ $t('tasks.planning.type') }}</label>
                                        <select v-model="form.recurrenceType" class="w-full rounded-md border border-neutral-300 px-3 py-2 text-sm">
                                            <option value="daily">{{ $t('tasks.planning.daily') }}</option>
                                            <option value="weekly">{{ $t('tasks.planning.weekly') }}</option>
                                            <option value="monthly">{{ $t('tasks.planning.monthly') }}</option>
                                            <option value="yearly">{{ $t('tasks.planning.yearly') }}</option>
                                        </select>
                                    </div>

                                    <!-- Interval -->
                                    <MalioInputText
                                        v-model="form.recurrenceInterval"
                                        :label="$t('tasks.planning.interval')"
                                        type="number"
                                        input-class="w-full sm:w-1/3"
                                        min="1"
                                        max="100"
                                    />

                                    <!-- Weekly: days of week -->
                                    <div v-if="form.recurrenceType === 'weekly'">
                                        <p class="mb-2 text-sm font-medium text-neutral-700">{{ $t('tasks.planning.daysOfWeek') }}</p>
                                        <div class="flex flex-wrap gap-2">
                                            <label
                                                v-for="day in weekDays"
                                                :key="day.value"
                                                class="cursor-pointer rounded-full px-3 py-1 text-xs font-semibold transition-all"
                                                :class="form.recurrenceDaysOfWeek.includes(day.value)
                                                    ? 'bg-primary-500 text-white'
                                                    : 'bg-neutral-100 text-neutral-600 hover:bg-neutral-200'"
                                            >
                                                <input
                                                    type="checkbox"
                                                    class="hidden"
                                                    :value="day.value"
                                                    :checked="form.recurrenceDaysOfWeek.includes(day.value)"
                                                    @change="toggleDay(day.value)"
                                                />
                                                {{ day.label }}
                                            </label>
                                        </div>
                                    </div>

                                    <!-- Monthly options -->
                                    <div v-if="form.recurrenceType === 'monthly'" class="space-y-3">
                                        <div class="flex gap-4">
                                            <label class="flex items-center gap-2 text-sm">
                                                <input v-model="form.monthlyMode" value="dayOfMonth" type="radio" />
                                                {{ $t('tasks.planning.dayOfMonth') }}
                                            </label>
                                            <label class="flex items-center gap-2 text-sm">
                                                <input v-model="form.monthlyMode" value="weekOfMonth" type="radio" />
                                                {{ $t('tasks.planning.weekOfMonth') }}
                                            </label>
                                        </div>
                                        <MalioInputText
                                            v-if="form.monthlyMode === 'dayOfMonth'"
                                            v-model="form.recurrenceDayOfMonth"
                                            :label="$t('tasks.planning.dayOfMonthLabel')"
                                            type="number"
                                            input-class="w-full sm:w-1/3"
                                            min="1"
                                            max="31"
                                        />
                                        <div v-if="form.monthlyMode === 'weekOfMonth'" class="grid grid-cols-2 gap-4">
                                            <div>
                                                <label class="mb-1 block text-sm font-medium text-neutral-700">{{ $t('tasks.planning.weekOfMonthLabel') }}</label>
                                                <select v-model="form.recurrenceWeekOfMonth" class="w-full rounded-md border border-neutral-300 px-3 py-2 text-sm">
                                                    <option :value="1">1er</option>
                                                    <option :value="2">2ème</option>
                                                    <option :value="3">3ème</option>
                                                    <option :value="4">4ème</option>
                                                </select>
                                            </div>
                                            <div>
                                                <label class="mb-1 block text-sm font-medium text-neutral-700">{{ $t('tasks.planning.dayLabel') }}</label>
                                                <select v-model="form.recurrenceWeekDay" class="w-full rounded-md border border-neutral-300 px-3 py-2 text-sm">
                                                    <option v-for="day in weekDays" :key="day.value" :value="day.value">{{ day.label }}</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>

                                    <!-- End of recurrence -->
                                    <div class="space-y-3">
                                        <p class="text-sm font-medium text-neutral-700">{{ $t('tasks.planning.endRecurrence') }}</p>
                                        <label class="flex items-center gap-2 text-sm">
                                            <input v-model="form.recurrenceEnd" value="never" type="radio" />
                                            {{ $t('tasks.planning.neverEnds') }}
                                        </label>
                                        <div class="flex items-center gap-2">
                                            <label class="flex items-center gap-2 text-sm">
                                                <input v-model="form.recurrenceEnd" value="occurrences" type="radio" />
                                                {{ $t('tasks.planning.afterOccurrences') }}
                                            </label>
                                            <MalioInputText
                                                v-if="form.recurrenceEnd === 'occurrences'"
                                                v-model="form.recurrenceMaxOccurrences"
                                                type="number"
                                                input-class="w-20"
                                                min="1"
                                            />
                                        </div>
                                        <div class="flex items-center gap-2">
                                            <label class="flex items-center gap-2 text-sm">
                                                <input v-model="form.recurrenceEnd" value="date" type="radio" />
                                                {{ $t('tasks.planning.onDate') }}
                                            </label>
                                            <MalioInputText
                                                v-if="form.recurrenceEnd === 'date'"
                                                v-model="form.recurrenceEndDate"
                                                type="date"
                                                input-class="w-44"
                                            />
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- Onglet Mails -->
                        <div v-show="activeTab === 'mails'" class="space-y-4">
                            <!-- Chargement -->
                            <div v-if="mailsLoading" class="flex items-center justify-center py-8">
                                <Icon name="material-symbols:progress-activity" size="24" class="animate-spin text-neutral-400" />
                            </div>

                            <!-- Vide -->
                            <div
                                v-else-if="linkedMails.length === 0"
                                class="flex flex-col items-center justify-center gap-3 py-8 text-center"
                            >
                                <Icon name="material-symbols:mail-outline" size="32" class="text-neutral-300" />
                                <p class="text-sm text-neutral-400 italic">{{ $t('mail.taskTab.empty') }}</p>
                            </div>

                            <!-- Liste mails liés -->
                            <div v-else class="divide-y divide-neutral-100 rounded-lg border border-neutral-200">
                                <NuxtLink
                                    v-for="mail in linkedMails"
                                    :key="mail.id"
                                    :to="`/mail?messageId=${mail.id}`"
                                    class="flex items-start gap-3 px-4 py-3 text-sm transition-colors hover:bg-neutral-50"
                                    :title="$t('mail.taskTab.openInMailer')"
                                >
                                    <Icon
                                        name="material-symbols:mail-outline"
                                        size="16"
                                        class="mt-0.5 flex-shrink-0 text-neutral-400"
                                    />
                                    <div class="min-w-0 flex-1">
                                        <p class="truncate font-medium text-neutral-800">
                                            {{ mail.subject ?? $t('mail.noSubject') }}
                                        </p>
                                        <p class="flex items-center gap-2 text-xs text-neutral-500">
                                            <span class="truncate">{{ mail.fromName ?? mail.fromEmail }}</span>
                                            <span>·</span>
                                            <span class="flex-shrink-0">{{ formatMailDate(mail.sentAt ?? mail.receivedAt) }}</span>
                                        </p>
                                    </div>
                                    <Icon
                                        name="material-symbols:open-in-new"
                                        size="14"
                                        class="flex-shrink-0 text-neutral-300"
                                    />
                                </NuxtLink>
                            </div>

                        </div>

                    </form>

                    <!-- Footer -->
                    <div
                        class="shrink-0 flex items-center border-t border-neutral-100 bg-white px-4 py-4 sm:px-8 sm:py-5"
                        :class="isEditing ? 'justify-between' : 'justify-end'"
                    >
                        <MalioButton
                            v-if="isEditing"
                            variant="danger"
                            label="Supprimer"
                            button-class="w-auto px-4"
                            :disabled="isSubmitting"
                            @click="confirmDeleteOpen = true"
                        />
                        <div class="flex gap-3">
                            <MalioButton
                                v-if="canArchive"
                                variant="tertiary"
                                :label="$t('archive.archiveButton')"
                                button-class="w-auto px-4"
                                :disabled="isSubmitting"
                                @click="handleArchive"
                            />
                            <MalioButton
                                v-if="canUnarchive"
                                variant="tertiary"
                                :label="$t('archive.unarchiveButton')"
                                button-class="w-auto px-4"
                                :disabled="isSubmitting"
                                @click="handleUnarchive"
                            />
                            <MalioButton
                                variant="tertiary"
                                label="Annuler"
                                button-class="w-auto px-4"
                                @click="close"
                            />
                            <MalioButton
                                label="Enregistrer"
                                button-class="w-auto px-6"
                                :disabled="isSubmitting"
                                @click="handleSubmit"
                            />
                        </div>
                    </div>

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

                    <!-- Confirm delete document modal -->
                    <ConfirmDeleteDocumentModal
                        v-model="confirmDeleteDocOpen"
                        @confirm="confirmDeleteDocument"
                    />
                </div>
            </div>
        </Transition>
    </Teleport>
</template>

<script setup lang="ts">
import type { Task, TaskWrite } from '~/modules/project-management/services/dto/task'
import type { TaskDocument } from '~/modules/project-management/services/dto/task-document'
import { useGiteaService } from '~/modules/integration/services/gitea'
import { useTaskDocumentService } from '~/modules/project-management/services/task-documents'
import ConfirmDeleteDocumentModal from '~/components/ui/ConfirmDeleteDocumentModal.vue'
import type { TaskStatus } from '~/modules/project-management/services/dto/task-status'
import type { TaskEffort } from '~/modules/project-management/services/dto/task-effort'
import type { TaskPriority } from '~/modules/project-management/services/dto/task-priority'
import type { TaskTag } from '~/modules/project-management/services/dto/task-tag'
import type { TaskGroup } from '~/modules/project-management/services/dto/task-group'
import type { UserData } from '~/services/dto/user-data'
import { useTaskService } from '~/modules/project-management/services/tasks'
import { useTaskRecurrenceService } from '~/modules/project-management/services/task-recurrences'

import type { Project } from '~/modules/project-management/services/dto/project'
import { useMailService } from '~/modules/mail/services/mail'
import type { MailMessageHeaderDto } from '~/modules/mail/services/dto/mail'

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

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

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

function close() {
    if (confirmDeleteDocOpen.value || confirmDeleteOpen.value) return
    isOpen.value = false
}

const isEditing = computed(() => !!props.task)
const isSubmitting = ref(false)
const confirmDeleteOpen = ref(false)
const activeTab = ref<'details' | 'planning' | 'mails'>('details')

// ─── Onglet Mails ─────────────────────────────────────────────────────────

const mailService = useMailService()
const linkedMails = ref<MailMessageHeaderDto[]>([])
const mailsLoading = ref(false)

const giteaUrl = ref('')
const { getSettings: getGiteaSettings } = useGiteaService()

const hasGitea = computed(() => {
    return !!props.task?.project?.giteaOwner && !!props.task?.project?.giteaRepo && !!giteaUrl.value
})

const hasBookStack = computed(() => {
    return !!props.task?.project?.bookstackShelfId
})

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,
    collaboratorIds: [] as number[],
    groupId: null as number | null,
    tagIds: [] as number[],
    projectId: null as number | null,
    scheduledStart: '',
    scheduledEnd: '',
    deadline: '',
    syncToCalendar: false,
    isRecurring: false,
    recurrenceType: 'daily' as string,
    recurrenceInterval: '1',
    recurrenceDaysOfWeek: [] as string[],
    recurrenceDayOfMonth: '',
    monthlyMode: 'dayOfMonth' as string,
    recurrenceWeekOfMonth: 1,
    recurrenceWeekDay: 'monday' as string,
    recurrenceEnd: 'never' as string,
    recurrenceMaxOccurrences: '',
    recurrenceEndDate: '',
})

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

const showProjectSelect = computed(() => !!props.projects?.length && !isEditing.value)

const projectOptions = computed(() =>
    (props.projects ?? []).map(p => ({ label: p.name, value: p.id }))
)

const resolvedProjectId = computed(() =>
    showProjectSelect.value ? form.projectId : props.projectId
)

const statusOptions = computed(() => {
    const project = props.projects?.find(p => p.id === resolvedProjectId.value)
    const wfStatuses = project?.workflow?.statuses ?? props.statuses
    const opts = wfStatuses.map(s => ({ label: s.label, value: s.id }))
    const current = props.task?.status
    if (current && !wfStatuses.some(s => s.id === current.id)) {
        opts.unshift({ label: current.label, value: current.id })
    }
    return opts
})

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 collaboratorOptions = computed(() =>
    props.users
        .filter(u => u.id !== form.assigneeId)
        .map(u => ({ label: u.username, value: u.id }))
)

watch(() => form.assigneeId, (newAssigneeId) => {
    if (newAssigneeId) {
        form.collaboratorIds = form.collaboratorIds.filter(id => id !== newAssigneeId)
    }
})

const groupOptions = computed(() => {
    let filtered = props.groups.filter(g => !g.archived)
    if (showProjectSelect.value && form.projectId) {
        filtered = filtered.filter(g => g.project?.id === form.projectId)
    }
    return filtered.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 toggleCollaborator(userId: number) {
    const idx = form.collaboratorIds.indexOf(userId)
    if (idx >= 0) form.collaboratorIds.splice(idx, 1)
    else form.collaboratorIds.push(userId)
}

const weekDays = computed(() => [
    { value: 'monday', label: t('tasks.planning.days.mon') },
    { value: 'tuesday', label: t('tasks.planning.days.tue') },
    { value: 'wednesday', label: t('tasks.planning.days.wed') },
    { value: 'thursday', label: t('tasks.planning.days.thu') },
    { value: 'friday', label: t('tasks.planning.days.fri') },
    { value: 'saturday', label: t('tasks.planning.days.sat') },
    { value: 'sunday', label: t('tasks.planning.days.sun') },
])

function toggleDay(day: string) {
    const idx = form.recurrenceDaysOfWeek.indexOf(day)
    if (idx >= 0) form.recurrenceDaysOfWeek.splice(idx, 1)
    else form.recurrenceDaysOfWeek.push(day)
}

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.collaboratorIds = task.collaborators?.map(c => c.id) ?? []
        form.groupId = task.group?.id ?? null
        form.tagIds = task.tags.map(t => t.id)
        form.scheduledStart = task.scheduledStart ? task.scheduledStart.slice(0, 16) : ''
        form.scheduledEnd = task.scheduledEnd ? task.scheduledEnd.slice(0, 16) : ''
        form.deadline = task.deadline ? task.deadline.slice(0, 10) : ''
        form.syncToCalendar = task.syncToCalendar ?? false

        if (task.recurrence) {
            form.isRecurring = true
            form.recurrenceType = task.recurrence.type
            form.recurrenceInterval = String(task.recurrence.interval)
            form.recurrenceDaysOfWeek = task.recurrence.daysOfWeek ?? []
            form.recurrenceDayOfMonth = task.recurrence.dayOfMonth ? String(task.recurrence.dayOfMonth) : ''
            form.recurrenceWeekOfMonth = task.recurrence.weekOfMonth ?? 1
            form.monthlyMode = task.recurrence.weekOfMonth ? 'weekOfMonth' : 'dayOfMonth'
            form.recurrenceWeekDay = task.recurrence.daysOfWeek?.[0] ?? 'monday'
            if (task.recurrence.maxOccurrences) {
                form.recurrenceEnd = 'occurrences'
                form.recurrenceMaxOccurrences = String(task.recurrence.maxOccurrences)
            } else if (task.recurrence.endDate) {
                form.recurrenceEnd = 'date'
                form.recurrenceEndDate = task.recurrence.endDate.slice(0, 10)
            } else {
                form.recurrenceEnd = 'never'
            }
        } else {
            form.isRecurring = false
            form.recurrenceType = 'daily'
            form.recurrenceInterval = '1'
            form.recurrenceDaysOfWeek = []
            form.recurrenceDayOfMonth = ''
            form.monthlyMode = 'dayOfMonth'
            form.recurrenceWeekOfMonth = 1
            form.recurrenceWeekDay = 'monday'
            form.recurrenceEnd = 'never'
            form.recurrenceMaxOccurrences = ''
            form.recurrenceEndDate = ''
        }
    } else {
        form.title = ''
        form.description = ''
        form.statusId = null
        form.effortId = null
        form.priorityId = null
        form.assigneeId = null
        form.collaboratorIds = []
        form.groupId = null
        form.tagIds = []
        form.projectId = null
        form.scheduledStart = ''
        form.scheduledEnd = ''
        form.deadline = ''
        form.syncToCalendar = false
        form.isRecurring = false
        form.recurrenceType = 'daily'
        form.recurrenceInterval = '1'
        form.recurrenceDaysOfWeek = []
        form.recurrenceDayOfMonth = ''
        form.monthlyMode = 'dayOfMonth'
        form.recurrenceWeekOfMonth = 1
        form.recurrenceWeekDay = 'monday'
        form.recurrenceEnd = 'never'
        form.recurrenceMaxOccurrences = ''
        form.recurrenceEndDate = ''
    }
    touched.title = false
    touched.project = false
}

watch(() => props.modelValue, async (open) => {
    if (open) {
        activeTab.value = 'details'
        confirmDeleteDocOpen.value = false
        documentToDelete.value = null
        linkedMails.value = []
        populateForm(props.task)
        if (props.task?.project?.giteaOwner && props.task?.project?.giteaRepo && !giteaUrl.value) {
            try {
                const settings = await getGiteaSettings()
                giteaUrl.value = settings.url ?? ''
            } catch {
                // Gitea not available
            }
        }
    }
})

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

const { create, update, remove } = useTaskService()
const { remove: removeDocument, getByTask: getDocumentsByTask } = useTaskDocumentService()
const { create: createRecurrence, update: updateRecurrence, remove: removeRecurrence } = useTaskRecurrenceService()
const { t } = useI18n()

// Reset group when project changes in create mode
watch(() => form.projectId, () => {
    if (!showProjectSelect.value) return
    form.groupId = null
})

const authStore = useAuthStore()
const isAdmin = computed(() => authStore.user?.roles?.includes('ROLE_ADMIN') ?? false)

const availableTabs = computed(() => {
    const base: Array<'details' | 'planning' | 'mails'> = ['details', 'planning']
    if (isEditing.value) base.push('mails')
    return base
})

async function loadLinkedMails(): Promise<void> {
    if (!props.task) return
    mailsLoading.value = true
    try {
        linkedMails.value = await mailService.listMailsForTask(props.task.id)
    } catch {
        linkedMails.value = []
    } finally {
        mailsLoading.value = false
    }
}

watch(activeTab, async (tab) => {
    if (tab === 'mails' && props.task) {
        await loadLinkedMails()
    }
})

function formatMailDate(iso: string | null): string {
    if (!iso) return ''
    return new Date(iso).toLocaleDateString('fr', {
        day: '2-digit',
        month: 'short',
    })
}

const localDocuments = ref<TaskDocument[]>([])
const previewDoc = ref<TaskDocument | null>(null)

// Lien vers un fichier du partage SMB (en plus de l'upload classique)
const { enabled: shareEnabled, ensureLoaded: ensureShareStatus } = useShareStatus()
const showShareLinker = ref(false)
ensureShareStatus()

// Sync documents from task prop when modal opens or task changes
watch(() => props.task?.documents, (docs) => {
    localDocuments.value = docs ? [...docs] : []
}, { immediate: true })

async function refreshDocuments() {
    if (!props.task) return
    localDocuments.value = await getDocumentsByTask(props.task.id)
}

const previewIndex = computed(() => {
    if (!previewDoc.value) return -1
    return localDocuments.value.findIndex(d => d.id === previewDoc.value!.id)
})

function openPreview(doc: TaskDocument) {
    previewDoc.value = doc
}

function prevPreview() {
    if (previewIndex.value > 0) {
        previewDoc.value = localDocuments.value[previewIndex.value - 1]
    }
}

function nextPreview() {
    if (previewIndex.value < localDocuments.value.length - 1) {
        previewDoc.value = localDocuments.value[previewIndex.value + 1]
    }
}

const confirmDeleteDocOpen = ref(false)
const documentToDelete = ref<TaskDocument | null>(null)

function handleDeleteDocument(doc: TaskDocument) {
    documentToDelete.value = doc
    confirmDeleteDocOpen.value = true
}

async function confirmDeleteDocument() {
    if (!documentToDelete.value) return
    await removeDocument(documentToDelete.value.id)
    confirmDeleteDocOpen.value = false
    documentToDelete.value = null
    await refreshDocuments()
}

async function handleDocumentUploaded() {
    await refreshDocuments()
}

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
    touched.project = true
    if (!form.title.trim()) return
    if (showProjectSelect.value && !form.projectId) 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,
            collaborators: form.collaboratorIds.map(id => `/api/users/${id}`),
            group: form.groupId ? `/api/task_groups/${form.groupId}` : null,
            project: `/api/projects/${resolvedProjectId.value}`,
            tags: form.tagIds.map(id => `/api/task_tags/${id}`),
            scheduledStart: form.scheduledStart || null,
            scheduledEnd: form.scheduledEnd || null,
            deadline: form.deadline || null,
            syncToCalendar: form.syncToCalendar,
        }

        let savedTask: Task
        if (isEditing.value && props.task) {
            savedTask = await update(props.task.id, payload)
        } else {
            savedTask = await create(payload)
        }

        // Handle recurrence
        if (form.isRecurring) {
            const recPayload = {
                type: form.recurrenceType as 'daily' | 'weekly' | 'monthly' | 'yearly',
                interval: parseInt(form.recurrenceInterval) || 1,
                daysOfWeek: form.recurrenceType === 'weekly' ? form.recurrenceDaysOfWeek : null,
                dayOfMonth: form.recurrenceType === 'monthly' && form.monthlyMode === 'dayOfMonth'
                    ? parseInt(form.recurrenceDayOfMonth) || null : null,
                weekOfMonth: form.recurrenceType === 'monthly' && form.monthlyMode === 'weekOfMonth'
                    ? form.recurrenceWeekOfMonth : null,
                endDate: form.recurrenceEnd === 'date' ? form.recurrenceEndDate || null : null,
                maxOccurrences: form.recurrenceEnd === 'occurrences'
                    ? parseInt(form.recurrenceMaxOccurrences) || null : null,
            }

            if (savedTask.recurrence) {
                await updateRecurrence(savedTask.recurrence.id, recPayload)
            } else {
                const recurrence = await createRecurrence(recPayload)
                await update(savedTask.id, { recurrence: recurrence['@id'] ?? `/api/task_recurrences/${recurrence.id}` })
            }
        } else if (isEditing.value && props.task?.recurrence) {
            await removeRecurrence(props.task.recurrence.id)
        }

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

<style scoped>
.task-modal-enter-active,
.task-modal-leave-active {
    transition: opacity 0.2s ease;
}

.task-modal-enter-active > div:last-child,
.task-modal-leave-active > div:last-child {
    transition: transform 0.2s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.2s ease;
}

.task-modal-enter-from,
.task-modal-leave-to {
    opacity: 0;
}

.task-modal-enter-from > div:last-child {
    transform: scale(0.95) translateY(8px);
    opacity: 0;
}

.task-modal-leave-to > div:last-child {
    transform: scale(0.97);
    opacity: 0;
}
</style>
