import { Interface, Make } from "../schema/mode/interfaces/Interface";

console.log("🚀 RUNTIME TYPE-TO-SCHEMA CONVERSION TEST");
console.log("=========================================\n");

// Sample data for testing
const sampleUser = {
  id: 1,
  name: "John Doe",
  email: "john@example.com",
  age: 30,
  tags: ["developer", "typescript"],
  metadata: { source: "registration", level: "premium" },
  isActive: true,
  website: "https://johndoe.com",
  uuid: "123e4567-e89b-12d3-a456-426614174000"
};

const sampleProduct = {
  id: "prod-123",
  name: "Laptop",
  price: 999.99,
  categories: ["electronics", "computers"],
  attributes: { brand: "TechCorp", model: "Pro-X1", warranty: "2 years" },
  inStock: true
};

console.log("1. Make.fromSample() - Pure Runtime Type Inference:");
console.log("==================================================");

const UserSchemaFromSample = Interface(Make.fromSample(sampleUser));
console.log("✅ User schema from sample created successfully!");

const userResult = UserSchemaFromSample.safeParse(sampleUser);
console.log("✅ User validation result:", userResult.success);

if (userResult.success && userResult.data) {
  console.log("   Auto-detected types:");
  console.log("   - ID:", typeof userResult.data.id, "(detected as positive integer)");
  console.log("   - Email:", userResult.data.email, "(detected as email format)");
  console.log("   - Website:", userResult.data.website, "(detected as URL format)");
  console.log("   - UUID:", userResult.data.uuid, "(detected as UUID format)");
  console.log("   - Tags:", userResult.data.tags, "(detected as string array)");
  console.log("   - Metadata:", userResult.data.metadata, "(detected as record)");
}

console.log("\n2. Make.fromInterface() with Sample Data:");
console.log("=========================================");

const ProductSchemaFromInterface = Interface(Make.fromInterface(sampleProduct));
console.log("✅ Product schema from interface created successfully!");

const productResult = ProductSchemaFromInterface.safeParse(sampleProduct);
console.log("✅ Product validation result:", productResult.success);

if (productResult.success && productResult.data) {
  console.log("   Product data validated:");
  console.log("   - ID:", productResult.data.id);
  console.log("   - Price:", productResult.data.price);
  console.log("   - Attributes:", productResult.data.attributes);
}

console.log("\n3. Make.fromInterface() with Type Hints:");
console.log("========================================");

// Override auto-detection with manual hints
const EnhancedUserSchema = Interface(Make.fromInterface(sampleUser, {
  id: "number",        // Override "positive" with generic "number"
  email: "string",     // Override "email" with generic "string"
  age: "int(18,120)",  // Add constraints
  website: "string"    // Override "url" with generic "string"
}));

console.log("✅ Enhanced user schema with hints created successfully!");

const enhancedResult = EnhancedUserSchema.safeParse(sampleUser);
console.log("✅ Enhanced validation result:", enhancedResult.success);

console.log("\n4. Make.fromType() with Runtime Samples:");
console.log("========================================");

// Individual property type inference
const MixedSchema = Interface({
  // Runtime type inference from samples
  positiveInt: Make.fromType(42),                    // Detects "positive"
  regularInt: Make.fromType(-5),                     // Detects "int"
  floatNum: Make.fromType(3.14),                     // Detects "number"
  emailStr: Make.fromType("test@example.com"),       // Detects "email"
  urlStr: Make.fromType("https://example.com"),      // Detects "url"
  uuidStr: Make.fromType("123e4567-e89b-12d3-a456-426614174000"), // Detects "uuid"
  regularStr: Make.fromType("hello world"),          // Detects "string"
  boolVal: Make.fromType(true),                      // Detects "boolean"
  dateVal: Make.fromType(new Date()),                // Detects "date"
  arrayVal: Make.fromType(["a", "b", "c"]),         // Detects "string[]"
  recordVal: Make.fromType({ key1: "val1", key2: "val2" }) // Detects "record<string,string>"
});

console.log("✅ Mixed schema with runtime type inference created!");

const mixedData = {
  positiveInt: 100,
  regularInt: -10,
  floatNum: 2.71,
  emailStr: "user@test.com",
  urlStr: "https://test.com",
  uuidStr: "987fcdeb-51d2-43a1-b456-426614174000",
  regularStr: "test string",
  boolVal: false,
  dateVal: new Date(),
  arrayVal: ["x", "y", "z"],
  recordVal: { foo: "bar", baz: "qux" }
};

const mixedResult = MixedSchema.safeParse(mixedData);
console.log("✅ Mixed validation result:", mixedResult.success);

console.log("\n5. Error Testing - Invalid Data:");
console.log("================================");

const invalidData = {
  id: "not-a-number",     // Should fail positive integer validation
  email: "invalid-email", // Should fail email validation
  tags: "not-an-array"    // Should fail array validation
};

const errorResult = UserSchemaFromSample.safeParse(invalidData);
console.log("❌ Invalid data validation result:", errorResult.success);
if (!errorResult.success) {
  console.log("   Validation errors:");
  errorResult.errors.forEach(error => console.log("   -", error));
}

console.log("\n🎉 RUNTIME CONVERSION SUMMARY:");
console.log("==============================");
console.log("✅ Make.fromSample() - Automatic schema generation from sample data");
console.log("✅ Make.fromInterface() - Enhanced interface conversion with optional hints");
console.log("✅ Make.fromType() - Individual property type inference from samples");
console.log("✅ Smart format detection - email, URL, UUID, positive integers, etc.");
console.log("✅ Record type detection - automatic Record<string, T> inference");
console.log("✅ Array type detection - automatic T[] inference");
console.log("✅ Type hint override system - manual control when needed");
console.log("🚀 REVOLUTIONARY: Zero-configuration schema generation from real data!");
