/// <reference types="cypress" />
import { makeAdmin } from "../../support/admin-fixtures";

describe("admins management - filters", () => {
  beforeEach(() => {
    cy.setAuthCookies();
  });

  it("opens filter modal and filters by search term", () => {
    const defaultUsers = [
      makeAdmin({ full_name: "John Doe", username: "johndoe", email: "john@example.com" }, 1),
    ];
    const filteredUsers = [
      makeAdmin({ full_name: "Ali Reza", username: "alireza", email: "ali@example.com" }, 2),
    ];
    cy.intercept("GET", "**/api/proxy/users**", (req) => {
      const isFiltered = req.query.search === "Ali";
      req.alias = isFiltered ? "getAdminsFilteredBySearch" : "getAdminsDefault";
      const users = isFiltered ? filteredUsers : defaultUsers;
      req.reply({
        statusCode: 200,
        body: {
          success: true,
          message: "ok",
          data: { users },
          meta: { offset: 0, limit: 10, total: users.length },
        },
      });
    });

    cy.visit("/admins");
    cy.wait("@getAdminsDefault");
    cy.contains("John Doe").should("be.visible");
    cy.getByCy("admins-filter-button").click();
    cy.getByCy("admins-filter-form").should("be.visible");
    cy.getByCy("admins-filter-search-input").type("Ali");
    cy.getByCy("admins-filter-submit-button").click();

    cy.wait("@getAdminsFilteredBySearch");
    cy.contains("Ali Reza").should("be.visible");
    cy.contains("John Doe").should("not.exist");
  });

  it("filters by status and clear resets results", () => {
    const allUsers = [
      makeAdmin({ status: "active", full_name: "Active Admin" }, 1),
      makeAdmin({ status: "inactive", full_name: "Inactive Admin" }, 2),
    ];
    const inactiveUsers = [makeAdmin({ status: "inactive", full_name: "Inactive Admin" }, 3)];
    cy.intercept("GET", "**/api/proxy/users**", (req) => {
      const isInactiveFilter = req.query.status === "inactive";
      req.alias = isInactiveFilter
        ? "getAdminsFilteredByStatus"
        : "getAdminsDefaultByStatus";
      req.reply({
        statusCode: 200,
        body: {
          success: true,
          message: "ok",
          data: { users: isInactiveFilter ? inactiveUsers : allUsers },
          meta: { offset: 0, limit: 10, total: isInactiveFilter ? 1 : 2 },
        },
      });
    });

    cy.visit("/admins");
    cy.wait("@getAdminsDefaultByStatus");
    cy.getByCy("admins-filter-button").click();
    cy.getByCy("admins-filter-status-select").select("inactive");
    cy.getByCy("admins-filter-submit-button").click();

    cy.wait("@getAdminsFilteredByStatus");
    cy.location("search").should("contain", "status=inactive");
    cy.contains("Inactive Admin").should("be.visible");
    cy.contains("Active Admin").should("not.exist");

    cy.getByCy("admins-filter-button").click();
    cy.getByCy("admins-filter-status-select-clear").click();
    cy.getByCy("admins-filter-submit-button").click();
    cy.wait("@getAdminsDefaultByStatus");
    cy.location("search").should("not.contain", "status=inactive");
    cy.contains("Active Admin").should("be.visible");
  });
});
