import { describe, it, expect, beforeEach } from 'vitest';
import { usePluginStore } from '../pluginStore';
import type { PluginUiManifest } from '../pluginStore';

const mockManifests: PluginUiManifest[] = [
  {
    name: 'plugin_a',
    display_name: 'Plugin A',
    version: '1.0.0',
    is_core: false,
    menu_items: [
      { label_key: 'nav.a', label: 'A', path: '/a', icon: 'A', group: '', order: 200, badge_key: '' },
      { label_key: 'nav.a2', label: 'A2', path: '/a2', icon: 'A', group: '', order: 50, badge_key: '' },
    ],
    page_routes: [
      { path: '/a', component: '@/pages/A', parent: '', protected: true, order: 200 },
    ],
    detail_tabs: [
      { entity_type: 'contact', label_key: 'tabs.a', label: 'Tab A', component: '@/components/TabA', icon: 'A', order: 100, permission: '' },
      { entity_type: 'company', label_key: 'tabs.a2', label: 'Tab A2', component: '@/components/TabA2', icon: 'A', order: 50, permission: '' },
    ],
    settings_pages: [
      { path: 'a', label_key: 'settings.a', label: 'Settings A', component: '@/pages/SettingsA', icon: 'A', order: 200, permission: '' },
    ],
    dashboard_widgets: [
      { id: 'widget_a', label_key: 'widgets.a', label: 'Widget A', component: '@/components/WidgetA', icon: 'A', order: 200, col_span: 2, row_span: 1, permission: '' },
    ],
    custom_fields: [],
  },
  {
    name: 'plugin_b',
    display_name: 'Plugin B',
    version: '1.0.0',
    is_core: true,
    menu_items: [
      { label_key: 'nav.b', label: 'B', path: '/b', icon: 'B', group: '', order: 100, badge_key: '' },
    ],
    page_routes: [
      { path: '/b', component: '@/pages/B', parent: '', protected: true, order: 100 },
    ],
    detail_tabs: [
      { entity_type: 'contact', label_key: 'tabs.b', label: 'Tab B', component: '@/components/TabB', icon: 'B', order: 200, permission: '' },
    ],
    settings_pages: [
      { path: 'b', label_key: 'settings.b', label: 'Settings B', component: '@/pages/SettingsB', icon: 'B', order: 100, permission: '' },
    ],
    dashboard_widgets: [
      { id: 'widget_b', label_key: 'widgets.b', label: 'Widget B', component: '@/components/WidgetB', icon: 'B', order: 100, col_span: 1, row_span: 1, permission: '' },
    ],
    custom_fields: [],
  },
];

describe('pluginStore', () => {
  beforeEach(() => {
    usePluginStore.getState().reset();
  });

  describe('getAllMenuItems', () => {
    it('returns empty array when no manifests', () => {
      const items = usePluginStore.getState().getAllMenuItems();
      expect(items).toHaveLength(0);
    });

    it('merges menu items from all manifests', () => {
      usePluginStore.setState({ manifests: mockManifests });
      const items = usePluginStore.getState().getAllMenuItems();
      expect(items).toHaveLength(3);
    });

    it('sorts menu items by order ascending', () => {
      usePluginStore.setState({ manifests: mockManifests });
      const items = usePluginStore.getState().getAllMenuItems();
      expect(items[0].order).toBe(50);
      expect(items[1].order).toBe(100);
      expect(items[2].order).toBe(200);
    });
  });

  describe('getDetailTabsForEntity', () => {
    it('returns empty array when no manifests', () => {
      const tabs = usePluginStore.getState().getDetailTabsForEntity('contact');
      expect(tabs).toHaveLength(0);
    });

    it('filters tabs by entity_type', () => {
      usePluginStore.setState({ manifests: mockManifests });
      const contactTabs = usePluginStore.getState().getDetailTabsForEntity('contact');
      expect(contactTabs).toHaveLength(2);
      expect(contactTabs.every((t) => t.entity_type === 'contact')).toBe(true);
    });

    it('returns empty array for entity with no tabs', () => {
      usePluginStore.setState({ manifests: mockManifests });
      const taskTabs = usePluginStore.getState().getDetailTabsForEntity('task');
      expect(taskTabs).toHaveLength(0);
    });

    it('sorts tabs by order ascending', () => {
      usePluginStore.setState({ manifests: mockManifests });
      const contactTabs = usePluginStore.getState().getDetailTabsForEntity('contact');
      expect(contactTabs[0].order).toBe(100);
      expect(contactTabs[1].order).toBe(200);
    });
  });

  describe('getAllSettingsPages', () => {
    it('returns empty array when no manifests', () => {
      const pages = usePluginStore.getState().getAllSettingsPages();
      expect(pages).toHaveLength(0);
    });

    it('merges settings pages from all manifests', () => {
      usePluginStore.setState({ manifests: mockManifests });
      const pages = usePluginStore.getState().getAllSettingsPages();
      expect(pages).toHaveLength(2);
    });

    it('sorts settings pages by order ascending', () => {
      usePluginStore.setState({ manifests: mockManifests });
      const pages = usePluginStore.getState().getAllSettingsPages();
      expect(pages[0].order).toBe(100);
      expect(pages[1].order).toBe(200);
    });
  });

  describe('getAllDashboardWidgets', () => {
    it('returns empty array when no manifests', () => {
      const widgets = usePluginStore.getState().getAllDashboardWidgets();
      expect(widgets).toHaveLength(0);
    });

    it('merges widgets from all manifests', () => {
      usePluginStore.setState({ manifests: mockManifests });
      const widgets = usePluginStore.getState().getAllDashboardWidgets();
      expect(widgets).toHaveLength(2);
    });

    it('sorts widgets by order ascending', () => {
      usePluginStore.setState({ manifests: mockManifests });
      const widgets = usePluginStore.getState().getAllDashboardWidgets();
      expect(widgets[0].order).toBe(100);
      expect(widgets[1].order).toBe(200);
    });
  });

  describe('reset', () => {
    it('clears all state', () => {
      usePluginStore.setState({ manifests: mockManifests, loading: true, error: 'test', loaded: true });
      usePluginStore.getState().reset();
      const state = usePluginStore.getState();
      expect(state.manifests).toHaveLength(0);
      expect(state.loading).toBe(false);
      expect(state.error).toBeNull();
      expect(state.loaded).toBe(false);
    });
  });

  describe('setManifests', () => {
    it('sets manifests and marks as loaded', () => {
      usePluginStore.getState().setManifests(mockManifests);
      const state = usePluginStore.getState();
      expect(state.manifests).toHaveLength(2);
      expect(state.loaded).toBe(true);
      expect(state.loading).toBe(false);
      expect(state.error).toBeNull();
    });
  });

  describe('getAllPageRoutes', () => {
    it('returns empty array when no manifests', () => {
      const routes = usePluginStore.getState().getAllPageRoutes();
      expect(routes).toHaveLength(0);
    });

    it('merges page routes from all manifests', () => {
      usePluginStore.setState({ manifests: mockManifests });
      const routes = usePluginStore.getState().getAllPageRoutes();
      expect(routes).toHaveLength(2);
    });

    it('sorts page routes by order ascending', () => {
      usePluginStore.setState({ manifests: mockManifests });
      const routes = usePluginStore.getState().getAllPageRoutes();
      expect(routes[0].order).toBe(100);
      expect(routes[1].order).toBe(200);
    });
  });
});
