import { create } from 'zustand';

export interface PluginMenuItem {
  label_key: string;
  label: string;
  path: string;
  icon: string;
  group: string;
  order: number;
  badge_key: string;
}

export interface PluginPageRoute {
  path: string;
  component: string;
  parent: string;
  protected: boolean;
  order: number;
}

export interface PluginDetailTab {
  entity_type: string;
  label_key: string;
  label: string;
  component: string;
  icon: string;
  order: number;
  permission: string;
}

export interface PluginSettingsPage {
  path: string;
  label_key: string;
  label: string;
  component: string;
  icon: string;
  order: number;
  permission: string;
}

export interface PluginDashboardWidget {
  id: string;
  label_key: string;
  label: string;
  component: string;
  icon: string;
  order: number;
  col_span: number;
  row_span: number;
  permission: string;
}

export interface PluginCustomFieldDefinition {
  name: string;
  label: string;
  label_key: string;
  field_type: 'text' | 'number' | 'date' | 'select' | 'multiselect' | 'boolean';
  options: string[];
  default_value: any;
  required: boolean;
  entity: string;
}

export interface PluginUiManifest {
  name: string;
  display_name: string;
  version: string;
  is_core: boolean;
  menu_items: PluginMenuItem[];
  page_routes: PluginPageRoute[];
  detail_tabs: PluginDetailTab[];
  settings_pages: PluginSettingsPage[];
  dashboard_widgets: PluginDashboardWidget[];
  custom_fields: PluginCustomFieldDefinition[];
}

interface PluginState {
  manifests: PluginUiManifest[];
  loading: boolean;
  error: string | null;
  loaded: boolean;
  setManifests: (manifests: PluginUiManifest[]) => void;
  setLoading: (loading: boolean) => void;
  setError: (error: string | null) => void;
  reset: () => void;
  // Computed selectors
  getAllMenuItems: () => PluginMenuItem[];
  getAllPageRoutes: () => PluginPageRoute[];
  getDetailTabsForEntity: (entityType: string) => PluginDetailTab[];
  getAllSettingsPages: () => PluginSettingsPage[];
  getAllDashboardWidgets: () => PluginDashboardWidget[];
  getCustomFieldsForEntity: (entityType: string) => PluginCustomFieldDefinition[];
}

export const usePluginStore = create<PluginState>((set, get) => ({
  manifests: [],
  loading: false,
  error: null,
  loaded: false,

  setManifests: (manifests) => set({ manifests, loaded: true, loading: false, error: null }),
  setLoading: (loading) => set({ loading }),
  setError: (error) => set({ error, loading: false }),
  reset: () => set({ manifests: [], loading: false, error: null, loaded: false }),

  getAllMenuItems: () => {
    const { manifests } = get();
    return manifests
      .flatMap((m) => m.menu_items)
      .sort((a, b) => a.order - b.order);
  },

  getAllPageRoutes: () => {
    const { manifests } = get();
    return manifests
      .flatMap((m) => m.page_routes)
      .sort((a, b) => a.order - b.order);
  },

  getDetailTabsForEntity: (entityType: string) => {
    const { manifests } = get();
    return manifests
      .flatMap((m) => m.detail_tabs)
      .filter((t) => t.entity_type === entityType)
      .sort((a, b) => a.order - b.order);
  },

  getAllSettingsPages: () => {
    const { manifests } = get();
    return manifests
      .flatMap((m) => m.settings_pages)
      .sort((a, b) => a.order - b.order);
  },

  getAllDashboardWidgets: () => {
    const { manifests } = get();
    return manifests
      .flatMap((m) => m.dashboard_widgets)
      .sort((a, b) => a.order - b.order);
  },

  getCustomFieldsForEntity: (entityType: string) => {
    const { manifests } = get();
    return manifests
      .flatMap((m) => m.custom_fields || [])
      .filter((cf) => cf.entity === entityType);
  },
}));
