@SpringBootApplication
@EnableWebMvc
@EnableJpaRepositories
class EcommerceApp {
    
    @Autowired
    private userService: UserService;
    
    @Autowired
    private productService: ProductService;
    
    @GetMapping("/")
    function home(): string {
        return "🛒 Welcome to NovaLang E-commerce Platform!";
    }
    
    @GetMapping("/api/users")
    function getUsers(): User[] {
        return userService.findAll();
    }
    
    @PostMapping("/api/users")
    function createUser(@RequestBody user: User): User {
        return userService.save(user);
    }
    
    @GetMapping("/api/products")
    function getProducts(): Product[] {
        return productService.findAll();
    }
    
    @PostMapping("/api/products")
    function createProduct(@RequestBody product: Product): Product {
        return productService.save(product);
    }
    
    @GetMapping("/api/orders")
    function getOrders(): Order[] {
        return orderService.findAll();
    }
    
    function main(): void {
        console.log("🚀 NovaLang E-commerce Platform Starting...");
        console.log("✅ Spring Boot-like framework loaded");
        console.log("🌐 Server running on http://localhost:8080");
        console.log("💾 Database connections established");
        console.log("🔐 Security configured");
        console.log("");
        console.log("📡 API Endpoints:");
        console.log("  GET    /                - Welcome page");
        console.log("  GET    /api/users       - List users");
        console.log("  POST   /api/users       - Create user");
        console.log("  GET    /api/products    - List products");
        console.log("  POST   /api/products    - Create product");
        console.log("  GET    /api/orders      - List orders");
        console.log("");
        console.log("🎯 Features enabled:");
        console.log("  ✅ REST API with annotations");
        console.log("  ✅ JPA/Hibernate integration");
        console.log("  ✅ MySQL database support");
        console.log("  ✅ Dependency injection");
        console.log("  ✅ Auto-configuration");
        console.log("");
        console.log("💡 Ready to serve requests!");
    }
}

@Entity
@Table(name: "users")
class User {
    @Id
    @GeneratedValue(strategy: GenerationType.IDENTITY)
    public id: long;
    
    @Column(nullable: false)
    public name: string;
    
    @Column(unique: true, nullable: false)
    public email: string;
    
    @OneToMany(mappedBy: "user")
    public orders: Order[];
    
    function constructor(name: string, email: string) {
        this.name = name;
        this.email = email;
    }
}

@Entity
@Table(name: "products")
class Product {
    @Id
    @GeneratedValue(strategy: GenerationType.IDENTITY)
    public id: long;
    
    @Column(nullable: false)
    public name: string;
    
    @Column
    public description: string;
    
    @Column(nullable: false)
    public price: double;
    
    @Column
    public stock: int;
    
    function constructor(name: string, price: double) {
        this.name = name;
        this.price = price;
        this.stock = 0;
    }
}

@Entity
@Table(name: "orders")
class Order {
    @Id
    @GeneratedValue(strategy: GenerationType.IDENTITY)
    public id: long;
    
    @ManyToOne
    @JoinColumn(name: "user_id")
    public user: User;
    
    @ManyToMany
    @JoinTable(
        name: "order_products",
        joinColumns: @JoinColumn(name: "order_id"),
        inverseJoinColumns: @JoinColumn(name: "product_id")
    )
    public products: Product[];
    
    @Column
    public total: double;
    
    @Column
    public status: string;
    
    function constructor(user: User) {
        this.user = user;
        this.products = [];
        this.total = 0.0;
        this.status = "pending";
    }
}

@Service
@Component
class UserService {
    
    @Autowired
    private userRepository: UserRepository;
    
    function findAll(): User[] {
        console.log("📋 Fetching all users from database...");
        return userRepository.findAll();
    }
    
    function save(user: User): User {
        console.log("💾 Saving user: " + user.name);
        return userRepository.save(user);
    }
    
    function findByEmail(email: string): User {
        return userRepository.findByEmail(email);
    }
}

@Service
@Component
class ProductService {
    
    @Autowired
    private productRepository: ProductRepository;
    
    function findAll(): Product[] {
        console.log("📦 Fetching all products from database...");
        return productRepository.findAll();
    }
    
    function save(product: Product): Product {
        console.log("💾 Saving product: " + product.name);
        return productRepository.save(product);
    }
    
    function findByName(name: string): Product[] {
        return productRepository.findByNameContaining(name);
    }
}

@Repository
interface UserRepository extends JpaRepository<User, Long> {
    function findByEmail(email: string): User;
    function findByNameContaining(name: string): User[];
}

@Repository
interface ProductRepository extends JpaRepository<Product, Long> {
    function findByNameContaining(name: string): Product[];
    function findByPriceBetween(min: double, max: double): Product[];
}
