/**
 * Unit Tests: Phone Validation
 * Tests all country code formats and validation logic
 */

import {
  validatePhoneNumber,
  normalizePhoneNumber,
} from "@/components/DemoScheduler/utils/validationHelpers";

describe("Phone Validation", () => {
  describe("validatePhoneNumber", () => {
    describe("Valid GCC Country Codes", () => {
      it("should accept +966 (Saudi Arabia) with valid number", () => {
        const result = validatePhoneNumber("+966501234567");
        expect(result.valid).toBe(true);
        expect(result.error).toBeUndefined();
      });

      it("should accept +970 (Palestine) with valid number", () => {
        const result = validatePhoneNumber("+970501234567");
        expect(result.valid).toBe(true);
        expect(result.error).toBeUndefined();
      });

      it("should accept +971 (UAE) with valid number", () => {
        const result = validatePhoneNumber("+971501234567");
        expect(result.valid).toBe(true);
        expect(result.error).toBeUndefined();
      });

      it("should accept +974 (Qatar) with valid number", () => {
        const result = validatePhoneNumber("+974501234567");
        expect(result.valid).toBe(true);
        expect(result.error).toBeUndefined();
      });

      it("should accept +975 with valid number", () => {
        const result = validatePhoneNumber("+975501234567");
        expect(result.valid).toBe(true);
        expect(result.error).toBeUndefined();
      });

      it("should accept +976 with valid number", () => {
        const result = validatePhoneNumber("+976501234567");
        expect(result.valid).toBe(true);
        expect(result.error).toBeUndefined();
      });

      it("should accept +977 with valid number", () => {
        const result = validatePhoneNumber("+977501234567");
        expect(result.valid).toBe(true);
        expect(result.error).toBeUndefined();
      });

      it("should accept +978 with valid number", () => {
        const result = validatePhoneNumber("+978501234567");
        expect(result.valid).toBe(true);
        expect(result.error).toBeUndefined();
      });

      it("should accept +979 with valid number", () => {
        const result = validatePhoneNumber("+979501234567");
        expect(result.valid).toBe(true);
        expect(result.error).toBeUndefined();
      });

      it("should accept +965 (Kuwait) with valid number", () => {
        const result = validatePhoneNumber("+965501234567");
        expect(result.valid).toBe(true);
        expect(result.error).toBeUndefined();
      });

      it("should accept +968 (Oman) with valid number", () => {
        const result = validatePhoneNumber("+968501234567");
        expect(result.valid).toBe(true);
        expect(result.error).toBeUndefined();
      });

      it("should accept +973 (Bahrain) with valid number", () => {
        const result = validatePhoneNumber("+973501234567");
        expect(result.valid).toBe(true);
        expect(result.error).toBeUndefined();
      });
    });

    describe("Phone Number Formats", () => {
      it("should accept phone without + prefix (digits only)", () => {
        const result = validatePhoneNumber("966501234567");
        expect(result.valid).toBe(true);
      });

      it("should accept phone with spaces", () => {
        const result = validatePhoneNumber("+966 50 123 4567");
        expect(result.valid).toBe(true);
      });

      it("should accept phone with dashes", () => {
        const result = validatePhoneNumber("+966-50-123-4567");
        expect(result.valid).toBe(true);
      });

      it("should accept phone with parentheses", () => {
        const result = validatePhoneNumber("+966 (50) 123-4567");
        expect(result.valid).toBe(true);
      });

      it("should accept mixed formatting", () => {
        const result = validatePhoneNumber("+966 (50) 123 4567");
        expect(result.valid).toBe(true);
      });
    });

    describe("Edge Cases", () => {
      it("should accept empty/undefined phone (optional field)", () => {
        const result1 = validatePhoneNumber("");
        const result2 = validatePhoneNumber(undefined);
        expect(result1.valid).toBe(true);
        expect(result2.valid).toBe(true);
      });

      it("should accept minimum length (6 digits)", () => {
        const result = validatePhoneNumber("+974123456");
        expect(result.valid).toBe(true);
      });

      it("should accept maximum length (15 digits)", () => {
        const result = validatePhoneNumber("+974123456789012");
        expect(result.valid).toBe(true);
      });

      it("should trim whitespace", () => {
        const result = validatePhoneNumber("  +966501234567  ");
        expect(result.valid).toBe(true);
      });
    });

    describe("Invalid Formats", () => {
      it("should reject phone with letters", () => {
        const result = validatePhoneNumber("+966abc123456");
        expect(result.valid).toBe(false);
        expect(result.error).toContain("only contain digits");
      });

      it("should reject phone with special characters", () => {
        const result = validatePhoneNumber("+966#501234567");
        expect(result.valid).toBe(false);
        expect(result.error).toContain("only contain digits");
      });

      it("should reject phone too short (< 6 digits)", () => {
        const result = validatePhoneNumber("+97412");
        expect(result.valid).toBe(false);
        expect(result.error).toContain("at least 6 digits");
      });

      it("should reject phone too long (> 15 digits)", () => {
        const result = validatePhoneNumber("+9741234567890123456");
        expect(result.valid).toBe(false);
        expect(result.error).toContain("too long");
      });
    });

    describe("International Numbers", () => {
      it("should accept US number (+1)", () => {
        const result = validatePhoneNumber("+15551234567");
        expect(result.valid).toBe(true);
      });

      it("should accept UK number (+44)", () => {
        const result = validatePhoneNumber("+447911123456");
        expect(result.valid).toBe(true);
      });

      it("should accept any valid international format", () => {
        const result = validatePhoneNumber("+81123456789");
        expect(result.valid).toBe(true);
      });
    });
  });

  describe("normalizePhoneNumber", () => {
    it("should remove parentheses from phone", () => {
      const normalized = normalizePhoneNumber("+966 (50) 123-4567");
      expect(normalized).toBe("+966 50 1234567");
    });

    it("should remove dashes but keep digits and +", () => {
      const normalized = normalizePhoneNumber("+966-50-123-4567");
      // normalizePhoneNumber removes dashes, so result won't have spaces
      expect(normalized).toBe("+966501234567");
    });

    it("should preserve + prefix", () => {
      const normalized = normalizePhoneNumber("+966501234567");
      expect(normalized).toContain("+");
    });

    it("should trim whitespace", () => {
      const normalized = normalizePhoneNumber("  +966 50 123 4567  ");
      expect(normalized).toBe("+966 50 123 4567");
    });

    it("should keep spaces between digit groups", () => {
      const normalized = normalizePhoneNumber("+966 50 123 4567");
      expect(normalized).toBe("+966 50 123 4567");
    });
  });
});
