// 🚀 NovaLang Spring Boot Style Backend
// Auto-Configuration with Entities, Repositories, Services & Controllers

print "🌟 NovaLang Spring Boot Style Backend Starting...";
print "=================================================";

// ===== AUTO-CONFIGURATION =====
print "\n⚙️ Auto-Configuration Starting...";

// Database auto-configuration (like Spring Boot)
let db_auto_config = {
    "host": "localhost",
    "port": 3306,
    "database": "novalang_app",
    "username": "root",
    "password": "root",
    "auto_create_db": true,
    "auto_create_tables": true,
    "show_sql": true,
    "dialect": "mysql"
};

function auto_configure_database() {
    print "🔧 Auto-configuring database connection...";
    print "📍 Host: " + db_auto_config.host + ":" + str(db_auto_config.port);
    print "🗄️ Database: " + db_auto_config.database;
    
    if (db_auto_config.auto_create_db) {
        print "🏗️ Auto-creating database: " + db_auto_config.database;
        // In real implementation: CREATE DATABASE IF NOT EXISTS
        print "✅ Database created automatically";
    }
    
    print "✅ Database connection auto-configured";
    return true;
}

// ===== ENTITY DEFINITIONS (Like JPA @Entity) =====
print "\n📋 Loading Entity Definitions...";

// @Entity
// @Table(name = "users")
function User(id, name, email, created_at) {
    return {
        "id": id,
        "name": name,
        "email": email,
        "created_at": created_at,
        "_entity": "User",
        "_table": "users"
    };
}

// @Entity  
// @Table(name = "products")
function Product(id, name, price, category, stock) {
    return {
        "id": id,
        "name": name,
        "price": price,
        "category": category,
        "stock": stock,
        "_entity": "Product",
        "_table": "products"
    };
}

// @Entity
// @Table(name = "orders")
function Order(id, user_id, product_id, quantity, total, status) {
    return {
        "id": id,
        "user_id": user_id,
        "product_id": product_id,
        "quantity": quantity,
        "total": total,
        "status": status,
        "_entity": "Order", 
        "_table": "orders"
    };
}

function auto_create_tables() {
    print "🏗️ Auto-creating database tables...";
    
    // Auto-create Users table
    print "📋 Creating table: users";
    print "   - id (PRIMARY KEY, AUTO_INCREMENT)";
    print "   - name (VARCHAR(100) NOT NULL)";
    print "   - email (VARCHAR(255) UNIQUE NOT NULL)";
    print "   - created_at (TIMESTAMP DEFAULT CURRENT_TIMESTAMP)";
    
    // Auto-create Products table
    print "📦 Creating table: products";
    print "   - id (PRIMARY KEY, AUTO_INCREMENT)";
    print "   - name (VARCHAR(100) NOT NULL)";
    print "   - price (DECIMAL(10,2) NOT NULL)";
    print "   - category (VARCHAR(50))";
    print "   - stock (INT DEFAULT 0)";
    
    // Auto-create Orders table
    print "🛒 Creating table: orders";
    print "   - id (PRIMARY KEY, AUTO_INCREMENT)";
    print "   - user_id (INT, FOREIGN KEY)";
    print "   - product_id (INT, FOREIGN KEY)";
    print "   - quantity (INT NOT NULL)";
    print "   - total (DECIMAL(10,2) NOT NULL)";
    print "   - status (VARCHAR(20) DEFAULT 'pending')";
    
    print "✅ All tables created automatically";
    return true;
}

// ===== REPOSITORIES (Like Spring Data JPA) =====
print "\n💾 Initializing Repositories...";

