/**
 * Form Values Consistency Tests
 *
 * These tests ensure that dropdown values in the signup company-info form
 * match the values in the profile settings form.
 *
 * This prevents the issue where data saved during signup doesn't display
 * correctly in the profile because of mismatched option values.
 */

// ─────────────────────────────────────────────────────────────
// Signup Form Values (from app/[lang]/signup/company-info/page.tsx)
// ─────────────────────────────────────────────────────────────

const SIGNUP_COMPANY_SIZES = [
  { value: "1-10", label: "1-10 employees" },
  { value: "11-50", label: "11-50 employees" },
  { value: "51-200", label: "51-200 employees" },
  { value: "200+", label: "200+ employees" },
];

const SIGNUP_GCC_COUNTRIES = [
  { value: "sa", label: "Saudi Arabia" },
  { value: "uae", label: "UAE" },
  { value: "qa", label: "Qatar" },
  { value: "kw", label: "Kuwait" },
  { value: "bh", label: "Bahrain" },
  { value: "om", label: "Oman" },
];

const SIGNUP_INDUSTRIES = [
  { value: "healthcare", label: "Healthcare" },
  { value: "dental", label: "Dental" },
  { value: "aesthetics", label: "Aesthetics" },
  { value: "medical-spas", label: "Medical Spas" },
  { value: "physio", label: "Physiotherapy" },
  { value: "labs-diagnostics", label: "Labs & Diagnostics" },
  { value: "telehealth", label: "Telehealth" },
  { value: "vet", label: "Veterinary" },
  { value: "beauty-wellness", label: "Beauty & Wellness" },
  { value: "food-hospitality", label: "Food & Hospitality" },
  { value: "restaurants-cafes", label: "Restaurants & Cafes" },
  { value: "hotels-accommodation", label: "Hotels & Accommodation" },
  { value: "mobility-industry", label: "Mobility Industry" },
  { value: "vehicle-rental", label: "Vehicle Rental" },
  { value: "education", label: "Education" },
  { value: "sports-academies", label: "Sports Academies" },
  { value: "events-venues", label: "Events & Venues" },
  { value: "property-facilities", label: "Property & Facilities" },
  { value: "home-services-trades", label: "Home Services & Trades" },
  { value: "pet-services", label: "Pet Services" },
  { value: "retail-services", label: "Retail Services" },
  { value: "public-sector", label: "Public Sector" },
  { value: "nonprofit-community", label: "Non-profit & Community" },
  { value: "other", label: "Other" },
];

// ─────────────────────────────────────────────────────────────
// Profile Form Values (from components/dashboard/settings/ProfileTab.tsx)
// ─────────────────────────────────────────────────────────────

const PROFILE_COMPANY_SIZES = [
  { value: "1-10", label: "1-10 employees" },
  { value: "11-50", label: "11-50 employees" },
  { value: "51-200", label: "51-200 employees" },
  { value: "200+", label: "200+ employees" },
];

const PROFILE_COUNTRIES = [
  { value: "sa", label: "Saudi Arabia" },
  { value: "uae", label: "UAE" },
  { value: "qa", label: "Qatar" },
  { value: "kw", label: "Kuwait" },
  { value: "bh", label: "Bahrain" },
  { value: "om", label: "Oman" },
];

const PROFILE_INDUSTRIES = [
  { value: "healthcare", label: "Healthcare" },
  { value: "dental", label: "Dental" },
  { value: "aesthetics", label: "Aesthetics" },
  { value: "medical-spas", label: "Medical Spas" },
  { value: "physio", label: "Physiotherapy" },
  { value: "labs-diagnostics", label: "Labs & Diagnostics" },
  { value: "telehealth", label: "Telehealth" },
  { value: "vet", label: "Veterinary" },
  { value: "beauty-wellness", label: "Beauty & Wellness" },
  { value: "food-hospitality", label: "Food & Hospitality" },
  { value: "restaurants-cafes", label: "Restaurants & Cafes" },
  { value: "hotels-accommodation", label: "Hotels & Accommodation" },
  { value: "mobility-industry", label: "Mobility Industry" },
  { value: "vehicle-rental", label: "Vehicle Rental" },
  { value: "education", label: "Education" },
  { value: "sports-academies", label: "Sports Academies" },
  { value: "events-venues", label: "Events & Venues" },
  { value: "property-facilities", label: "Property & Facilities" },
  { value: "home-services-trades", label: "Home Services & Trades" },
  { value: "pet-services", label: "Pet Services" },
  { value: "retail-services", label: "Retail Services" },
  { value: "public-sector", label: "Public Sector" },
  { value: "nonprofit-community", label: "Non-profit & Community" },
  { value: "other", label: "Other" },
];

