"""Variant definitions for Query 7."""

from __future__ import annotations

from benchbox.core.tpchavoc.variant_base import StaticSQLVariant

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

select
    supp_nation,
    cust_nation,
    l_year,
    (select sum(l2.l_extendedprice * (1 - l2.l_discount))
     from supplier s2, lineitem l2, orders o2, customer c2, nation n3, nation n4
     where s2.s_suppkey = l2.l_suppkey
       and o2.o_orderkey = l2.l_orderkey
       and c2.c_custkey = o2.o_custkey
       and s2.s_nationkey = n3.n_nationkey
       and c2.c_nationkey = n4.n_nationkey
       and n3.n_name = shipping.supp_nation
       and n4.n_name = shipping.cust_nation
       and extract(year from l2.l_shipdate) = shipping.l_year
       and l2.l_shipdate between date '1995-01-01' and date '1996-12-31') as revenue
from (
    select distinct
        n1.n_name as supp_nation,
        n2.n_name as cust_nation,
        extract(year from l_shipdate) as l_year
    from
        supplier, lineitem, orders, customer, nation n1, nation n2
    where
        s_suppkey = l_suppkey
        and o_orderkey = l_orderkey
        and c_custkey = o_custkey
        and s_nationkey = n1.n_nationkey
        and c_nationkey = n2.n_nationkey
        and ((n1.n_name = 'FRANCE' and n2.n_name = 'GERMANY')
             or (n1.n_name = 'GERMANY' and n2.n_name = 'FRANCE'))
        and l_shipdate between date '1995-01-01' and date '1996-12-31'
) as shipping
group by
    supp_nation,
    cust_nation,
    l_year
order by
    supp_nation,
    cust_nation,
    l_year
""",
    ),
    2: StaticSQLVariant(
        2,
        "HAVING clause: Use HAVING for volume filtering",
        """\

select
    supp_nation,
    cust_nation,
    l_year,
    sum(volume) as revenue
from (
    select
        n1.n_name as supp_nation,
        n2.n_name as cust_nation,
        extract(year from l_shipdate) as l_year,
        l_extendedprice * (1 - l_discount) as volume
    from
        supplier, lineitem, orders, customer, nation n1, nation n2
    where
        s_suppkey = l_suppkey
        and o_orderkey = l_orderkey
        and c_custkey = o_custkey
        and s_nationkey = n1.n_nationkey
        and c_nationkey = n2.n_nationkey
        and ((n1.n_name = 'FRANCE' and n2.n_name = 'GERMANY')
             or (n1.n_name = 'GERMANY' and n2.n_name = 'FRANCE'))
        and l_shipdate between date '1995-01-01' and date '1996-12-31'
) as shipping
group by
    supp_nation,
    cust_nation,
    l_year
having
    sum(volume) > 0
order by
    supp_nation,
    cust_nation,
    l_year
""",
    ),
    3: StaticSQLVariant(
        3,
        "Explicit JOINs: Convert all implicit joins to explicit JOIN syntax",
        """\

select
    supp_nation,
    cust_nation,
    l_year,
    sum(volume) as revenue
from (
    select
        n1.n_name as supp_nation,
        n2.n_name as cust_nation,
        extract(year from l_shipdate) as l_year,
        l_extendedprice * (1 - l_discount) as volume
    from
        supplier s
        inner join lineitem l on s.s_suppkey = l.l_suppkey
        inner join orders o on l.l_orderkey = o.o_orderkey
        inner join customer c on o.o_custkey = c.c_custkey
        inner join nation n1 on s.s_nationkey = n1.n_nationkey
        inner join nation n2 on c.c_nationkey = n2.n_nationkey
    where
        ((n1.n_name = 'FRANCE' and n2.n_name = 'GERMANY')
         or (n1.n_name = 'GERMANY' and n2.n_name = 'FRANCE'))
        and l.l_shipdate between date '1995-01-01' and date '1996-12-31'
) as shipping
group by
    supp_nation,
    cust_nation,
    l_year
order by
    supp_nation,
    cust_nation,
    l_year
""",
    ),
    4: StaticSQLVariant(
        4,
        "UNION ALL: Split by nation combinations",
        """\

select
    supp_nation,
    cust_nation,
    l_year,
    sum(volume) as revenue