// @Repository
// interface UserRepository extends JpaRepository<User, Long>
let UserRepository = {
    "save": function(user) {
        print "💾 UserRepository.save() - Saving user: " + user.name;
        // Auto-generate SQL: INSERT INTO users (name, email) VALUES (?, ?)
        print "📝 SQL: INSERT INTO users (name, email) VALUES ('" + user.name + "', '" + user.email + "')";
        user.id = 1; // Simulate auto-generated ID
        print "✅ User saved with ID: " + str(user.id);
        return user;
    },
    
    "findById": function(id) {
        print "🔍 UserRepository.findById(" + str(id) + ")";
        // Auto-generate SQL: SELECT * FROM users WHERE id = ?
        print "📝 SQL: SELECT * FROM users WHERE id = " + str(id);
        let user = User(id, "Sample User", "user@example.com", "2024-01-01");
        print "✅ User found: " + user.name;
        return user;
    },
    
    "findAll": function() {
        print "📋 UserRepository.findAll()";
        // Auto-generate SQL: SELECT * FROM users
        print "📝 SQL: SELECT * FROM users";
        print "✅ Found 3 users";
        return 3;
    },
    
    "findByEmail": function(email) {
        print "🔍 UserRepository.findByEmail('" + email + "')";
        // Auto-generate SQL: SELECT * FROM users WHERE email = ?
        print "📝 SQL: SELECT * FROM users WHERE email = '" + email + "'";
        let user = User(1, "Found User", email, "2024-01-01");
        print "✅ User found by email";
        return user;
    },
    
    "deleteById": function(id) {
        print "🗑️ UserRepository.deleteById(" + str(id) + ")";
        // Auto-generate SQL: DELETE FROM users WHERE id = ?
        print "📝 SQL: DELETE FROM users WHERE id = " + str(id);
        print "✅ User deleted";
        return true;
    }
};

// @Repository
let ProductRepository = {
    "save": function(product) {
        print "💾 ProductRepository.save() - Saving product: " + product.name;
        print "📝 SQL: INSERT INTO products (name, price, category, stock) VALUES (...)";
        product.id = 1;
        print "✅ Product saved with ID: " + str(product.id);
        return product;
    },
    
    "findAll": function() {
        print "📋 ProductRepository.findAll()";
        print "📝 SQL: SELECT * FROM products";
        print "✅ Found 5 products";
        return 5;
    },
    
    "findByCategory": function(category) {
        print "🔍 ProductRepository.findByCategory('" + category + "')";
        print "📝 SQL: SELECT * FROM products WHERE category = '" + category + "'";
        print "✅ Found products in category: " + category;
        return 2;
    }
};

// @Repository
let OrderRepository = {
    "save": function(order) {
        print "💾 OrderRepository.save() - Creating order";
        print "📝 SQL: INSERT INTO orders (user_id, product_id, quantity, total, status) VALUES (...)";
        order.id = 1;
        print "✅ Order created with ID: " + str(order.id);
        return order;
    },
    
    "findByUserId": function(user_id) {
        print "🔍 OrderRepository.findByUserId(" + str(user_id) + ")";
        print "📝 SQL: SELECT * FROM orders WHERE user_id = " + str(user_id);
        print "✅ Found user orders";
        return 2;
    }
};

// ===== SERVICES (Like @Service) =====
print "\n⚙️ Initializing Services...";

// @Service
let UserService = {
    "createUser": function(name, email) {
        print "🔄 UserService.createUser()";
        
        // Business logic validation
        if (len(name) < 2) {
            print "❌ Validation error: Name too short";
            return false;
        }
        
        // Check if email exists
        print "🔍 Checking email uniqueness...";
        
        // Create new user
        let user = User(0, name, email, "2024-01-01");
        let saved_user = UserRepository.save(user);
        
        // Send welcome email (business logic)
        print "📧 Sending welcome email to: " + email;
        
        print "✅ User creation completed";
        return saved_user;
    },
    
    "getUserById": function(id) {
        print "🔍 UserService.getUserById(" + str(id) + ")";
        return UserRepository.findById(id);
    },
    
    "getAllUsers": function() {
        print "📋 UserService.getAllUsers()";
        return UserRepository.findAll();
    },
    
    "updateUser": function(id, name, email) {
        print "🔄 UserService.updateUser(" + str(id) + ")";
        
        // Validation
        if (len(name) < 2) {
            print "❌ Validation error: Name too short";
            return false;
        }
        
        // Update logic
        let user = User(id, name, email, "2024-01-01");
        return UserRepository.save(user);
    },
    
    "deleteUser": function(id) {
        print "🗑️ UserService.deleteUser(" + str(id) + ")";
        return UserRepository.deleteById(id);
    }
};

// @Service
let ProductService = {
    "createProduct": function(name, price, category) {
        print "🔄 ProductService.createProduct()";
        
        if (price <= 0) {
            print "❌ Validation error: Price must be positive";
            return false;
        }
        
        let product = Product(0, name, price, category, 0);
        return ProductRepository.save(product);
    },
    
    "getAllProducts": function() {
        print "📋 ProductService.getAllProducts()";
        return ProductRepository.findAll();
    },
    
    "getProductsByCategory": function(category) {
        print "🔍 ProductService.getProductsByCategory('" + category + "')";
        return ProductRepository.findByCategory(category);
    }
};

