import { When } from "../schema/extensions";
import { Interface } from "../schema/mode/interfaces/Interface";

console.log("🚀 TESTING MODULAR CONDITIONAL OPERATORS SYSTEM");
console.log("===============================================");

// Test schema with ALL supported operators using the new modular system
const ComprehensiveTestSchema = Interface({
  role: "admin|user|guest",
  accountType: "free|premium|enterprise",
  age: "int(13,120)",
  email: "email",

  // ✅ EQUALITY OPERATOR (= - Full IDE support)
  permissions_equals: "when role=admin *? string[] : string[]?",

  // ⚠️ INEQUALITY OPERATOR (!= - Runtime only, no IDE errors)
  paymentMethod_notEquals: "when accountType!=free *? string : string?",

  // ✅ NUMERIC COMPARISON OPERATORS (>, <, >=, <= - Should work with modular system)
  accessLevel_gt: "when age>18 *? string : string?",
  discountRate_lt: "when age<25 *? number(0,0.5) : number(0,0.2)",
  fullAccess_gte: "when age>=21 *? =yes : =no",
  seniorDiscount_lte: "when age<=65 *? =none : number?",

  // ✅ REGEX OPERATOR (~ - Should work with modular system)
  adminEmail_regex: "when email~^admin *? string : =none?",

  // ✅ EXISTENCE OPERATORS (.exists, .!exists - Should work with modular system)
  billingAddress_exists:
    "when paymentMethod_notEquals.exists *? string : string?",
  guestFeatures_notExists:
    "when paymentMethod_notEquals.!exists *? string[] : string[]?",

  // ✅ INCLUSION OPERATORS (.in(), .!in() - Should work with modular system)
  maxProjects_in: "when role.in(admin,user) *? int(1,) : int(1,3)",
  restrictions_notIn: "when role.!in(guest) *? string[] : string[]?",
});

console.log("\n1. Testing ALL operators with valid data:");
const validResult = ComprehensiveTestSchema.safeParse({
  role: "admin",
  accountType: "premium",
  age: 25,
  email: "admin@example.com",

  // Expected values based on conditions
  permissions_equals: ["read", "write", "delete"], // role=admin, so string[]
  paymentMethod_notEquals: "credit_card", // accountType!=free, so required
  accessLevel_gt: "full", // age>18, so required
  discountRate_lt: 0.15, // age<25, so 0-0.5 range (fixed: was 0.3 > 0.2 max)
  fullAccess_gte: "yes", // age>=21, so required (fixed: was boolean, should be string)
  seniorDiscount_lte: "none", // age<=65 but not senior, so optional (fixed: was undefined)
  adminEmail_regex: "admin_panel", // email~^admin, so required
  billingAddress_exists: "123 Admin St", // paymentMethod exists, so required
  guestFeatures_notExists: undefined, // paymentMethod exists, so optional
  maxProjects_in: 999, // role in [admin,user], so unlimited
  restrictions_notIn: ["none"], // role not in [guest], so required
});

console.log("Valid result success:", validResult.success);
if (!validResult.success) {
  console.log("❌ UNEXPECTED: Valid data failed validation!");
  console.log("Errors:", validResult.errors);
} else {
  console.log("✅ SUCCESS: All operators working correctly!");
  console.log("✅ Modular system successfully handles ALL operators!");
}

// // TypeScript knows the EXACT type based on conditions
// const adminUser = {
//   role: "admin" as const,
//   permissions: ["read", "write", "delete"], // ✅ TypeScript: string[]
//   maxProjects: 100, // ✅ TypeScript: number
//   paymentMethod: "credit_card", // ✅ TypeScript: string
// };

// const regularUser = {
//   role: "user" as const,
//   accountType: "free" as const,
//   maxProjects: 2, // ✅ TypeScript: number (1-3 range)
//   // permissions not allowed for regular users
//   // paymentMethod not required for free users
// };

// // TypeScript catches conditional type mismatches at compile time
// const invalidUser = {
//   role: "user" as const,
//   permissions: ["read", 2], // ❌ TypeScript ERROR: Type 'number' is not assignable to type 'string'
// };

// Compile-time safety prevents runtime errors
// UserSchema.safeParse({
//   id: 1,
//   name: "John",
//   invalidProp: "error", // ❌ TypeScript ERROR caught at compile time
// });