from (
    select
        n1.n_name as supp_nation,
        n2.n_name as cust_nation,
        extract(year from l_shipdate) as l_year,
        l_extendedprice * (1 - l_discount) as volume
    from
        supplier, lineitem, orders, customer, nation n1, nation n2
    where
        s_suppkey = l_suppkey
        and o_orderkey = l_orderkey
        and c_custkey = o_custkey
        and s_nationkey = n1.n_nationkey
        and c_nationkey = n2.n_nationkey
        and n1.n_name = 'FRANCE' and n2.n_name = 'GERMANY'
        and l_shipdate between date '1995-01-01' and date '1996-12-31'

    union all

    select
        n1.n_name as supp_nation,
        n2.n_name as cust_nation,
        extract(year from l_shipdate) as l_year,
        l_extendedprice * (1 - l_discount) as volume
    from
        supplier, lineitem, orders, customer, nation n1, nation n2
    where
        s_suppkey = l_suppkey
        and o_orderkey = l_orderkey
        and c_custkey = o_custkey
        and s_nationkey = n1.n_nationkey
        and c_nationkey = n2.n_nationkey
        and n1.n_name = 'GERMANY' and n2.n_name = 'FRANCE'
        and l_shipdate between date '1995-01-01' and date '1996-12-31'
) as shipping
group by
    supp_nation,
    cust_nation,
    l_year
order by
    supp_nation,
    cust_nation,
    l_year
""",
    ),
    5: StaticSQLVariant(
        5,
        "Common Table Expression: Use CTEs to break down complex logic",
        """\

with qualified_suppliers as (
    select s_suppkey, n_name as supp_nation
    from supplier, nation
    where s_nationkey = n_nationkey
      and n_name in ('FRANCE', 'GERMANY')
),
qualified_customers as (
    select c_custkey, n_name as cust_nation
    from customer, nation
    where c_nationkey = n_nationkey
      and n_name in ('FRANCE', 'GERMANY')
),
qualified_orders as (
    select o_orderkey, o_custkey, extract(year from l_shipdate) as l_year
    from orders, lineitem
    where o_orderkey = l_orderkey
      and l_shipdate between date '1995-01-01' and date '1996-12-31'
),
shipping_base as (
    select
        qs.supp_nation,
        qc.cust_nation,
        qo.l_year,
        l.l_extendedprice * (1 - l.l_discount) as volume
    from
        qualified_suppliers qs
        join lineitem l on qs.s_suppkey = l.l_suppkey
        join qualified_orders qo on l.l_orderkey = qo.o_orderkey
        join qualified_customers qc on qo.o_custkey = qc.c_custkey
    where
        ((qs.supp_nation = 'FRANCE' and qc.cust_nation = 'GERMANY')
         or (qs.supp_nation = 'GERMANY' and qc.cust_nation = 'FRANCE'))
)
select
    supp_nation,
    cust_nation,
    l_year,
    sum(volume) as revenue
from shipping_base
group by
    supp_nation,
    cust_nation,
    l_year
order by
    supp_nation,
    cust_nation,
    l_year
""",
    ),
    6: StaticSQLVariant(
        6,
        "Nested derived tables: Multiple levels of derived tables",
        """\

select
    supp_nation,
    cust_nation,
    l_year,
    total_volume
from (
    select
        supp_nation,
        cust_nation,
        l_year,
        sum(volume) as total_volume
    from (
        select
            shipping_info.supp_nation,
            shipping_info.cust_nation,
            shipping_info.l_year,
            shipping_info.volume
        from (
            select
                n1.n_name as supp_nation,
                n2.n_name as cust_nation,
                extract(year from l_shipdate) as l_year,
                l_extendedprice * (1 - l_discount) as volume
            from
                supplier, lineitem, orders, customer, nation n1, nation n2
            where
                s_suppkey = l_suppkey
                and o_orderkey = l_orderkey
                and c_custkey = o_custkey
                and s_nationkey = n1.n_nationkey
                and c_nationkey = n2.n_nationkey
                and ((n1.n_name = 'FRANCE' and n2.n_name = 'GERMANY')
                     or (n1.n_name = 'GERMANY' and n2.n_name = 'FRANCE'))
                and l_shipdate between date '1995-01-01' and date '1996-12-31'
        ) as shipping_info
    ) as shipping_details
    group by
        supp_nation,
        cust_nation,
        l_year
) as shipping_summary
order by
    supp_nation,
    cust_nation,
    l_year
""",
    ),
    7: StaticSQLVariant(
        7,
        "FILTER clause: Use FILTER for conditional aggregation (OLAP)",
        """\

select
    supp_nation,
    cust_nation,
    l_year,
    sum(l_extendedprice * (1 - l_discount))
        filter (where l_shipdate between date '1995-01-01' and date '1996-12-31') as revenue
from (
    select
        n1.n_name as supp_nation,
        n2.n_name as cust_nation,
        extract(year from l_shipdate) as l_year,
        l_extendedprice,
        l_discount,
        l_shipdate
    from
        supplier, lineitem, orders, customer, nation n1, nation n2
    where
        s_suppkey = l_suppkey
        and o_orderkey = l_orderkey
        and c_custkey = o_custkey
        and s_nationkey = n1.n_nationkey
        and c_nationkey = n2.n_nationkey
        and ((n1.n_name = 'FRANCE' and n2.n_name = 'GERMANY')
             or (n1.n_name = 'GERMANY' and n2.n_name = 'FRANCE'))
) as shipping
group by
    supp_nation,
    cust_nation,
    l_year