// @Service  
let OrderService = {
    "createOrder": function(user_id, product_id, quantity) {
        print "🔄 OrderService.createOrder()";
        
        // Business logic: calculate total
        let price = 29.99; // In real app, get from ProductService
        let total = price * quantity;
        
        let order = Order(0, user_id, product_id, quantity, total, "pending");
        let saved_order = OrderRepository.save(order);
        
        // Business logic: update stock, send notification
        print "📧 Order confirmation sent";
        
        return saved_order;
    },
    
    "getOrdersByUser": function(user_id) {
        print "🔍 OrderService.getOrdersByUser(" + str(user_id) + ")";
        return OrderRepository.findByUserId(user_id);
    }
};

// ===== CONTROLLERS (Like @RestController) =====
print "\n🌐 Initializing REST Controllers...";

// @RestController
// @RequestMapping("/api/users")
let UserController = {
    "createUser": function(name, email) {
        print "🌐 POST /api/users";
        print "📥 Request Body: {name: '" + name + "', email: '" + email + "'}";
        
        let result = UserService.createUser(name, email);
        
        if (result) {
            print "✅ HTTP 201 CREATED - User created successfully";
            return 201;
        } else {
            print "❌ HTTP 400 BAD REQUEST - Validation failed";
            return 400;
        }
    },
    
    "getUser": function(id) {
        print "🌐 GET /api/users/" + str(id);
        
        let user = UserService.getUserById(id);
        
        if (user) {
            print "✅ HTTP 200 OK - User retrieved";
            return 200;
        } else {
            print "❌ HTTP 404 NOT FOUND - User not found";
            return 404;
        }
    },
    
    "getAllUsers": function() {
        print "🌐 GET /api/users";
        
        let count = UserService.getAllUsers();
        print "✅ HTTP 200 OK - " + str(count) + " users retrieved";
        return 200;
    },
    
    "updateUser": function(id, name, email) {
        print "🌐 PUT /api/users/" + str(id);
        print "📥 Request Body: {name: '" + name + "', email: '" + email + "'}";
        
        let result = UserService.updateUser(id, name, email);
        
        if (result) {
            print "✅ HTTP 200 OK - User updated";
            return 200;
        } else {
            print "❌ HTTP 400 BAD REQUEST - Update failed";
            return 400;
        }
    },
    
    "deleteUser": function(id) {
        print "🌐 DELETE /api/users/" + str(id);
        
        let result = UserService.deleteUser(id);
        
        if (result) {
            print "✅ HTTP 204 NO CONTENT - User deleted";
            return 204;
        } else {
            print "❌ HTTP 404 NOT FOUND - User not found";
            return 404;
        }
    }
};

// @RestController
// @RequestMapping("/api/products")
let ProductController = {
    "createProduct": function(name, price, category) {
        print "🌐 POST /api/products";
        print "📥 Creating product: " + name + " ($" + str(price) + ")";
        
        let result = ProductService.createProduct(name, price, category);
        
        if (result) {
            print "✅ HTTP 201 CREATED - Product created";
            return 201;
        } else {
            print "❌ HTTP 400 BAD REQUEST - Invalid product data";
            return 400;
        }
    },
    
    "getAllProducts": function() {
        print "🌐 GET /api/products";
        
        let count = ProductService.getAllProducts();
        print "✅ HTTP 200 OK - " + str(count) + " products retrieved";
        return 200;
    },
    
    "getProductsByCategory": function(category) {
        print "🌐 GET /api/products/category/" + category;
        
        let count = ProductService.getProductsByCategory(category);
        print "✅ HTTP 200 OK - " + str(count) + " products in " + category;
        return 200;
    }
};

// @RestController
// @RequestMapping("/api/orders")
let OrderController = {
    "createOrder": function(user_id, product_id, quantity) {
        print "🌐 POST /api/orders";
        print "📥 Order: User " + str(user_id) + " buying " + str(quantity) + " of product " + str(product_id);
        
        let result = OrderService.createOrder(user_id, product_id, quantity);
        
        if (result) {
            print "✅ HTTP 201 CREATED - Order placed successfully";
            return 201;
        } else {
            print "❌ HTTP 400 BAD REQUEST - Order failed";
            return 400;
        }
    },
    
    "getUserOrders": function(user_id) {
        print "🌐 GET /api/orders/user/" + str(user_id);
        
        let count = OrderService.getOrdersByUser(user_id);
        print "✅ HTTP 200 OK - " + str(count) + " orders found";
        return 200;
    }
};

