<template>
    <MalioDrawer v-model="isOpen">
        <template #header>
            <h2 class="text-xl font-bold">{{ isEditing ? $t('prospects.editProspect') : $t('prospects.addProspect') }}</h2>
        </template>
        <form @submit.prevent="handleSubmit" class="flex flex-col gap-2">
            <MalioInputText
                v-model="form.name"
                label="Nom société"
                input-class="w-full"
                :error="touched.name && !form.name.trim() ? $t('prospects.validation.nameRequired') : ''"
                @blur="touched.name = true"
            />

            <div class="mt-6 flex items-center justify-between gap-2">
                <MalioButton
                    v-if="isEditing && !isConverted"
                    :label="$t('prospects.convert')"
                    variant="secondary"
                    icon-name="mdi:account-convert"
                    icon-position="left"
                    button-class="w-auto px-4"
                    :disabled="isSubmitting"
                    @click="handleConvert"
                />
                <span v-else-if="isConverted" class="text-sm text-green-700">
                    {{ $t('prospects.alreadyConverted') }}
                </span>
                <span v-else />
                <MalioButton
                    :label="$t('common.save')"
                    button-class="w-auto px-6"
                    :disabled="isSubmitting"
                    @click="handleSubmit"
                />
            </div>
        </form>
    </MalioDrawer>
</template>

<script setup lang="ts">
import type { Prospect, ProspectWrite } from '~/modules/directory/services/dto/prospect'
import { useProspectService } from '~/modules/directory/services/prospects'

const props = defineProps<{
    modelValue: boolean
    prospect: Prospect | null
}>()

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.prospect)
const isConverted = computed(() => !!props.prospect?.convertedClient)
const isSubmitting = ref(false)

const form = reactive({
    name: '',
})

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

watch(() => props.modelValue, (open) => {
    if (open) {
        form.name = props.prospect?.name ?? ''
        touched.name = false
    }
})

const { create, update, convert } = useProspectService()

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

    isSubmitting.value = true
    try {
        const payload: ProspectWrite = {
            name: form.name.trim(),
        }

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

        emit('saved')
        isOpen.value = false
    } finally {
        isSubmitting.value = false
    }
}

async function handleConvert() {
    if (!props.prospect) return
    isSubmitting.value = true
    try {
        await convert(props.prospect.id)
        emit('saved')
        isOpen.value = false
    } finally {
        isSubmitting.value = false
    }
}
</script>
