describe("Products - Advanced Tests", () => {
  beforeEach(() => {
    cy.login();
  });

  describe("Product CRUD Operations", () => {
    it("should create a new product with all fields", () => {
      cy.visit("/products");
      cy.get(".bg-primary-600.rounded-full").first().click();

      // Fill basic product information
      cy.get('input[name="name"]').type("تست محصول جدید");
      cy.get('textarea[name="description"]').type("توضیحات کامل محصول تست");
      cy.get('input[name="design_style"]').type("مدرن");

      // Enable product
      cy.get('input[name="enabled"]').check({ force: true });

      // Set product type
      cy.get('select[name="type"]').select("0");

      // Submit form
      cy.get('button[type="submit"]').click();

      // Verify redirect and success message
      cy.url().should("include", "/products");
      cy.contains("تست محصول جدید").should("be.visible");
    });

    it("should edit an existing product", () => {
      cy.visit("/products");

      // Click edit on first product
      cy.get("tbody tr")
        .first()
        .within(() => {
          cy.get(
            '[data-testid="edit-button"], [title="ویرایش"], .text-blue-600'
          )
            .first()
            .click();
        });

      // Update product name
      cy.get('input[name="name"]').clear().type("محصول ویرایش شده");
      cy.get('button[type="submit"]').click();

      // Verify changes
      cy.url().should("include", "/products");
      cy.contains("محصول ویرایش شده").should("be.visible");
    });

    it("should delete a product with confirmation", () => {
      cy.visit("/products");

      // Click delete on first product
      cy.get("tbody tr")
        .first()
        .within(() => {
          cy.get('[data-testid="delete-button"], [title="حذف"], .text-red-600')
            .first()
            .click();
        });

      // Confirm deletion in modal
      cy.get('.modal, [role="dialog"]').should("be.visible");
      cy.get("button").contains("حذف").click();

      // Verify success message
      cy.contains("محصول با موفقیت حذف شد", { timeout: 10000 }).should(
        "be.visible"
      );
    });
  });

  describe("Product Form Validation", () => {
    beforeEach(() => {
      cy.visit("/products");
      cy.get(".bg-primary-600.rounded-full").first().click();
    });

    it("should show validation errors for empty required fields", () => {
      // Try to submit empty form
      cy.get('button[type="submit"]').click();

      // Check for validation messages
      cy.contains("نام محصول الزامی است", { timeout: 5000 }).should(
        "be.visible"
      );
    });

    it("should validate minimum length for product name", () => {
      cy.get('input[name="name"]').type("a");
      cy.get('button[type="submit"]').click();

      cy.contains("نام محصول باید حداقل", { timeout: 5000 }).should(
        "be.visible"
      );
    });
  });

  describe("Product Search and Filter", () => {
    beforeEach(() => {
      cy.visit("/products");
    });

    it("should search products by name", () => {
      cy.get('input[placeholder*="جستجو"], input[name="search"]').type("تست");
      cy.get('button[type="submit"], button').contains("جستجو").click();

      // Wait for results
      cy.wait(2000);

      // Check that search results contain the search term
      cy.get("tbody tr").should("have.length.at.least", 0);
    });

    it("should filter products by category", () => {
      cy.get('select[name="category_id"], select').first().select("1");
      cy.get("button").contains("اعمال فیلتر").click();

      cy.wait(2000);

      // Results should be filtered
      cy.get("tbody tr").should("have.length.at.least", 0);
    });
  });

  describe("Product Status Management", () => {
    beforeEach(() => {
      cy.visit("/products");
    });

    it("should toggle product status", () => {
      cy.get("tbody tr")
        .first()
        .within(() => {
          cy.get('input[type="checkbox"], .toggle')
            .first()
            .click({ force: true });
        });

      cy.contains("وضعیت محصول با موفقیت تغییر کرد").should("be.visible");
    });
  });
});