// ===== APPLICATION STARTUP (Like @SpringBootApplication) =====
print "\n🚀 STARTING NOVALANG SPRING BOOT APPLICATION";
print "==============================================";

function main() {
    print "🌟 NovaLang Application Starting...";
    
    // Step 1: Auto-configuration
    print "\n1️⃣ Auto-Configuration:";
    let db_configured = auto_configure_database();
    
    // Step 2: Create database schema automatically
    print "\n2️⃣ Schema Auto-Creation:";
    let tables_created = auto_create_tables();
    
    // Step 3: Initialize repositories, services, controllers
    print "\n3️⃣ Component Initialization:";
    print "✅ Repositories initialized (UserRepository, ProductRepository, OrderRepository)";
    print "✅ Services initialized (UserService, ProductService, OrderService)";
    print "✅ Controllers initialized (UserController, ProductController, OrderController)";
    
    // Step 4: Application ready
    print "\n4️⃣ Application Ready:";
    print "✅ NovaLang Spring Boot Application started successfully!";
    print "🌐 Server running on http://localhost:8080";
    
    // ===== DEMO API CALLS =====
    print "\n🧪 RUNNING SPRING BOOT STYLE DEMO";
    print "==================================";
    
    // User management
    print "\n👥 User Management:";
    let create1 = UserController.createUser("John Doe", "john@company.com");
    let create2 = UserController.createUser("Jane Smith", "jane@company.com");
    let getAll = UserController.getAllUsers();
    let getUser = UserController.getUser(1);
    let updateUser = UserController.updateUser(1, "John Updated", "john.updated@company.com");
    
    // Product management
    print "\n📦 Product Management:";
    let createProd1 = ProductController.createProduct("Laptop", 999.99, "Electronics");
    let createProd2 = ProductController.createProduct("Coffee", 12.99, "Food");
    let getAllProds = ProductController.getAllProducts();
    let getElectronics = ProductController.getProductsByCategory("Electronics");
    
    // Order management
    print "\n🛒 Order Management:";
    let createOrder1 = OrderController.createOrder(1, 1, 2);
    let createOrder2 = OrderController.createOrder(2, 2, 1);
    let getUserOrders = OrderController.getUserOrders(1);
    
    print "\n🎉 SPRING BOOT STYLE DEMO COMPLETE!";
    print "====================================";
    
    print "\n📊 Enterprise Features:";
    print "   ✅ Auto-configuration (like Spring Boot)";
    print "   ✅ Entity definitions (like JPA @Entity)";
    print "   ✅ Repository pattern (like Spring Data)";
    print "   ✅ Service layer (like @Service)";
    print "   ✅ REST controllers (like @RestController)";
    print "   ✅ Automatic database creation";
    print "   ✅ Automatic table creation";
    print "   ✅ Dependency injection simulation";
    print "   ✅ HTTP status codes";
    print "   ✅ Business logic separation";
    
    print "\n🔗 Available REST Endpoints:";
    print "   👥 Users:";
    print "     • POST   /api/users           - Create user";
    print "     • GET    /api/users           - Get all users";
    print "     • GET    /api/users/{id}      - Get user by ID";
    print "     • PUT    /api/users/{id}      - Update user";
    print "     • DELETE /api/users/{id}      - Delete user";
    print "   📦 Products:";
    print "     • POST   /api/products        - Create product";
    print "     • GET    /api/products        - Get all products";
    print "     • GET    /api/products/category/{category} - Get by category";
    print "   🛒 Orders:";
    print "     • POST   /api/orders          - Create order";
    print "     • GET    /api/orders/user/{id} - Get user orders";
    
    print "\n🌍 Spring Boot Features Implemented:";
    print "   • Auto-configuration";
    print "   • Component scanning simulation";
    print "   • Dependency injection pattern";
    print "   • JPA-style repository pattern";
    print "   • Service layer pattern";
    print "   • REST controller pattern";
    print "   • Automatic schema generation";
    print "   • Clean architecture separation";
    
    print "\n✨ Your NovaLang Spring Boot application is running!";
    print "🚀 Just like Spring Boot - run once and everything works!";
}

// Start the application
main();
