"""Variant definitions for Query 5."""

from __future__ import annotations

from benchbox.core.tpchavoc.variant_base import StaticSQLVariant

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

select
    n_name,
    (select sum(l2.l_extendedprice * (1 - l2.l_discount))
     from lineitem l2, orders o2, customer c2, supplier s2
     where l2.l_orderkey = o2.o_orderkey
       and o2.o_custkey = c2.c_custkey
       and l2.l_suppkey = s2.s_suppkey
       and c2.c_nationkey = s2.s_nationkey
       and s2.s_nationkey = n1.n_nationkey
       and o2.o_orderdate >= date '1994-01-01'
       and o2.o_orderdate < date '1994-01-01' + interval '1' year) as revenue
from
    nation n1,
    region
where
    n1.n_regionkey = r_regionkey
    and r_name = 'ASIA'
    and exists (
        select 1
        from lineitem, orders, customer, supplier
        where l_orderkey = o_orderkey
          and o_custkey = c_custkey
          and l_suppkey = s_suppkey
          and c_nationkey = s_nationkey
          and s_nationkey = n1.n_nationkey
          and o_orderdate >= date '1994-01-01'
          and o_orderdate < date '1994-01-01' + interval '1' year
    )
order by
    revenue desc
""",
    ),
    2: StaticSQLVariant(
        2,
        "HAVING clause: Use HAVING for revenue filtering",
        """\

select
    n_name,
    sum(l_extendedprice * (1 - l_discount)) as revenue
from
    customer,
    orders,
    lineitem,
    supplier,
    nation,
    region
where
    c_custkey = o_custkey
    and l_orderkey = o_orderkey
    and l_suppkey = s_suppkey
    and c_nationkey = s_nationkey
    and s_nationkey = n_nationkey
    and n_regionkey = r_regionkey
    and r_name = 'ASIA'
    and o_orderdate >= date '1994-01-01'
    and o_orderdate < date '1994-01-01' + interval '1' year
group by
    n_name
having
    sum(l_extendedprice * (1 - l_discount)) > 0
order by
    revenue desc
""",
    ),
    3: StaticSQLVariant(
        3,
        "Explicit JOINs: Convert all implicit joins to explicit syntax",
        """\

select
    n.n_name,
    sum(l.l_extendedprice * (1 - l.l_discount)) as revenue
from
    region r
    inner join nation n on r.r_regionkey = n.n_regionkey
    inner join supplier s on n.n_nationkey = s.s_nationkey
    inner join customer c on s.s_nationkey = c.c_nationkey
    inner join orders o on c.c_custkey = o.o_custkey
    inner join lineitem l on o.o_orderkey = l.l_orderkey and s.s_suppkey = l.l_suppkey
where
    r.r_name = 'ASIA'
    and o.o_orderdate >= date '1994-01-01'
    and o.o_orderdate < date '1994-01-01' + interval '1' year
group by
    n.n_name
order by
    revenue desc
""",
    ),
    4: StaticSQLVariant(
        4,
        "UNION ALL: Split by regions",
        """\

select
    n_name,
    sum(revenue) as revenue
from (
    select
        n_name,
        sum(l_extendedprice * (1 - l_discount)) as revenue
    from customer, orders, lineitem, supplier, nation, region
    where c_custkey = o_custkey
      and l_orderkey = o_orderkey
      and l_suppkey = s_suppkey
      and c_nationkey = s_nationkey
      and s_nationkey = n_nationkey
      and n_regionkey = r_regionkey
      and r_name = 'ASIA'
      and o_orderdate >= date '1994-01-01'
      and o_orderdate < date '1994-01-01' + interval '1' year
    group by n_name

    union all

    select
        n_name,
        0 as revenue
    from nation, region
    where n_regionkey = r_regionkey
      and r_name = 'ASIA'
      and not exists (
          select 1
          from customer, orders, lineitem, supplier
          where c_custkey = o_custkey
            and l_orderkey = o_orderkey
            and l_suppkey = s_suppkey
            and c_nationkey = s_nationkey
            and s_nationkey = n_nationkey
            and o_orderdate >= date '1994-01-01'
            and o_orderdate < date '1994-01-01' + interval '1' year
      )
) combined
group by n_name
having sum(revenue) > 0
order by revenue desc
""",
    ),
    5: StaticSQLVariant(
        5,
        "Common Table Expression: Use CTE to break down complex joins",
        """\

with asia_nations as (
    select n_nationkey, n_name
    from nation, region
    where n_regionkey = r_regionkey
      and r_name = 'ASIA'
),
qualified_orders as (
    select o_orderkey, o_custkey
    from orders
    where o_orderdate >= date '1994-01-01'
      and o_orderdate < date '1994-01-01' + interval '1' year
),
qualified_customers as (
    select c_custkey, c_nationkey
    from customer
),
qualified_suppliers as (
    select s_suppkey, s_nationkey
    from supplier
),
revenue_base as (
    select
        an.n_name,
        l.l_extendedprice * (1 - l.l_discount) as item_revenue
    from
        asia_nations an
        join qualified_suppliers qs on an.n_nationkey = qs.s_nationkey
        join qualified_customers qc on qs.s_nationkey = qc.c_nationkey
        join qualified_orders qo on qc.c_custkey = qo.o_custkey
        join lineitem l on qo.o_orderkey = l.l_orderkey and qs.s_suppkey = l.l_suppkey
)
select
    n_name,
    sum(item_revenue) as revenue
