import { describe, it, expect, beforeEach } from 'vitest';
import { act } from '@testing-library/react';
import { useUIStore } from './uiStore';

describe('uiStore', () => {
  beforeEach(() => {
    useUIStore.getState().reset();
  });

  describe('initial state', () => {
    it('should have correct initial values', () => {
      const state = useUIStore.getState();
      expect(state.showAppSelector).toBe(false);
      expect(state.appSelectorType).toBe('action');
      expect(state.selectedPlaceholderId).toBeNull();
      expect(state.dataFieldsModal).toBeNull();
      expect(state.nodeToDelete).toBeNull();
      expect(state.showUnsavedDialog).toBe(false);
    });
  });

  describe('app selector', () => {
    it('should open app selector with type action', () => {
      act(() => useUIStore.getState().openAppSelector('action'));

      const state = useUIStore.getState();
      expect(state.showAppSelector).toBe(true);
      expect(state.appSelectorType).toBe('action');
      expect(state.selectedPlaceholderId).toBeNull();
    });

    it('should open app selector with type trigger', () => {
      act(() => useUIStore.getState().openAppSelector('trigger'));

      const state = useUIStore.getState();
      expect(state.showAppSelector).toBe(true);
      expect(state.appSelectorType).toBe('trigger');
    });

    it('should open app selector with placeholder id', () => {
      act(() => useUIStore.getState().openAppSelector('action', 'node-123'));

      const state = useUIStore.getState();
      expect(state.showAppSelector).toBe(true);
      expect(state.appSelectorType).toBe('action');
      expect(state.selectedPlaceholderId).toBe('node-123');
    });

    it('should close app selector and clear placeholder', () => {
      act(() => {
        useUIStore.getState().openAppSelector('trigger', 'node-456');
        useUIStore.getState().closeAppSelector();
      });

      const state = useUIStore.getState();
      expect(state.showAppSelector).toBe(false);
      expect(state.selectedPlaceholderId).toBeNull();
    });
  });

  describe('data fields modal', () => {
    it('should open data fields modal with trigger type', () => {
      act(() => useUIStore.getState().openDataFieldsModal('trigger', 'wordpress/user_registration'));

      expect(useUIStore.getState().dataFieldsModal).toEqual({
        nodeType: 'trigger',
        typeIdentifier: 'wordpress/user_registration',
      });
    });

    it('should open data fields modal with action type', () => {
      act(() => useUIStore.getState().openDataFieldsModal('action', 'wordpress/send_email'));

      expect(useUIStore.getState().dataFieldsModal).toEqual({
        nodeType: 'action',
        typeIdentifier: 'wordpress/send_email',
      });
    });

    it('should close data fields modal', () => {
      act(() => {
        useUIStore.getState().openDataFieldsModal('action', 'wordpress/send_email');
        useUIStore.getState().closeDataFieldsModal();
      });

      expect(useUIStore.getState().dataFieldsModal).toBeNull();
    });
  });

  describe('node deletion', () => {
    it('should request node deletion with id and name', () => {
      act(() => useUIStore.getState().requestDeleteNode('node-1', 'Send Email'));

      expect(useUIStore.getState().nodeToDelete).toEqual({
        id: 'node-1',
        name: 'Send Email',
      });
    });

    it('should cancel node deletion', () => {
      act(() => {
        useUIStore.getState().requestDeleteNode('node-1', 'Send Email');
        useUIStore.getState().cancelDeleteNode();
      });

      expect(useUIStore.getState().nodeToDelete).toBeNull();
    });

    it('should clear node to delete', () => {
      act(() => {
        useUIStore.getState().requestDeleteNode('node-1', 'Send Email');
        useUIStore.getState().clearNodeToDelete();
      });

      expect(useUIStore.getState().nodeToDelete).toBeNull();
    });
  });

  describe('unsaved changes dialog', () => {
    it('should set showUnsavedDialog', () => {
      act(() => useUIStore.getState().setShowUnsavedDialog(true));
      expect(useUIStore.getState().showUnsavedDialog).toBe(true);

      act(() => useUIStore.getState().setShowUnsavedDialog(false));
      expect(useUIStore.getState().showUnsavedDialog).toBe(false);
    });
  });

  describe('reset', () => {
    it('should reset all state to initial values', () => {
      act(() => {
        useUIStore.getState().openAppSelector('trigger', 'node-1');
        useUIStore.getState().openDataFieldsModal('action', 'wordpress/send_email');
        useUIStore.getState().requestDeleteNode('node-2', 'Test');
        useUIStore.getState().setShowUnsavedDialog(true);
      });

      act(() => useUIStore.getState().reset());

      const state = useUIStore.getState();
      expect(state.showAppSelector).toBe(false);
      expect(state.appSelectorType).toBe('action');
      expect(state.selectedPlaceholderId).toBeNull();
      expect(state.dataFieldsModal).toBeNull();
      expect(state.nodeToDelete).toBeNull();
      expect(state.showUnsavedDialog).toBe(false);
    });
  });
});