// ─────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────

describe("Form Values Consistency", () => {
  describe("Company Size Values", () => {
    it("should have matching values between signup and profile forms", () => {
      const signupValues = SIGNUP_COMPANY_SIZES.map((s) => s.value).sort();
      const profileValues = PROFILE_COMPANY_SIZES.map((s) => s.value).sort();

      expect(signupValues).toEqual(profileValues);
    });

    it("should have same number of options", () => {
      expect(SIGNUP_COMPANY_SIZES.length).toBe(PROFILE_COMPANY_SIZES.length);
    });

    it("every signup company size value should exist in profile form", () => {
      const profileValues = new Set(PROFILE_COMPANY_SIZES.map((s) => s.value));

      for (const size of SIGNUP_COMPANY_SIZES) {
        expect(profileValues.has(size.value)).toBe(true);
      }
    });
  });

  describe("Country Values", () => {
    it("should have matching values between signup and profile forms", () => {
      const signupValues = SIGNUP_GCC_COUNTRIES.map((c) => c.value).sort();
      const profileValues = PROFILE_COUNTRIES.map((c) => c.value).sort();

      expect(signupValues).toEqual(profileValues);
    });

    it("should have same number of options", () => {
      expect(SIGNUP_GCC_COUNTRIES.length).toBe(PROFILE_COUNTRIES.length);
    });

    it("every signup country value should exist in profile form", () => {
      const profileValues = new Set(PROFILE_COUNTRIES.map((c) => c.value));

      for (const country of SIGNUP_GCC_COUNTRIES) {
        expect(profileValues.has(country.value)).toBe(true);
      }
    });

    it("country values should be lowercase codes, not full names", () => {
      for (const country of SIGNUP_GCC_COUNTRIES) {
        expect(country.value).toBe(country.value.toLowerCase());
        expect(country.value.length).toBeLessThanOrEqual(3);
      }

      for (const country of PROFILE_COUNTRIES) {
        expect(country.value).toBe(country.value.toLowerCase());
        expect(country.value.length).toBeLessThanOrEqual(3);
      }
    });
  });

  describe("Industry Values", () => {
    it("should have matching values between signup and profile forms", () => {
      const signupValues = SIGNUP_INDUSTRIES.map((i) => i.value).sort();
      const profileValues = PROFILE_INDUSTRIES.map((i) => i.value).sort();

      expect(signupValues).toEqual(profileValues);
    });

    it("should have same number of options", () => {
      expect(SIGNUP_INDUSTRIES.length).toBe(PROFILE_INDUSTRIES.length);
    });

    it("every signup industry value should exist in profile form", () => {
      const profileValues = new Set(PROFILE_INDUSTRIES.map((i) => i.value));

      for (const industry of SIGNUP_INDUSTRIES) {
        expect(profileValues.has(industry.value)).toBe(true);
      }
    });

    it("industry values should be kebab-case identifiers", () => {
      const kebabCaseRegex = /^[a-z]+(-[a-z]+)*$/;

      for (const industry of SIGNUP_INDUSTRIES) {
        expect(industry.value).toMatch(kebabCaseRegex);
      }

      for (const industry of PROFILE_INDUSTRIES) {
        expect(industry.value).toMatch(kebabCaseRegex);
      }
    });
  });

  describe("Data Flow Validation", () => {
    it("should handle all possible signup values in profile display", () => {
      // Simulate saving each signup value and checking if profile can display it
      const testCases = [
        {
          field: "companySize",
          signupValues: SIGNUP_COMPANY_SIZES,
          profileValues: PROFILE_COMPANY_SIZES,
        },
        {
          field: "country",
          signupValues: SIGNUP_GCC_COUNTRIES,
          profileValues: PROFILE_COUNTRIES,
        },
        {
          field: "industry",
          signupValues: SIGNUP_INDUSTRIES,
          profileValues: PROFILE_INDUSTRIES,
        },
      ];

      for (const testCase of testCases) {
        const profileValueSet = new Set(
          testCase.profileValues.map((v) => v.value),
        );

        for (const signupOption of testCase.signupValues) {
          const savedValue = signupOption.value;
          const canDisplayInProfile = profileValueSet.has(savedValue);

          expect(canDisplayInProfile).toBe(true);
        }
      }
    });

    it("should not have any orphaned values in either form", () => {
      // Check company size
      const signupSizeValues = new Set(
        SIGNUP_COMPANY_SIZES.map((s) => s.value),
      );
      const profileSizeValues = new Set(
        PROFILE_COMPANY_SIZES.map((s) => s.value),
      );

      for (const value of signupSizeValues) {
        expect(profileSizeValues.has(value)).toBe(true);
      }
      for (const value of profileSizeValues) {
        expect(signupSizeValues.has(value)).toBe(true);
      }

      // Check country
      const signupCountryValues = new Set(
        SIGNUP_GCC_COUNTRIES.map((c) => c.value),
      );
      const profileCountryValues = new Set(
        PROFILE_COUNTRIES.map((c) => c.value),
      );

      for (const value of signupCountryValues) {
        expect(profileCountryValues.has(value)).toBe(true);
      }
      for (const value of profileCountryValues) {
        expect(signupCountryValues.has(value)).toBe(true);
      }

      // Check industry
      const signupIndustryValues = new Set(
        SIGNUP_INDUSTRIES.map((i) => i.value),
      );
      const profileIndustryValues = new Set(
        PROFILE_INDUSTRIES.map((i) => i.value),
      );

      for (const value of signupIndustryValues) {
        expect(profileIndustryValues.has(value)).toBe(true);
      }
      for (const value of profileIndustryValues) {
        expect(signupIndustryValues.has(value)).toBe(true);
      }
    });
  });
});

