/**
 * E2E tests for Home page management: searching and filtering tasks and notes.
 */

const mockUser = {
  userId: 1,
  name: 'Test User',
  email: 'test@example.com',
  admin: false,
  createdAt: '2026-01-01T00:00:00.000Z',
  gravatarImageUrl: '',
  lang: 'en'
};

const mockTasks = [
  {
    id: 1,
    description: 'Buy groceries',
    done: false,
    highPriority: true,
    dueDate: '2026-06-01',
    dueDateFmt: 'Jun 1, 2026',
    lastUpdate: '2026-05-01',
    tags: ['personal'],
    urls: []
  },
  {
    id: 2,
    description: 'Write report',
    done: false,
    highPriority: false,
    dueDate: '',
    dueDateFmt: '',
    lastUpdate: '2026-05-02',
    tags: ['work'],
    urls: []
  }
];

const mockNotes = [
  {
    id: 1,
    title: 'Meeting notes',
    description: 'Discuss quarterly goals',
    url: null,
    tags: ['work'],
    lastUpdate: '2026-05-01',
    shared: false,
    shareToken: null
  },
  {
    id: 2,
    title: 'Recipe',
    description: 'Pasta carbonara recipe',
    url: null,
    tags: ['personal'],
    lastUpdate: '2026-05-02',
    shared: false,
    shareToken: null
  }
];

const mockTags = ['personal', 'work'];

describe('Home Management', () => {
  beforeEach(() => {
    cy.intercept('GET', /\/rest\/user-sessions\/refresh/, {
      statusCode: 200,
      body: { token: 'fake-jwt-token', ...mockUser }
    }).as('refreshToken');

    cy.intercept('GET', /\/rest\/users\/me/, {
      statusCode: 200,
      body: mockUser
    }).as('getCurrentUser');

    cy.intercept('GET', /\/rest\/home\/tasks\/tags/, {
      statusCode: 200,
      body: mockTags
    }).as('getTags');

    cy.intercept('GET', /\/rest\/tasks$/, {
      statusCode: 200,
      body: mockTasks
    }).as('getTasks');

    cy.intercept('GET', /\/rest\/notes$/, {
      statusCode: 200,
      body: mockNotes
    }).as('getNotes');

    cy.visit('/home', {
      onBeforeLoad(win) {
        win.localStorage.setItem('TASKNOTE-TOKEN', 'fake-jwt-token');
        win.localStorage.setItem('TASKNOTE-USER', JSON.stringify(mockUser));
      }
    });
  });

  /**
   * Home page display
   */
  describe('Display', () => {
    it('shows tasks and notes after loading', () => {
      cy.wait('@getTasks');
      cy.wait('@getNotes');

      cy.contains('Buy groceries').should('be.visible');
      cy.contains('Write report').should('be.visible');
      cy.contains('Meeting notes').should('be.visible');
      cy.contains('Recipe').should('be.visible');
    });
  });

  /**
   * Search / text filtering
   */
  describe('Search', () => {
    beforeEach(() => {
      cy.wait('@getTasks');
      cy.wait('@getNotes');
    });

    it('filters items by search text matching a task description', () => {
      cy.get('input[placeholder="Filter tasks & notes"]').type('groceries');

      cy.contains('Buy groceries').should('be.visible');
      cy.contains('Write report').should('not.exist');
      cy.contains('Meeting notes').should('not.exist');
      cy.contains('Recipe').should('not.exist');
    });

    it('filters items by search text matching a note title', () => {
      cy.get('input[placeholder="Filter tasks & notes"]').type('Meeting');

      cy.contains('Meeting notes').should('be.visible');
      cy.contains('Buy groceries').should('not.exist');
      cy.contains('Recipe').should('not.exist');
    });

    it('restores all items after clearing the search', () => {
      cy.get('input[placeholder="Filter tasks & notes"]').type('groceries');
      cy.contains('Write report').should('not.exist');

      cy.get('input[placeholder="Filter tasks & notes"]').clear();

      cy.contains('Buy groceries').should('be.visible');
      cy.contains('Meeting notes').should('be.visible');
    });
  });

  /**
   * Dropdown filter
   */
  describe('Dropdown Filter', () => {
    beforeEach(() => {
      cy.wait('@getTasks');
      cy.wait('@getNotes');
    });

    it('shows only tasks when Tasks filter is selected', () => {
      cy.get('[data-testid="dropdown-tag-filter"]').click();
      cy.contains('Tasks').click();

      cy.contains('Buy groceries').should('be.visible');
      cy.contains('Write report').should('be.visible');
      cy.contains('Meeting notes').should('not.exist');
      cy.contains('Recipe').should('not.exist');
    });

    it('shows only notes when Notes filter is selected', () => {
      cy.get('[data-testid="dropdown-tag-filter"]').click();
      cy.contains('Notes').click();

      cy.contains('Meeting notes').should('be.visible');
      cy.contains('Recipe').should('be.visible');
      cy.contains('Buy groceries').should('not.exist');
      cy.contains('Write report').should('not.exist');
    });

    it('shows everything when Everything filter is selected', () => {
      cy.get('[data-testid="dropdown-tag-filter"]').click();
      cy.contains('Tasks').click();

      cy.get('[data-testid="dropdown-tag-filter"]').click();
      cy.contains('Everything').click();

      cy.contains('Buy groceries').should('be.visible');
      cy.contains('Meeting notes').should('be.visible');
    });

    it('filters by tag when a tag is selected', () => {
      cy.wait('@getTags');

      cy.get('[data-testid="dropdown-tag-filter"]').click();
      cy.contains('#personal').click();

      cy.contains('Buy groceries').should('be.visible');
      cy.contains('Recipe').should('be.visible');
      cy.contains('Write report').should('not.exist');
      cy.contains('Meeting notes').should('not.exist');
    });
  });
});
