<template>
    <form @submit.prevent="validate">

        <div class="flex items-center relative">
            <div class="flex flex-row absolute -left-[60px] ">
                <Icon @click="router.push('/admin/supplier/supplier-list')" name="gg:arrow-left-o" size="40" class="cursor-pointer text-primary-500"/>
            </div>
            <h1 class="text-3xl text-primary-500  font-bold uppercase">
                {{ supplierId ? "Modifications du fournisseur" : "Ajout d'un fournisseur" }}
            </h1>
        </div>

        <div class="flex flex-cols-3 justify-between mb-11 pt-7">
            <UiTextInput id="supplier-name" v-model="form.name" label="Nom du fournisseur" :disabled="!auth.isAdmin" wrapper-class="w-[280px]"/>
            <UiTextInput id="supplier-email" v-model="form.email" label="Email" :disabled="!auth.isAdmin" wrapper-class="w-[280px]"/>
            <UiTextInput id="supplier-phone" v-model="form.phone" label="Téléphone" :disabled="!auth.isAdmin" wrapper-class="w-[280px]"/>
        </div>
        <div class="flex items-center justify-center">
            <UiButton
                class="inline-flex items-center mb-[106px] justify-center text-xl text-white uppercase bg-primary-500 h-[50px] px-8 rounded hover:opacity-80 gap-2"
                type="submit"
                :disabled="isLoading || !auth.isAdmin"
            >
                <Icon :name="supplierId ? '' : 'mdi:plus'" size="28" />
                {{ supplierId ? "Valider" : "Ajouter" }}
            </UiButton>
        </div>

        <div class="flex items-center justify-between mb-7">
            <h2 class="text-3xl text-primary-500 font-bold uppercase">Adresses fournisseur</h2>
        </div>
        <div class="overflow-x-auto mb-11 text-primary-700">
            <table class="w-full border-collapse">
                <thead>
                <tr class="text-left border bg-slate-100 border-gray-200">
                    <th class="py-3 px-4 text-sm uppercase">Libellé</th>
                    <th class="py-3 px-4 text-sm uppercase">Rue</th>
                    <th class="py-3 px-4 text-sm uppercase">Complément</th>
                    <th class="py-3 px-4 text-sm uppercase">Code postal</th>
                    <th class="py-3 px-4 text-sm uppercase">Ville</th>
                    <th class="py-3 px-4 text-sm uppercase">Pays</th>
                </tr>
                </thead>
                <tbody>
                <template v-if="form.addresses.length === 0">
                    <tr>
                        <td colspan="6" class="py-4 text-slate-400">
                            Aucune adresse.
                        </td>
                    </tr>
                </template>
                <template v-else>
                    <tr
                        v-for="(address, index) in form.addresses"
                        :key="address.id ?? index"
                        class="border border-gray-100 hover:bg-slate-50"
                        :class="auth.isAdmin ? 'cursor-pointer' : 'cursor-not-allowed opacity-60'"
                        @click="goToEditAddress(address.id ?? null)"
                    >
                        <td class="py-3 px-4">{{ address.label || "—" }}</td>
                        <td class="py-3 px-4">{{ address.street || "—" }}</td>
                        <td class="py-3 px-4">{{ address.street2 || "—" }}</td>
                        <td class="py-3 px-4">{{ address.postalCode || "—" }}</td>
                        <td class="py-3 px-4">{{ address.city || "—" }}</td>
                        <td class="py-3 px-4">{{ address.countryCode || "—" }}</td>
                    </tr>
                </template>
                </tbody>
            </table>
        </div>
        <div class="flex justify-center items-center">
        <UiButton
            type="button"
            class="inline-flex items-center mb-16 justify-center text-xl text-white uppercase bg-primary-500 h-[50px] px-8 rounded hover:opacity-80 gap-2"
            :disabled="supplierId === null || !auth.isAdmin"
            @click="goToAddAddress"
        >
            <Icon name="mdi:plus" size="28" />
            Ajouter
        </UiButton>
        </div>
    </form>
</template>

<script setup lang="ts">
import {computed, reactive, ref, watch} from "vue"
import {createSupplier, getSupplier, updateSupplier} from "~/services/supplier"
import type {SupplierData, SupplierFormData, SupplierPayload} from "~/services/dto/supplier-data"
import {useAuthStore} from "~/stores/auth"

const route = useRoute()
const router = useRouter()
const auth = useAuthStore()

const resolveId = (param: unknown) => {
    const idStr = Array.isArray(param) ? param[0] : param
    if (!idStr) return null
    const id = Number(idStr)
    return Number.isFinite(id) ? id : null
}
const supplierId = computed(() => resolveId(route.params.id))
const isLoading = ref(false)
const form = reactive<SupplierFormData>({
    name: "",
    email: "",
    phone: "",
    addresses: [],
})

const goToAddAddress = () => {
    if (supplierId.value === null || !auth.isAdmin) return
    router.push({
        path: "/admin/supplier/address",
        query: {
            supplierId: String(supplierId.value),
            fromSupplier: "1",
        },
    })
}

const goToEditAddress = (addressId: number | null) => {
    if (supplierId.value === null || addressId === null || !auth.isAdmin) return
    router.push({
        path: "/admin/supplier/address",
        query: {
            supplierId: String(supplierId.value),
            addressId: String(addressId),
            fromSupplier: "1",
        },
    })
}

const hydrateFromSupplier = (supplier: SupplierData | null) => {
    if (!supplier) return
    form.name = supplier.name ?? ""
    form.email = supplier.email ?? ""
    form.phone = supplier.phone ?? ""
    if (!Array.isArray(supplier.addresses) || supplier.addresses.length === 0) {
        form.addresses = []
        return
    }
    if (typeof supplier.addresses[0] === "string") {
        form.addresses = []
        return
    }

    form.addresses = supplier.addresses.map((address) => ({
        id: address.id ?? null,
        label: address.label ?? "",
        street: address.street ?? "",
        street2: address.street2 ?? null,
        postalCode: address.postalCode ?? "",
        city: address.city ?? "",
        countryCode: address.countryCode ?? "",
    }))
}

watch(
    () => supplierId.value,
    async (id) => {
        if (id === null) return
        isLoading.value = true
        try {
            const supplier = await getSupplier(id)
            hydrateFromSupplier(supplier)
        } finally {
            isLoading.value = false
        }
    },
    {immediate: true}
)

async function validate() {
    if (isLoading.value) return
    if (!auth.isAdmin) return
    isLoading.value = true

    try {
        const name = form.name.trim()
        const email = (form.email ?? "").trim() || null
        const phone = (form.phone ?? "").trim() || null

        const supplierPayload: SupplierPayload = {
            name,
            email,
            phone,
        }
        let targetId: number | null = null

        if (supplierId.value !== null) {
            await updateSupplier(supplierId.value, supplierPayload)
            targetId = supplierId.value
        } else {
            const created = await createSupplier(supplierPayload)
            targetId = created.id
        }

        await router.push(`/admin/supplier/${targetId}`)
    } finally {
        isLoading.value = false
    }
}
</script>
