<script setup>
/** @deprecated Внимание: старая версия (V1/V2). Используйте V3. */
import { computed } from 'vue';

const props = defineProps({
    /**
     * Supported shapes:
     * - Array of rows: [{ segmentName: '...', ChannelA: 'High', ChannelB: 'Low', ... }, ...]
     * - Object: { channels/columns: string[], data: Array<row> }
     */
    matrix: { type: [Array, Object], default: null },
    title: { type: String, default: 'Матрица каналов по сегментам' },
    icon: { type: String, default: 'pi pi-th-large' },
    showHeader: { type: Boolean, default: true }
});

const normalizeMatrix = (input) => {
    if (!input) return null;

    const data = Array.isArray(input) ? input : Array.isArray(input.data) ? input.data : Array.isArray(input.matrix) ? input.matrix : [];
    if (!Array.isArray(data) || data.length === 0) return { columns: [], data: [] };

    const cols = new Set(Array.isArray(input.columns) ? input.columns : Array.isArray(input.channels) ? input.channels : []);
    if (cols.size === 0) {
        data.forEach((row) => {
            if (!row || typeof row !== 'object') return;
            Object.keys(row).forEach((key) => {
                if (key !== 'segmentName' && key !== 'segment' && key !== 'name') cols.add(key);
            });
        });
    }

    return { columns: Array.from(cols), data };
};

const normalized = computed(() => normalizeMatrix(props.matrix));

const getRowName = (row) => {
    return row?.segmentName || row?.segment || row?.name || '-';
};

const normalizeLevel = (value) => {
    if (value == null || value === '') return null;
    const s = String(value).trim().toLowerCase();

    if (s === 'high' || s.includes('высок')) return 'high';
    if (s === 'medium' || s.includes('средн')) return 'medium';
    if (s === 'low' || s.includes('низк')) return 'low';
    return null;
};

const getCellStyle = (value) => {
    const level = normalizeLevel(value);
    if (level === 'high') return { backgroundColor: '#22c55e', color: '#ffffff' };
    if (level === 'medium') return { backgroundColor: '#facc15', color: '#111827' };
    if (level === 'low') return { backgroundColor: '#e5e7eb', color: '#111827' };
    return { backgroundColor: 'rgba(156, 163, 175, 0.35)', color: '#111827' };
};

const formatCell = (value) => {
    if (value == null || value === '') return '-';
    return String(value);
};
</script>

<template>
    <div v-if="normalized && normalized.data.length" class="heatmap-table mt-6">
        <div v-if="showHeader" class="flex align-items-center mb-3">
            <i v-if="icon" :class="icon" class="text-primary-500 mr-2"></i>
            <h4 class="text-lg font-semibold text-surface-700 dark:text-surface-300 m-0">{{ title }}</h4>
        </div>

        <div class="overflow-x-auto border-round" style="border: 1px solid var(--surface-border)">
            <table class="heatmap-table__table w-full border-collapse">
                <thead>
                    <tr>
                        <th class="heatmap-table__th">Сегмент</th>
                        <th v-for="col in normalized.columns" :key="col" class="heatmap-table__th text-center">
                            {{ col }}
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(row, idx) in normalized.data" :key="idx" class="heatmap-table__tr">
                        <td class="heatmap-table__td font-semibold">{{ getRowName(row) }}</td>
                        <td v-for="col in normalized.columns" :key="col" class="heatmap-table__td text-center">
                            <span class="heatmap-table__badge" :style="getCellStyle(row?.[col])">
                                {{ formatCell(row?.[col]) }}
                            </span>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</template>

<style scoped>
.heatmap-table__table {
    border: 1px solid var(--surface-border);
    border-radius: 0.5rem;
    overflow: hidden;
}

.heatmap-table__th {
    padding: 0.75rem 1rem;
    text-align: left;
    font-weight: 600;
    color: var(--text-color);
    background-color: var(--surface-100);
    border-bottom: 2px solid var(--surface-border);
    white-space: nowrap;
}

.heatmap-table__td {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--surface-border);
    color: var(--text-color);
}

.heatmap-table__tr:last-child .heatmap-table__td {
    border-bottom: none;
}

.heatmap-table__tr:hover {
    background-color: var(--surface-50);
}

.heatmap-table__badge {
    display: inline-block;
    padding: 0.375rem 0.75rem;
    border-radius: 0.375rem;
    font-weight: 700;
    font-size: 0.875rem;
    min-width: 70px;
    text-align: center;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}
</style>
