import { describe, it, expect } from 'vitest';
import { handleBaseProductChange, handleModuleToggle } from './wizard-state';
import { Product, ProductModule, Category, WizardSelections } from './types';

describe('Wizard State Management', () => {
  const mockCategories: Category[] = [
    {
      id: 'cat-1',
      name: 'Kasse',
      sort_order: 1,
      is_required: true,
      allow_multiselect: false,
      free_items_limit: 0,
      preselect: false,
      resets_others: false,
      created_at: '',
    },
    {
      id: 'cat-2',
      name: 'Backoffice',
      sort_order: 2,
      is_required: false,
      allow_multiselect: false, // Radio behavior
      free_items_limit: 0,
      preselect: false,
      resets_others: false,
      created_at: '',
    }
  ];

  const mockModules: ProductModule[] = [
    {
      id: 'mod-cloud',
      product_id: 'prod-gastro',
      name: 'POSCloud',
      price: 10,
      requirements: [],
      exclusions: [],
      created_at: '',
    },
    {
      id: 'mod-office-1',
      product_id: 'prod-gastro',
      name: 'Office Pro',
      price: 20,
      requirements: [],
      exclusions: [],
      created_at: '',
    },
    {
      id: 'mod-office-2',
      product_id: 'prod-gastro',
      name: 'Office Light',
      price: 5,
      requirements: [],
      exclusions: [],
      created_at: '',
    }
  ];

  it('should automatically remove excluded modules when switching base product', () => {
    const currentSelections: WizardSelections = {
      'cat-2': {
        productId: null,
        moduleIds: ['mod-cloud'] // User has POSCloud selected
      }
    };

    // Exclusion rule: 'prod-small' excludes 'mod-cloud'
    const mockExclusions = [
      {
        product_id: 'prod-small',
        excluded_module_id: 'mod-cloud'
      }
    ];

    const result = handleBaseProductChange(
      'prod-small',
      currentSelections,
      mockModules,
      [],
      mockExclusions
    );

    // POSCloud should be removed
    expect(result['cat-2'].moduleIds).not.toContain('mod-cloud');
  });

  it('should respect allow_multiselect = false and only allow one selection in that category', () => {
    let selections: WizardSelections = {
      'cat-2': {
        productId: null,
        moduleIds: ['mod-office-1']
      }
    };

    // Toggle on 'mod-office-2' in a single-select category
    selections = handleModuleToggle(
      'mod-office-2',
      'cat-2',
      selections,
      mockCategories,
      mockModules
    );

    // Should only contain 'mod-office-2'
    expect(selections['cat-2'].moduleIds).toContain('mod-office-2');
    expect(selections['cat-2'].moduleIds).not.toContain('mod-office-1');
  });
});
