import { Injectable } from '@angular/core';
import { GroupConfig } from '../../shared/components/data-table/data-table.component';

export interface TablePreferences {
  visibility: Record<string, boolean>;
  columnOrder: string[];
  showFilters?: boolean;
  grouping?: {
    groups: GroupConfig[];
    aggregates: {
      [level: number]: {
        [field: string]: 'sum' | 'avg' | 'count' | 'min' | 'max' | null;
      };
    };
  };
  columns: ColumnPreference[];
}

interface ColumnPreference {
  field: string;
  width: string;
}

@Injectable({
  providedIn: 'root'
})
export class UserPreferencesService {
  private readonly STORAGE_PREFIX = 'table_preferences_';

  saveTablePreferences(tableId: string, preferences: TablePreferences) {
    localStorage.setItem(
      this.STORAGE_PREFIX + tableId,
      JSON.stringify(preferences)
    );
  }

  getTablePreferences(tableId: string): TablePreferences {
    const saved = localStorage.getItem(this.STORAGE_PREFIX + tableId);
    return saved ? JSON.parse(saved) : {
      visibility: {},
      columnOrder: [],
      columns: [],
      showFilters: true
    };
  }
} 