# trilogy models run on imports to reuse logic
import lineitem as line_item;
import std.display;

# you can define new concepts in line 
auto discounted_price <- line_item.extended_price * (1-line_item.discount); #the discounted price is off the extended privce
auto charge_price <- discounted_price * (1+line_item.tax); #charged price includes taxes

# use functions to define repeatable templatized logic
def part_percent_of_nation(x) -> cast(sum(x) by line_item.part.name, line_item.supplier.nation.id / sum(x) by line_item.supplier.nation.id as float::percent);


WHERE line_item.ship_date <= '1998-12-01'::date 
SELECT
    line_item.part.name,
    line_item.supplier.nation.name,
    sum(charge_price)-> total_charge_price,
    sum(discounted_price) -> total_discounted_price,
    # call functions with @
    @part_percent_of_nation(charge_price) as charge_price_percent_of_nation,
    @part_percent_of_nation(discounted_price) as discount_price_percent_of_nation
ORDER BY   
    total_charge_price desc limit 100
;

