/**
 * Column Configuration Types
 * 
 * Defines all available columns for the variations table
 */

export interface ColumnDefinition {
  id: string;
  label: string;
  enabled: boolean;
  order: number;
  editable: boolean;
  description?: string;
  width?: number;
}

export type ColumnId = 
  | 'id'
  | 'image'
  | 'parent_name'
  | 'sku'
  | 'price'
  | 'regular_price'
  | 'sale_price'
  | 'stock_quantity'
  | 'stock_status'
  | 'manage_stock'
  | 'weight'
  | 'length'
  | 'width'
  | 'height'
  | 'tax_class'
  | 'status'
  | 'description'
  | 'backorders'
  | 'low_stock_threshold'
  | 'virtual'
  | 'downloadable';

export const DEFAULT_COLUMNS: Record<ColumnId, ColumnDefinition> = {
  id: {
    id: 'id',
    label: 'ID',
    enabled: true,
    order: 0,
    editable: false,
    width: 80,
  },
  image: {
    id: 'image',
    label: 'Kép',
    enabled: true,
    order: 1,
    editable: false,
    width: 60,
  },
  parent_name: {
    id: 'parent_name',
    label: 'Termék',
    enabled: true,
    order: 2,
    editable: false,
    width: 250,
  },
  sku: {
    id: 'sku',
    label: 'SKU',
    enabled: true,
    order: 3,
    editable: true,
    width: 150,
  },
  price: {
    id: 'price',
    label: 'Ár',
    enabled: true,
    order: 4,
    editable: true,
    description: 'Aktuális ár (sale price vagy regular price)',
    width: 120,
  },
  regular_price: {
    id: 'regular_price',
    label: 'Alap ár',
    enabled: false,
    order: 5,
    editable: true,
    description: 'Eredeti ár akcióárazás nélkül',
    width: 120,
  },
  sale_price: {
    id: 'sale_price',
    label: 'Akciós ár',
    enabled: true,
    order: 6,
    editable: true,
    width: 120,
  },
  stock_quantity: {
    id: 'stock_quantity',
    label: 'Készlet',
    enabled: true,
    order: 7,
    editable: true,
    width: 100,
  },
  stock_status: {
    id: 'stock_status',
    label: 'Készlet státusz',
    enabled: true,
    order: 8,
    editable: true,
    description: 'Raktáron / Nincs raktáron / Utánrendelhető',
    width: 140,
  },
  manage_stock: {
    id: 'manage_stock',
    label: 'Készletkezelés',
    enabled: false,
    order: 9,
    editable: true,
    description: 'Automatikus készletkezelés be/ki',
    width: 130,
  },
  weight: {
    id: 'weight',
    label: 'Súly (kg)',
    enabled: false,
    order: 10,
    editable: true,
    description: 'Termék súlya kilogrammban',
    width: 100,
  },
  length: {
    id: 'length',
    label: 'Hossz (cm)',
    enabled: false,
    order: 11,
    editable: true,
    width: 100,
  },
  width: {
    id: 'width',
    label: 'Szélesség (cm)',
    enabled: false,
    order: 12,
    editable: true,
    width: 120,
  },
  height: {
    id: 'height',
    label: 'Magasság (cm)',
    enabled: false,
    order: 13,
    editable: true,
    width: 120,
  },
  tax_class: {
    id: 'tax_class',
    label: 'Adóosztály',
    enabled: false,
    order: 14,
    editable: true,
    description: 'ÁFA kategória',
    width: 120,
  },
  status: {
    id: 'status',
    label: 'Státusz',
    enabled: false,
    order: 15,
    editable: true,
    description: 'Publikálva / Vázlat / Privát',
    width: 120,
  },
  description: {
    id: 'description',
    label: 'Leírás',
    enabled: false,
    order: 16,
    editable: true,
    width: 200,
  },
  backorders: {
    id: 'backorders',
    label: 'Utánrendelés',
    enabled: false,
    order: 17,
    editable: true,
    description: 'Engedélyezett-e a készlet alatti rendelés',
    width: 130,
  },
  low_stock_threshold: {
    id: 'low_stock_threshold',
    label: 'Alacsony készlet',
    enabled: false,
    order: 18,
    editable: true,
    description: 'Figyelmeztetési küszöb',
    width: 140,
  },
  virtual: {
    id: 'virtual',
    label: 'Virtuális',
    enabled: false,
    order: 19,
    editable: true,
    description: 'Nem szállítandó termék',
    width: 100,
  },
  downloadable: {
    id: 'downloadable',
    label: 'Letölthető',
    enabled: false,
    order: 20,
    editable: true,
    description: 'Digitális termék letöltéssel',
    width: 110,
  },
};

export const LOCALSTORAGE_KEY = 'varihu_column_config';

/**
 * Get user's column configuration from localStorage
 */
export function getColumnConfig(): Record<ColumnId, ColumnDefinition> {
  try {
    const stored = localStorage.getItem(LOCALSTORAGE_KEY);
    if (stored) {
      const parsed = JSON.parse(stored);
      // Merge with defaults to handle new columns
      return { ...DEFAULT_COLUMNS, ...parsed };
    }
  } catch (error) {
    console.error('[ColumnConfig] Failed to load from localStorage:', error);
  }
  return DEFAULT_COLUMNS;
}

/**
 * Save column configuration to localStorage
 */
export function saveColumnConfig(config: Record<ColumnId, ColumnDefinition>): void {
  try {
    localStorage.setItem(LOCALSTORAGE_KEY, JSON.stringify(config));
  } catch (error) {
    console.error('[ColumnConfig] Failed to save to localStorage:', error);
  }
}

/**
 * Get enabled columns sorted by order
 */
export function getEnabledColumns(config: Record<ColumnId, ColumnDefinition>): ColumnDefinition[] {
  return Object.values(config)
    .filter(col => col.enabled)
    .sort((a, b) => a.order - b.order);
}

/**
 * Get all columns sorted by order
 */
export function getAllColumnsSorted(config: Record<ColumnId, ColumnDefinition>): ColumnDefinition[] {
  return Object.values(config).sort((a, b) => a.order - b.order);
}
