<template>
    <div class="px-[86px]">
        <div class="flex items-center justify-between relative">
            <div class="flex flex-row absolute -left-[60px]">
                <Icon
                    @click="router.push('/infrastructure/building')"
                    name="gg:arrow-left-o"
                    size="44"
                    class="cursor-pointer text-primary-500"
                />
            </div>
            <div class="flex items-center gap-4">
                <h1 class="font-bold text-4xl text-primary-500 uppercase">
                    {{ title }}
                </h1>
                <span class="text-lg text-slate-500">({{ totalItems }} bovin{{ totalItems > 1 ? 's' : '' }})</span>
                <div
                    v-if="hasCaseId"
                    class="bg-primary-500 p-1 rounded-md flex items-center cursor-pointer"
                    title="Imprimer"
                    @click="printCaseReport"
                >
                    <Icon name="mdi:printer-outline" size="32" class="text-white" />
                </div>
            </div>
            <NuxtLink
                v-if="hasCaseId && auth.isAdmin"
                :to="addBovineRoute"
                class="inline-flex items-center justify-center text-xl text-white uppercase bg-primary-500 h-[50px] px-6 rounded hover:opacity-80 gap-2"
            >
                <Icon name="mdi:plus" size="28" />
                Ajouter
            </NuxtLink>
        </div>

        <div class="mt-8 mb-16">
            <UiDataTable
                v-model:page="page"
                v-model:per-page="perPage"
                :columns="columns"
                :items="items"
                :total-items="totalItems"
                :loading="loading"
                :row-class="rowClass"
                :row-clickable="auth.isAdmin"
                empty-message="Aucun bovin dans cette case."
                @row-click="goToBovine"
            >
                <template #header-nationalNumber>
                    <UiTextInput
                        v-model="filters.nationalNumber"
                        placeholder="N° National"
                        size="compact"
                    />
                </template>
                <template #header-workNumber>
                    <UiTextInput
                        v-model="filters.workNumber"
                        placeholder="N° Travail"
                        size="compact"
                    />
                </template>
                <template #header-sex>
                    <UiSelect
                        v-model="filters.sex"
                        placeholder="Sexe"
                        :options="sexOptions"
                        size="compact"
                    />
                </template>
                <template #header-birthDate>
                    <UiDateMaskedInput v-model="birthDateFilter" size="compact" placeholder="Né le" />
                </template>
                <template #header-age>
                    <UiTextInput :model-value="''" placeholder="Age" size="compact" disabled />
                </template>
                <template #header-breedCode>
                    <UiTextInput
                        v-model="filters.breedCode"
                        placeholder="Race"
                        size="compact"
                    />
                </template>
                <template #header-buildingCase.building.label>
                    <UiTextInput :model-value="''" placeholder="Bâtiment" size="compact" disabled />
                </template>
                <template #header-buildingCase.caseNumber>
                    <UiTextInput :model-value="''" placeholder="Case" size="compact" disabled />
                </template>
                <template #header-arrivalDate>
                    <UiDateMaskedInput v-model="arrivalDateFilter" size="compact" placeholder="Entrée le" />
                </template>
                <template #cell-birthDate="{ item }">
                    {{ formatDate(item.birthDate) }}
                </template>
                <template #cell-age="{ item }">
                    {{ formatAgeLabel(item.ageMonths) }}
                </template>
                <template #cell-arrivalDate="{ item }">
                    {{ formatDate(item.arrivalDate) }}
                </template>
                <template #cell-buildingCase.building.label="{ item }">
                    {{ item.buildingCase?.building?.label ?? '—' }}
                </template>
                <template #cell-buildingCase.caseNumber="{ item }">
                    {{ item.buildingCase?.caseNumber ?? '—' }}
                </template>
            </UiDataTable>
        </div>
    </div>
</template>

<script setup lang="ts">
import type { BuildingCaseData } from '~/services/dto/building-case-data'
import type { BovineData } from '~/services/dto/bovine-data'
import { useAuthStore } from '~/stores/auth'
import { useDataTableServerState } from '~/composables/useDataTableServerState'
import { formatAgeLabel } from '~/utils/bovine-age'

const route = useRoute()
const router = useRouter()
const { printPdf } = usePdfPrinter()
const api = useApi()
const auth = useAuthStore()