describe("Database Schema Compatibility", () => {
  it("company size values should be valid for database storage", () => {
    const maxLength = 20; // Reasonable max length for companySize field

    for (const size of SIGNUP_COMPANY_SIZES) {
      expect(size.value.length).toBeLessThanOrEqual(maxLength);
    }
  });

  it("country values should be valid for database storage", () => {
    const maxLength = 10; // Country codes should be short

    for (const country of SIGNUP_GCC_COUNTRIES) {
      expect(country.value.length).toBeLessThanOrEqual(maxLength);
    }
  });

  it("industry values should be valid for database storage", () => {
    const maxLength = 50; // Reasonable max length for industry field

    for (const industry of SIGNUP_INDUSTRIES) {
      expect(industry.value.length).toBeLessThanOrEqual(maxLength);
    }
  });
});

describe("Regression Prevention", () => {
  it('should prevent the "Select..." display bug', () => {
    // This test documents the bug that was fixed:
    // When a user signed up with country='qa', the profile showed "Select country"
    // because the profile form expected 'Qatar' instead of 'qa'

    const problematicValues = [
      { field: "country", savedValue: "qa", expectedToWork: true },
      { field: "country", savedValue: "uae", expectedToWork: true },
      { field: "country", savedValue: "sa", expectedToWork: true },
      {
        field: "industry",
        savedValue: "beauty-wellness",
        expectedToWork: true,
      },
      { field: "industry", savedValue: "healthcare", expectedToWork: true },
      { field: "companySize", savedValue: "200+", expectedToWork: true },
      { field: "companySize", savedValue: "1-10", expectedToWork: true },
    ];

    for (const testCase of problematicValues) {
      let profileOptions: { value: string }[];

      switch (testCase.field) {
        case "country":
          profileOptions = PROFILE_COUNTRIES;
          break;
        case "industry":
          profileOptions = PROFILE_INDUSTRIES;
          break;
        case "companySize":
          profileOptions = PROFILE_COMPANY_SIZES;
          break;
        default:
          throw new Error(`Unknown field: ${testCase.field}`);
      }

      const valueExistsInProfile = profileOptions.some(
        (opt) => opt.value === testCase.savedValue,
      );
      expect(valueExistsInProfile).toBe(testCase.expectedToWork);
    }
  });

  it("should NOT have old incorrect values that caused the bug", () => {
    // These are the OLD values that caused the mismatch bug
    const incorrectOldValues = {
      country: ["Qatar", "UAE", "Saudi Arabia", "Kuwait", "Bahrain", "Oman"], // Full names instead of codes
      companySize: ["201-500", "500+"], // Different ranges than signup
      industry: ["beauty", "hospitality", "retail", "services"], // Simplified values
    };

    // Ensure none of these old incorrect values exist in profile form
    const profileCountryValues = PROFILE_COUNTRIES.map((c) => c.value);
    for (const oldValue of incorrectOldValues.country) {
      expect(profileCountryValues).not.toContain(oldValue);
    }

    const profileSizeValues = PROFILE_COMPANY_SIZES.map((s) => s.value);
    for (const oldValue of incorrectOldValues.companySize) {
      expect(profileSizeValues).not.toContain(oldValue);
    }

    // Note: 'other' is valid, so we only check for the ones that were wrong
    const profileIndustryValues = PROFILE_INDUSTRIES.map((i) => i.value);
    expect(profileIndustryValues).not.toContain("beauty"); // Should be 'beauty-wellness'
    expect(profileIndustryValues).not.toContain("hospitality"); // Should be specific like 'food-hospitality'
  });
});