having
    revenue is not null
order by
    supp_nation,
    cust_nation,
    l_year
""",
    ),
    8: StaticSQLVariant(
        8,
        "EXISTS pattern: Use EXISTS for nation filtering",
        """\

select
    supp_nation,
    cust_nation,
    l_year,
    sum(volume) as revenue
from (
    select
        n1.n_name as supp_nation,
        n2.n_name as cust_nation,
        extract(year from l_shipdate) as l_year,
        l_extendedprice * (1 - l_discount) as volume
    from
        supplier, lineitem, orders, customer, nation n1, nation n2
    where
        s_suppkey = l_suppkey
        and o_orderkey = l_orderkey
        and c_custkey = o_custkey
        and s_nationkey = n1.n_nationkey
        and c_nationkey = n2.n_nationkey
        and l_shipdate between date '1995-01-01' and date '1996-12-31'
        and exists (
            select 1
            where (n1.n_name = 'FRANCE' and n2.n_name = 'GERMANY')
               or (n1.n_name = 'GERMANY' and n2.n_name = 'FRANCE')
        )
) as shipping
group by
    supp_nation,
    cust_nation,
    l_year
order by
    supp_nation,
    cust_nation,
    l_year
""",
    ),
    9: StaticSQLVariant(
        9,
        "Window functions: Use window functions for revenue analysis (OLAP)",
        """\

select distinct
    supp_nation,
    cust_nation,
    l_year,
    sum(volume) over (partition by supp_nation, cust_nation, l_year) as revenue,
    rank() over (partition by l_year order by sum(volume) over (partition by supp_nation, cust_nation, l_year) desc) as revenue_rank
from (
    select
        n1.n_name as supp_nation,
        n2.n_name as cust_nation,
        extract(year from l_shipdate) as l_year,
        l_extendedprice * (1 - l_discount) as volume
    from
        supplier, lineitem, orders, customer, nation n1, nation n2
    where
        s_suppkey = l_suppkey
        and o_orderkey = l_orderkey
        and c_custkey = o_custkey
        and s_nationkey = n1.n_nationkey
        and c_nationkey = n2.n_nationkey
        and ((n1.n_name = 'FRANCE' and n2.n_name = 'GERMANY')
             or (n1.n_name = 'GERMANY' and n2.n_name = 'FRANCE'))
        and l_shipdate between date '1995-01-01' and date '1996-12-31'
) as shipping
order by
    supp_nation,
    cust_nation,
    l_year
""",
    ),
    10: StaticSQLVariant(
        10,
        "CASE expressions: Add extensive CASE logic for conditional processing",
        """\

select
    case
        when supp_nation = 'FRANCE' then 'FRANCE'
        when supp_nation = 'GERMANY' then 'GERMANY'
        else supp_nation
    end as supp_nation,
    case
        when cust_nation = 'FRANCE' then 'FRANCE'
        when cust_nation = 'GERMANY' then 'GERMANY'
        else cust_nation
    end as cust_nation,
    l_year,
    sum(
        case
            when volume > 0 then volume
            else 0
        end
    ) as revenue
from (
    select
        case
            when n1.n_name in ('FRANCE', 'GERMANY') then n1.n_name
            else 'OTHER'
        end as supp_nation,
        case
            when n2.n_name in ('FRANCE', 'GERMANY') then n2.n_name
            else 'OTHER'
        end as cust_nation,
        extract(year from l_shipdate) as l_year,
        case
            when l_discount between 0.05 and 0.07 then l_extendedprice * (1 - l_discount)
            when l_discount < 0.05 then l_extendedprice * (1 - l_discount)
            when l_discount > 0.07 then l_extendedprice * (1 - l_discount)
            else l_extendedprice * (1 - l_discount)
        end as volume
    from
        supplier, lineitem, orders, customer, nation n1, nation n2
    where
        s_suppkey = l_suppkey
        and o_orderkey = l_orderkey
        and c_custkey = o_custkey
        and s_nationkey = n1.n_nationkey
        and c_nationkey = n2.n_nationkey
        and case
            when (n1.n_name = 'FRANCE' and n2.n_name = 'GERMANY') then 1
            when (n1.n_name = 'GERMANY' and n2.n_name = 'FRANCE') then 1
            else 0
        end = 1
        and case
            when l_shipdate >= date '1995-01-01' then 1
            else 0
        end = 1
        and case
            when l_shipdate <= date '1996-12-31' then 1
            else 0
        end = 1
) as shipping
group by
    supp_nation,
    cust_nation,
    l_year
order by
    supp_nation,
    cust_nation,
    l_year
""",
    ),
}

__all__ = ["VARIANTS"]
