<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.company"
                :label="$t('prospects.fields.company')"
                input-class="w-full"
                :error="touched.company && !form.company.trim() ? $t('prospects.validation.companyRequired') : ''"
                @blur="touched.company = 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({
    company: '',
})

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

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

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

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

    isSubmitting.value = true
    try {
        const payload: ProspectWrite = {
            company: form.company.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>
