{
    "models": [
        {
            "classname": "Customer",
            "code": "class Customer(Base):\n    __tablename__ = 'customer'\n\n    id = Column(Integer, primary_key=True, autoincrement=True)\n    name = Column(String, unique=True)\n    balance = Column(DECIMAL)\n    credit_limit = Column(DECIMAL)",
            "description": "Defines the Customer entity with a unique name, balance, and credit limit.",
            "name": "Customer"
        },
        {
            "classname": "Order",
            "code": "class Order(Base):\n    __tablename__ = 'order'\n\n    id = Column(Integer, primary_key=True, autoincrement=True)\n    notes = Column(String)\n    customer_id = Column(Integer, ForeignKey('customer.id'), nullable=False)\n    date_shipped = Column(Date)\n    amount_total = Column(DECIMAL)",
            "description": "Defines the Order entity which belongs to a customer. Includes notes and amount total.",
            "name": "Order"
        },
        {
            "classname": "Item",
            "code": "class Item(Base):\n    __tablename__ = 'item'\n\n    id = Column(Integer, primary_key=True, autoincrement=True)\n    order_id = Column(Integer, ForeignKey('order.id'))\n    product_id = Column(Integer, ForeignKey('product.id'), nullable=False)\n    quantity = Column(Integer, nullable=False)\n    amount = Column(DECIMAL)\n    unit_price = Column(DECIMAL)",
            "description": "Defines the Item entity with quantity, amounts, and unit price details.",
            "name": "Item"
        },
        {
            "classname": "Product",
            "code": "class Product(Base):\n    __tablename__ = 'product'\n\n    id = Column(Integer, primary_key=True, autoincrement=True)\n    name = Column(String, unique=True)\n    unit_price = Column(DECIMAL)",
            "description": "Defines the Product entity with a unique name and unit price.",
            "name": "Product"
        }
    ],
    "rules": [
        {
            "name": "Customer Balance Constraint",
            "description": "Ensure the customer's balance is less than their credit limit.",
            "use_case": "Check Credit",
            "entity": "Customer",
            "code": "Rule.constraint(validate=Customer, as_condition=lambda row: row.balance <= row.credit_limit, error_msg=\"Customer balance ({row.balance}) exceeds credit limit ({row.credit_limit})\")"
        },
        {
            "name": "Customer Balance Derivation",
            "description": "Derive the customer's balance as the sum of order totals where not yet shipped.",
            "use_case": "Check Credit",
            "entity": "Customer",
            "code": "Rule.sum(derive=Customer.balance, as_sum_of=Order.amount_total, where=lambda row: row.date_shipped is None)"
        },
        {
            "name": "Order Total Derivation",
            "description": "Derive the order's total amount from the sum of item amounts.",
            "use_case": "Check Credit",
            "entity": "Order",
            "code": "Rule.sum(derive=Order.amount_total, as_sum_of=Item.amount)"
        },
        {
            "name": "Item Amount Calculation",
            "description": "Calculate item amount based on quantity and unit price.",
            "use_case": "Check Credit",
            "entity": "Item",
            "code": "Rule.formula(derive=Item.amount, as_expression=lambda row: row.quantity * row.unit_price)"
        },
        {
            "name": "Item Unit Price Copy",
            "description": "Copy unit price from product to item.",
            "use_case": "Check Credit",
            "entity": "Item",
            "code": "Rule.copy(derive=Item.unit_price, from_parent=Product.unit_price)"
        },
        {
            "name": "Order Kafka Notification",
            "description": "Send order details to Kafka if order is shipped.",
            "use_case": "App Integration",
            "entity": "Order",
            "code": "Rule.after_flush_row_event(on_class=Order, calling=kafka_producer.send_row_to_kafka, if_condition=lambda row: row.date_shipped is not None, with_args={'topic': 'order_shipping'})"
        }
    ],
    "graphics": [
        {
            "sqlalchemy_query": "session.query(Product.name, func.sum(Item.amount).label('TotalSales')).join(Item, Product.id == Item.product_id).filter(Order.date_shipped.isnot(None)).group_by(Product.name).order_by(func.sum(Item.amount).desc())",
            "sql_query": "SELECT Product.name, SUM(Item.amount) AS TotalSales FROM Product JOIN Item ON Product.id = Item.product_id JOIN Order ON Item.order_id = Order.id WHERE Order.date_shipped IS NOT NULL GROUP BY Product.name ORDER BY TotalSales DESC",
            "classes_used": "Product, Item, Order",
            "class_x_axis": "Product",
            "name": "sales_by_product",
            "prompt": "Graph Sales by Product",
            "title": "Sales by Product",
            "xAxis": "Product",
            "yAxis": "Total Sales",
            "dashboard": true,
            "graph_type": "Bar",
            "html_code": "var data = result.result; var categories = data.map(item => item[0]); var totals = data.map(item => item[1]); var ctx = document.getElementById('salesByProductChart').getContext('2d'); new Chart(ctx, {type: 'bar', data: {labels: categories, datasets: [{label: 'Sales by Product', data: totals}]}});"
        },
        {
            "sqlalchemy_query": "session.query(Customer.name, func.sum(Order.amount_total).label('TotalCustomerSales')).join(Order, Customer.id == Order.customer_id).filter(Order.date_shipped.isnot(None)).group_by(Customer.name).order_by(func.sum(Order.amount_total).desc())",
            "sql_query": "SELECT Customer.name, SUM(Order.amount_total) AS TotalCustomerSales FROM Customer JOIN Order ON Customer.id = Order.customer_id WHERE Order.date_shipped IS NOT NULL GROUP BY Customer.name ORDER BY TotalCustomerSales DESC",
            "classes_used": "Customer, Order",
            "class_x_axis": "Customer",
            "name": "sales_by_customer",
            "prompt": "Graph Sales by Customer",
            "title": "Sales by Customer",
            "xAxis": "Customer",
            "yAxis": "Total Sales",
            "dashboard": true,
            "graph_type": "Bar",
            "html_code": "var data = result.result; var customers = data.map(item => item[0]); var sales = data.map(item => item[1]); var ctx = document.getElementById('salesByCustomerChart').getContext('2d'); new Chart(ctx, {type: 'bar', data: {labels: customers, datasets: [{label: 'Sales by Customer', data: sales}]}});"
        }
    ],
    "test_data": "This is the test data generated for Customers, Orders, Items, and Products in the new system.",
    "test_data_rows": [
        {
            "test_data_row_variable": "customer1",
            "code": "customer1 = Customer(name=\"Alice\", balance=1000.00, credit_limit=5000.00)"
        },
        {
            "test_data_row_variable": "customer2",
            "code": "customer2 = Customer(name=\"Bob\", balance=2000.00, credit_limit=3000.00)"
        },
        {
            "test_data_row_variable": "customer3",
            "code": "customer3 = Customer(name=\"Charlie\", balance=500.00, credit_limit=2000.00)"
        },
        {
            "test_data_row_variable": "customer4",
            "code": "customer4 = Customer(name=\"Diana\", balance=0.00, credit_limit=1000.00)"
        },
        {
            "test_data_row_variable": "product1",
            "code": "product1 = Product(name=\"Gadget\", unit_price=150.00)"
        },
        {
            "test_data_row_variable": "product2",
            "code": "product2 = Product(name=\"Widget\", unit_price=90.00)"
        },
        {
            "test_data_row_variable": "product3",
            "code": "product3 = Product(name=\"Thingamajig\", unit_price=75.00)"
        },
        {
            "test_data_row_variable": "product4",
            "code": "product4 = Product(name=\"Doodad\", unit_price=110.00)"
        },
        {
            "test_data_row_variable": "order1",
            "code": "order1 = Order(notes=\"First Order\", customer_id=1, date_shipped=date(2023, 3, 22), amount_total=300.00)"
        },
        {
            "test_data_row_variable": "order2",
            "code": "order2 = Order(notes=\"Second Order\", customer_id=2, date_shipped=None, amount_total=0.00)"
        },
        {
            "test_data_row_variable": "order3",
            "code": "order3 = Order(notes=\"Pending Shipment\", customer_id=3, date_shipped=None, amount_total=300.00)"
        },
        {
            "test_data_row_variable": "order4",
            "code": "order4 = Order(notes=\"Urgent Order\", customer_id=4, date_shipped=date(2023, 7, 15), amount_total=220.00)"
        },
        {
            "test_data_row_variable": "item1",
            "code": "item1 = Item(order_id=1, product_id=1, quantity=2, amount=300.00, unit_price=150.00)"
        },
        {
            "test_data_row_variable": "item2",
            "code": "item2 = Item(order_id=2, product_id=2, quantity=1, amount=90.00, unit_price=90.00)"
        },
        {
            "test_data_row_variable": "item3",
            "code": "item3 = Item(order_id=3, product_id=4, quantity=2, amount=220.00, unit_price=110.00)"
        },
        {
            "test_data_row_variable": "item4",
            "code": "item4 = Item(order_id=4, product_id=3, quantity=4, amount=300.00, unit_price=75.00)"
        }
    ],
    "test_data_sqlite": "INSERT INTO customer (name, balance, credit_limit) VALUES ('Alice', 1000.00, 5000.00);\nINSERT INTO customer (name, balance, credit_limit) VALUES ('Bob', 2000.00, 3000.00);\nINSERT INTO customer (name, balance, credit_limit) VALUES ('Charlie', 500.00, 2000.00);\nINSERT INTO customer (name, balance, credit_limit) VALUES ('Diana', 0.00, 1000.00);\n\nINSERT INTO product (name, unit_price) VALUES ('Gadget', 150.00);\nINSERT INTO product (name, unit_price) VALUES ('Widget', 90.00);\nINSERT INTO product (name, unit_price) VALUES ('Thingamajig', 75.00);\nINSERT INTO product (name, unit_price) VALUES ('Doodad', 110.00);\n\nINSERT INTO [order] (notes, customer_id, date_shipped, amount_total) VALUES ('First Order', 1, '2023-03-22', 300.00);\nINSERT INTO [order] (notes, customer_id, date_shipped, amount_total) VALUES ('Second Order', 2, NULL, 0.00);\nINSERT INTO [order] (notes, customer_id, date_shipped, amount_total) VALUES ('Pending Shipment', 3, NULL, 300.00);\nINSERT INTO [order] (notes, customer_id, date_shipped, amount_total) VALUES ('Urgent Order', 4, '2023-07-15', 220.00);\n\nINSERT INTO item (order_id, product_id, quantity, amount, unit_price) VALUES (1, 1, 2, 300.00, 150.00);\nINSERT INTO item (order_id, product_id, quantity, amount, unit_price) VALUES (2, 2, 1, 90.00, 90.00);\nINSERT INTO item (order_id, product_id, quantity, amount, unit_price) VALUES (3, 4, 2, 220.00, 110.00);\nINSERT INTO item (order_id, product_id, quantity, amount, unit_price) VALUES (4, 3, 4, 300.00, 75.00);",
    "name": "CustomerOrderSystem"
}