<script setup lang="ts">
import { computed } from 'vue';
import MediaThumbnailCell from '@/components/media/MediaThumbnailCell.vue';
import { Badge } from '@/components/ui/badge';
import {
    Empty,
    EmptyDescription,
    EmptyHeader,
    EmptyTitle,
} from '@/components/ui/empty';
import {
    Table,
    TableBody,
    TableCell,
    TableHead,
    TableHeader,
    TableRow,
} from '@/components/ui/table';
import type { CuttingListItem } from '@/types/cutting';

const props = defineProps<{
    cuttings: CuttingListItem[];
}>();

interface GroupedCuttingResults {
    productId: number;
    productName: string;
    items: any[];
}

function getGroupedResults(results: any[]): GroupedCuttingResults[] {
    const groups: Record<number, GroupedCuttingResults> = {};

    results.forEach((item) => {
        const product = item.product_variant?.product;
        const productId = product?.id ?? 0;
        const productName = product?.name ?? 'Produk Tidak Diketahui';

        if (!groups[productId]) {
            groups[productId] = {
                productId,
                productName,
                items: [],
            };
        }

        groups[productId].items.push(item);
    });

    return Object.values(groups);
}

const showingCount = computed(() => props.cuttings.length);
</script>

<template>
    <div class="space-y-4">
        <div class="flex items-center gap-2">
            <h3 class="text-lg font-semibold">
                Riwayat Verifikasi
            </h3>
        </div>

        <div v-if="cuttings.length" class="space-y-4">
            <div v-for="cutting in cuttings" :key="cutting.id" class="overflow-hidden rounded-md border">
                <div
                    class="flex flex-col gap-3 border-b bg-muted/30 px-4 py-3 sm:flex-row sm:items-start sm:justify-between">
                    <div class="flex min-w-0 items-start gap-3">
                        <div class="min-w-0 space-y-2">
                            <div class="flex flex-wrap items-center gap-2">
                                <h3 class="font-medium leading-tight">
                                    Cutting #{{ cutting.id }}
                                </h3>
                                <Badge variant="default">
                                    Terverifikasi
                                </Badge>
                            </div>
                            <div class="text-muted-foreground space-y-1 text-sm">
                                <p>{{ cutting.created_at_formatted }}</p>
                                <p>Oleh {{ cutting.created_by?.profile?.full_name ?? cutting.created_by?.username }}</p>
                            </div>
                            <p v-if="cutting.description" class="text-muted-foreground text-sm">
                                {{ cutting.description }}
                            </p>
                            <div class="flex flex-wrap gap-x-4 gap-y-1 text-sm">
                                <span>Total Hasil Cutting <strong class="text-primary">{{ cutting.total_result_pieces ??
                                    0 }} pcs</strong></span>
                                <span>Biaya Produksi <strong class="text-primary">{{
                                    cutting.total_production_cost_formatted ?? 'Rp 0' }}</strong></span>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="p-4">
                    <div class="space-y-2">
                        <h4 class="text-sm font-semibold tracking-tight text-foreground">Hasil Produk</h4>
                        <div class="rounded-md border">
                            <Table>
                                <TableHeader>
                                    <TableRow>
                                        <TableHead>Varian</TableHead>
                                        <TableHead>Foto</TableHead>
                                        <TableHead>Hasil</TableHead>
                                        <TableHead>Sample</TableHead>
                                        <TableHead>Diluar Sample</TableHead>
                                    </TableRow>
                                </TableHeader>
                                <TableBody>
                                    <TableRow v-if="!cutting.results.length" :key="`${cutting.id}-result-empty`">
                                        <TableCell colspan="5" class="text-muted-foreground">
                                            Belum ada hasil produk
                                        </TableCell>
                                    </TableRow>
                                    <template v-else v-for="group in getGroupedResults(cutting.results)"
                                        :key="group.productId">
                                        <TableRow class="bg-muted/20 hover:bg-muted/20">
                                            <TableCell colspan="5" class="font-semibold text-foreground">
                                                {{ group.productName }}
                                            </TableCell>
                                        </TableRow>
                                        <TableRow v-for="result in group.items" :key="result.id">
                                            <TableCell class="pl-6 font-medium">
                                                {{ result.product_variant?.name }}
                                            </TableCell>
                                            <TableCell>
                                                <MediaThumbnailCell :items="result.product_variant?.images ?? []" :max-visible="1" :all-stocks="(result.product_variant?.images ?? []).map(() => `${result.cutting_result} pcs`)" />
                                            </TableCell>
                                            <TableCell class="tabular-nums">
                                                {{ result.cutting_result }} pcs
                                            </TableCell>
                                            <TableCell class="tabular-nums">
                                                {{ result.sample }} pcs
                                            </TableCell>
                                            <TableCell class="tabular-nums">
                                                {{ result.original_outside_sample }} pcs
                                            </TableCell>
                                        </TableRow>
                                    </template>
                                </TableBody>
                            </Table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div v-else class="rounded-md border px-6 py-10">
            <Empty>
                <EmptyHeader>
                    <EmptyTitle>Belum ada riwayat</EmptyTitle>
                    <EmptyDescription>
                        Belum ada cutting yang telah diverifikasi.
                    </EmptyDescription>
                </EmptyHeader>
            </Empty>
        </div>
    </div>
</template>
