"""Variant definitions for Query 9."""

from __future__ import annotations

from benchbox.core.tpchavoc.variant_base import StaticSQLVariant

VARIANTS = {
    1: StaticSQLVariant(
        1,
        "Scalar subqueries: Use scalar subqueries for profit calculation",
        """\

select
    nation,
    o_year,
    (select sum(l2.l_extendedprice * (1 - l2.l_discount) - ps2.ps_supplycost * l2.l_quantity)
     from part p2, supplier s2, lineitem l2, partsupp ps2, orders o2, nation n2
     where s2.s_suppkey = l2.l_suppkey
       and ps2.ps_suppkey = l2.l_suppkey
       and ps2.ps_partkey = l2.l_partkey
       and p2.p_partkey = l2.l_partkey
       and o2.o_orderkey = l2.l_orderkey
       and s2.s_nationkey = n2.n_nationkey
       and p2.p_name like '%green%'
       and n2.n_name = profit.nation
       and extract(year from o2.o_orderdate) = profit.o_year) as sum_profit
from (
    select distinct
        n_name as nation,
        extract(year from o_orderdate) as o_year
    from
        part, supplier, lineitem, partsupp, orders, nation
    where
        s_suppkey = l_suppkey
        and ps_suppkey = l_suppkey
        and ps_partkey = l_partkey
        and p_partkey = l_partkey
        and o_orderkey = l_orderkey
        and s_nationkey = n_nationkey
        and p_name like '%green%'
) as profit
group by
    nation,
    o_year
order by
    nation,
    o_year desc
""",
    ),
    2: StaticSQLVariant(
        2,
        "HAVING clause: Use HAVING for profit filtering",
        """\

select
    nation,
    o_year,
    sum(amount) as sum_profit
from (
    select
        n_name as nation,
        extract(year from o_orderdate) as o_year,
        l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount
    from
        part, supplier, lineitem, partsupp, orders, nation
    where
        s_suppkey = l_suppkey
        and ps_suppkey = l_suppkey
        and ps_partkey = l_partkey
        and p_partkey = l_partkey
        and o_orderkey = l_orderkey
        and s_nationkey = n_nationkey
        and p_name like '%green%'
) as profit
group by
    nation,
    o_year
having
    sum(amount) > 0
order by
    nation,
    o_year desc
""",
    ),
    3: StaticSQLVariant(
        3,
        "Explicit JOINs: Convert all implicit joins to explicit JOIN syntax",
        """\

select
    nation,
    o_year,
    sum(amount) as sum_profit
from (
    select
        n.n_name as nation,
        extract(year from o.o_orderdate) as o_year,
        l.l_extendedprice * (1 - l.l_discount) - ps.ps_supplycost * l.l_quantity as amount
    from
        part p
        inner join lineitem l on p.p_partkey = l.l_partkey
        inner join supplier s on l.l_suppkey = s.s_suppkey
        inner join partsupp ps on s.s_suppkey = ps.ps_suppkey and p.p_partkey = ps.ps_partkey
        inner join orders o on l.l_orderkey = o.o_orderkey
        inner join nation n on s.s_nationkey = n.n_nationkey
    where
        p.p_name like '%green%'
) as profit
group by
    nation,
    o_year
order by
    nation,
    o_year desc
""",
    ),
    4: StaticSQLVariant(
        4,
        "UNION ALL: Split by nation groups",
        """\

select
    nation,
    o_year,
    sum(amount) as sum_profit
from (
    select
        n_name as nation,
        extract(year from o_orderdate) as o_year,
        l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount
    from part, supplier, lineitem, partsupp, orders, nation
    where s_suppkey = l_suppkey
      and ps_suppkey = l_suppkey
      and ps_partkey = l_partkey
      and p_partkey = l_partkey
      and o_orderkey = l_orderkey
      and s_nationkey = n_nationkey
      and p_name like '%green%'
      and n_name in ('UNITED STATES', 'CANADA', 'BRAZIL')

    union all

    select
        n_name as nation,
        extract(year from o_orderdate) as o_year,
        l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount
    from part, supplier, lineitem, partsupp, orders, nation
    where s_suppkey = l_suppkey
      and ps_suppkey = l_suppkey
      and ps_partkey = l_partkey
      and p_partkey = l_partkey
      and o_orderkey = l_orderkey
      and s_nationkey = n_nationkey
      and p_name like '%green%'
      and n_name not in ('UNITED STATES', 'CANADA', 'BRAZIL')
) as profit
group by
    nation,
    o_year
order by
    nation,
    o_year desc
""",
    ),
    5: StaticSQLVariant(
        5,
        "Common Table Expression: Use CTEs to break down complex profit calculation",
        """\

with green_parts as (
    select p_partkey, p_name
    from part
    where p_name like '%green%'
),
qualified_suppliers as (
    select s_suppkey, s_nationkey, n_name as nation
    from supplier, nation
    where s_nationkey = n_nationkey
),
profit_calculation as (
    select
        qs.nation,
        extract(year from o.o_orderdate) as o_year,
        l.l_extendedprice * (1 - l.l_discount) - ps.ps_supplycost * l.l_quantity as amount
    from
        green_parts gp
        join lineitem l on gp.p_partkey = l.l_partkey
        join qualified_suppliers qs on l.l_suppkey = qs.s_suppkey
        join partsupp ps on qs.s_suppkey = ps.ps_suppkey and gp.p_partkey = ps.ps_partkey
        join orders o on l.l_orderkey = o.o_orderkey
)
select
    nation,
    o_year,
    sum(amount) as sum_profit
from profit_calculation
group by
    nation,
    o_year
order by
    nation,
    o_year desc
""",
    ),
    6: StaticSQLVariant(
        6,
        "Derived table: Nested derived tables for profit analysis",
        """\

select
    nation,
    o_year,
    total_profit
from (
    select
        nation,
        o_year,
        sum(amount) as total_profit
    from (
        select
            profit_data.nation,
            profit_data.o_year,
            profit_data.amount
        from (
            select
                n_name as nation,
                extract(year from o_orderdate) as o_year,
                l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount
            from
                part, supplier, lineitem, partsupp, orders, nation
            where
                s_suppkey = l_suppkey
                and ps_suppkey = l_suppkey
                and ps_partkey = l_partkey
                and p_partkey = l_partkey
                and o_orderkey = l_orderkey
                and s_nationkey = n_nationkey
                and p_name like '%green%'
        ) as profit_data
    ) as profit_details
    group by
        nation,
        o_year
) as profit_summary
order by
    nation,
    o_year desc
""",
    ),
    7: StaticSQLVariant(
        7,
        "FILTER clause: Use FILTER for conditional profit aggregation (OLAP)",
        """\

select
    nation,
    o_year,
    sum(l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity)
        filter (where p_name like '%green%') as sum_profit
from (
    select
        n_name as nation,
        extract(year from o_orderdate) as o_year,
        l_extendedprice,
        l_discount,
        ps_supplycost,
        l_quantity,
        p_name
    from
        part, supplier, lineitem, partsupp, orders, nation
    where
        s_suppkey = l_suppkey
        and ps_suppkey = l_suppkey
        and ps_partkey = l_partkey
        and p_partkey = l_partkey
        and o_orderkey = l_orderkey
        and s_nationkey = n_nationkey
) as profit
group by
    nation,
    o_year
having
    sum_profit is not null
order by
    nation,
    o_year desc
""",
    ),
    8: StaticSQLVariant(
        8,
        "EXISTS pattern: Use EXISTS for part filtering",
        """\

select
    nation,
    o_year,
    sum(amount) as sum_profit
from (
    select
        n_name as nation,
        extract(year from o_orderdate) as o_year,
        l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount
    from
        supplier, lineitem, partsupp, orders, nation
    where
        s_suppkey = l_suppkey
        and ps_suppkey = l_suppkey
        and ps_partkey = l_partkey
        and o_orderkey = l_orderkey
        and s_nationkey = n_nationkey
        and exists (
            select 1
            from part
            where p_partkey = l_partkey
              and p_name like '%green%'
        )
) as profit
group by
    nation,
    o_year
order by
    nation,
    o_year desc
""",
    ),
    9: StaticSQLVariant(
        9,
        "Window functions: Use window functions for profit analysis (OLAP)",
        """\

select distinct
    nation,
    o_year,
    sum(amount) over (partition by nation, o_year) as sum_profit,
    rank() over (partition by o_year order by sum(amount) over (partition by nation, o_year) desc) as profit_rank
from (
    select
        n_name as nation,
        extract(year from o_orderdate) as o_year,
        l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount
    from
        part, supplier, lineitem, partsupp, orders, nation
    where
        s_suppkey = l_suppkey
        and ps_suppkey = l_suppkey
        and ps_partkey = l_partkey
        and p_partkey = l_partkey
        and o_orderkey = l_orderkey
        and s_nationkey = n_nationkey
        and p_name like '%green%'
) as profit
order by
    nation,
    o_year desc
""",
    ),
    10: StaticSQLVariant(
        10,
        "CASE expressions: Add extensive CASE logic for profit calculation",
        """\

select
    case
        when nation in ('UNITED STATES', 'CANADA', 'BRAZIL') then nation
        else 'OTHER AMERICAS'
    end as nation,
    o_year,
    sum(
        case
            when amount > 0 then amount
            when amount < 0 then 0
            else amount
        end
    ) as sum_profit
from (
    select
        case
            when n_name is not null then n_name
            else 'UNKNOWN'
        end as nation,
        case
            when extract(year from o_orderdate) between 1990 and 2000 then extract(year from o_orderdate)
            else null
        end as o_year,
        case
            when l_discount between 0.05 and 0.07 then l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity
            when l_discount < 0.05 then l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity
            when l_discount > 0.07 then l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity
            else l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity
        end as amount
    from
        part, supplier, lineitem, partsupp, orders, nation
    where
        s_suppkey = l_suppkey
        and ps_suppkey = l_suppkey
        and ps_partkey = l_partkey
        and p_partkey = l_partkey
        and o_orderkey = l_orderkey
        and s_nationkey = n_nationkey
        and case
            when p_name like '%green%' then 1
            else 0
        end = 1
) as profit
where o_year is not null
group by
    nation,
    o_year
order by
    nation,
    o_year desc
""",
    ),
}

__all__ = ["VARIANTS"]