from revenue_base
group by n_name
order by revenue desc
""",
    ),
    6: StaticSQLVariant(
        6,
        "Derived table: Wrap main query with derived table",
        """\

select
    nation_name,
    total_revenue
from (
    select
        n_name as nation_name,
        sum(l_extendedprice * (1 - l_discount)) as total_revenue
    from
        customer,
        orders,
        lineitem,
        supplier,
        nation,
        region
    where
        c_custkey = o_custkey
        and l_orderkey = o_orderkey
        and l_suppkey = s_suppkey
        and c_nationkey = s_nationkey
        and s_nationkey = n_nationkey
        and n_regionkey = r_regionkey
        and r_name = 'ASIA'
        and o_orderdate >= date '1994-01-01'
        and o_orderdate < date '1994-01-01' + interval '1' year
    group by
        n_name
) nation_revenue
order by
    total_revenue desc
""",
    ),
    7: StaticSQLVariant(
        7,
        "FILTER clause: Use FILTER for conditional aggregation (OLAP)",
        """\

select
    n_name,
    sum(l_extendedprice * (1 - l_discount))
        filter (where o_orderdate >= date '1994-01-01'
                   and o_orderdate < date '1994-01-01' + interval '1' year) as revenue
from
    customer,
    orders,
    lineitem,
    supplier,
    nation,
    region
where
    c_custkey = o_custkey
    and l_orderkey = o_orderkey
    and l_suppkey = s_suppkey
    and c_nationkey = s_nationkey
    and s_nationkey = n_nationkey
    and n_regionkey = r_regionkey
    and r_name = 'ASIA'
group by
    n_name
having
    revenue > 0
order by
    revenue desc
""",
    ),
    8: StaticSQLVariant(
        8,
        "EXISTS pattern: Use EXISTS for join conditions",
        """\

select
    n_name,
    sum(l_extendedprice * (1 - l_discount)) as revenue
from
    lineitem,
    nation
where
    exists (
        select 1
        from supplier
        where s_suppkey = l_suppkey
          and s_nationkey = n_nationkey
    )
    and exists (
        select 1
        from orders, customer
        where o_orderkey = l_orderkey
          and c_custkey = o_custkey
          and c_nationkey = n_nationkey
          and o_orderdate >= date '1994-01-01'
          and o_orderdate < date '1994-01-01' + interval '1' year
    )
    and exists (
        select 1
        from region
        where r_regionkey = n_regionkey
          and r_name = 'ASIA'
    )
group by
    n_name
order by
    revenue desc
""",
    ),
    9: StaticSQLVariant(
        9,
        "Window functions: Use window functions for regional analysis (OLAP)",
        """\

select distinct
    n_name,
    sum(l_extendedprice * (1 - l_discount)) over (partition by n_name) as revenue,
    rank() over (order by sum(l_extendedprice * (1 - l_discount)) over (partition by n_name) desc) as revenue_rank
from
    customer,
    orders,
    lineitem,
    supplier,
    nation,
    region
where
    c_custkey = o_custkey
    and l_orderkey = o_orderkey
    and l_suppkey = s_suppkey
    and c_nationkey = s_nationkey
    and s_nationkey = n_nationkey
    and n_regionkey = r_regionkey
    and r_name = 'ASIA'
    and o_orderdate >= date '1994-01-01'
    and o_orderdate < date '1994-01-01' + interval '1' year
order by
    revenue desc
""",
    ),
    10: StaticSQLVariant(
        10,
        "CASE expressions: Add extensive CASE logic for conditional processing",
        """\

select
    case
        when n_name = 'CHINA' then 'CHINA'
        when n_name = 'INDIA' then 'INDIA'
        when n_name = 'JAPAN' then 'JAPAN'
        when n_name = 'INDONESIA' then 'INDONESIA'
        when n_name = 'VIETNAM' then 'VIETNAM'
        else n_name
    end as n_name,
    sum(
        case
            when l_discount between 0.05 and 0.07 then l_extendedprice * (1 - l_discount) * 1.0
            when l_discount < 0.05 then l_extendedprice * (1 - l_discount) * 1.0
            when l_discount > 0.07 then l_extendedprice * (1 - l_discount) * 1.0
            else l_extendedprice * (1 - l_discount)
        end
    ) as revenue
from
    customer,
    orders,
    lineitem,
    supplier,
    nation,
    region
where
    c_custkey = o_custkey
    and l_orderkey = o_orderkey
    and l_suppkey = s_suppkey
    and case
        when c_nationkey = s_nationkey then c_nationkey
        else null
    end = s_nationkey
    and s_nationkey = n_nationkey
    and n_regionkey = r_regionkey
    and case
        when r_name = 'ASIA' then r_name
        else null
    end = 'ASIA'
    and case
        when o_orderdate >= date '1994-01-01' then 1
        else 0
    end = 1
    and case
        when o_orderdate < date '1994-01-01' + interval '1' year then 1
        else 0
    end = 1
group by
    n_name
order by
    revenue desc
""",
    ),
}

__all__ = ["VARIANTS"]
