/**
 * Property-Based Test: Color resolution with fallback
 *
 * Feature: compliance-nonmetric-filters, Property 4: Color resolution with fallback
 * **Validates: Requirements 6.2, 6.3**
 *
 * For any non-metric category metric_id, the resolved color equals
 * CATEGORY_COLORS[metricCategoriesConfig[metricId]] when both lookups succeed,
 * else #94A3B8.
 */
import fc from 'fast-check';
import { deriveNonMetricCategories, CATEGORY_COLORS } from '../components/pages/CompliancePage';

// The color resolution logic extracted for direct testing
function resolveColor(metricId, categoriesConfig) {
    const categoryName = categoriesConfig[metricId] || null;
    return (categoryName && CATEGORY_COLORS[categoryName]) || '#94A3B8';
}

// Generators
const metricIdArb = fc.stringMatching(/^[A-Za-z0-9_.]{1,20}$/);

const knownCategories = Object.keys(CATEGORY_COLORS);
const categoryNameArb = fc.oneof(
    fc.constantFrom(...knownCategories),
    fc.string({ minLength: 1, maxLength: 20 }) // may not be in CATEGORY_COLORS
);

const categoriesConfigArb = fc.dictionary(metricIdArb, categoryNameArb);

describe('Compliance Non-Metric Filter — Property 4: Color resolution with fallback', () => {
    it('color equals CATEGORY_COLORS[config[metricId]] when both lookups succeed, else #94A3B8', () => {
        fc.assert(
            fc.property(metricIdArb, categoriesConfigArb, (metricId, config) => {
                const result = resolveColor(metricId, config);

                const categoryName = config[metricId];
                if (categoryName && CATEGORY_COLORS[categoryName]) {
                    expect(result).toBe(CATEGORY_COLORS[categoryName]);
                } else {
                    expect(result).toBe('#94A3B8');
                }
            }),
            { numRuns: 100 }
        );
    });

    it('deriveNonMetricCategories produces correct colors for all returned categories', () => {
        const deviceWithMetricArb = metricIdArb.map(id => ({
            hostname: 'host-' + id,
            failing_metrics: [{ metric_id: id, metric_desc: '', category: '' }],
        }));

        fc.assert(
            fc.property(
                fc.array(deviceWithMetricArb, { minLength: 1, maxLength: 15 }),
                categoriesConfigArb,
                (devices, config) => {
                    // Empty summary means all device metric_ids are non-metric
                    const result = deriveNonMetricCategories(devices, [], config);

                    for (const item of result) {
                        const expectedColor = resolveColor(item.metricId, config);
                        expect(item.color).toBe(expectedColor);
                    }
                }
            ),
            { numRuns: 100 }
        );
    });
});