const caseId = computed(() => Number(route.query.id))
const hasCaseId = computed(() => Number.isFinite(caseId.value) && caseId.value > 0)

const buildingCase = ref<BuildingCaseData | null>(null)

const { items, totalItems, page, perPage, filters, loading, reload } =
    useDataTableServerState<BovineData>(
        'bovines',
        {
            'exists[exitedAt]': 'false',
            buildingCase: '',
            nationalNumber: '',
            workNumber: '',
            breedCode: '',
            sex: '',
            'arrivalDate[after]': '',
            'arrivalDate[strictly_before]': '',
            'birthDate[after]': '',
            'birthDate[strictly_before]': ''
        },
        { initialPerPage: 10 }
    )

const sexOptions = [
    { value: 'M', label: 'Mâle' },
    { value: 'F', label: 'Femelle' }
]

const addOneDay = (dateString: string): string => {
    const [year, month, day] = dateString.split('-').map(Number)
    const next = new Date(Date.UTC(year, month - 1, day + 1))
    return next.toISOString().slice(0, 10)
}

const singleDateFilter = (afterKey: string, beforeKey: string) =>
    computed<string>({
        get: () => (filters.value[afterKey] as string) ?? '',
        set: (value: string) => {
            if (!value) {
                filters.value[afterKey] = ''
                filters.value[beforeKey] = ''
                return
            }
            filters.value[afterKey] = value
            filters.value[beforeKey] = addOneDay(value)
        }
    })

const arrivalDateFilter = singleDateFilter('arrivalDate[after]', 'arrivalDate[strictly_before]')
const birthDateFilter = singleDateFilter('birthDate[after]', 'birthDate[strictly_before]')

const columns = [
    { key: 'nationalNumber', label: 'N° National', width: '160px' },
    { key: 'workNumber', label: 'N° Travail', width: '85px' },
    { key: 'sex', label: 'Sexe', width: '70px' },
    { key: 'birthDate', label: 'Né le', width: '120px' },
    { key: 'age', label: 'Age', width: '110px' },
    { key: 'breedCode', label: 'Race' },
    { key: 'buildingCase.building.label', label: 'Bâtiment', width: '1.5fr' },
    { key: 'buildingCase.caseNumber', label: 'Case', width: '80px' },
    { key: 'arrivalDate', label: 'Entrée le', width: '120px' }
]

const title = computed(() => {
    if (!buildingCase.value) return ''
    const buildingLabel = buildingCase.value.building?.label ?? ''
    const caseNumber = buildingCase.value.caseNumber ?? ''
    return `${buildingLabel} case ${caseNumber}`.trim()
})

const addBovineRoute = computed(() => ({
    path: '/infrastructure/bovine',
    query: { caseId: String(caseId.value) }
}))

const formatDate = (date: string | null) => {
    if (!date) return '—'
    const d = new Date(date)
    if (isNaN(d.getTime())) return date
    return d.toLocaleDateString('fr-FR', {
        day: '2-digit',
        month: '2-digit',
        year: 'numeric'
    })
}

const rowClass = (item: BovineData): string => {
    if (item.ageMonths === null || item.ageMonths === undefined) return ''
    if (item.ageMonths >= 24) return 'bg-violet-300 hover:bg-violet-400'
    if (item.ageMonths >= 22) return 'bg-red-300 hover:bg-red-400'
    if (item.ageMonths >= 20) return 'bg-orange-300 hover:bg-orange-400'
    return ''
}

const loadCase = async () => {
    if (!hasCaseId.value) {
        buildingCase.value = null
        return
    }
    buildingCase.value = await api.get<BuildingCaseData>(`/building_cases/${caseId.value}`)
}

const printCaseReport = async () => {
    if (!hasCaseId.value) return
    const filename = `tableau_poids_case_${caseId.value}.pdf`
    await printPdf(`/building_cases/${caseId.value}/weights-report`, filename)
}

const goToBovine = (bovine: BovineData) => {
    if (!auth.isAdmin) return
    router.push({
        path: '/infrastructure/bovine',
        query: { id: String(bovine.id), caseId: String(caseId.value) }
    })
}

watch(caseId, (id) => {
    if (!hasCaseId.value) {
        filters.value.buildingCase = ''
        buildingCase.value = null
        return
    }
    filters.value.buildingCase = `/api/building_cases/${id}`
    loadCase()
    reload()
}, { immediate: true })
</script>
