"""Variant definitions for Query 14."""

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
    100.00 *
    (select sum(l2.l_extendedprice * (1 - l2.l_discount))
     from lineitem l2, part p2
     where l2.l_partkey = p2.p_partkey
       and p2.p_type like 'PROMO%'
       and l2.l_shipdate >= date '1995-09-01'
       and l2.l_shipdate < date '1995-09-01' + interval '1' month) /
    (select sum(l3.l_extendedprice * (1 - l3.l_discount))
     from lineitem l3, part p3
     where l3.l_partkey = p3.p_partkey
       and l3.l_shipdate >= date '1995-09-01'
       and l3.l_shipdate < date '1995-09-01' + interval '1' month) as promo_revenue
""",
    ),
    2: StaticSQLVariant(
        2,
        "HAVING clause: Use HAVING for revenue validation",
        """\

select
    100.00 * sum(case
        when p_type like 'PROMO%'
            then l_extendedprice * (1 - l_discount)
        else 0
    end) / sum(l_extendedprice * (1 - l_discount)) as promo_revenue
from
    lineitem,
    part
where
    l_partkey = p_partkey
    and l_shipdate >= date '1995-09-01'
    and l_shipdate < date '1995-09-01' + interval '1' month
group by
    ()
having
    sum(l_extendedprice * (1 - l_discount)) > 0
""",
    ),
    3: StaticSQLVariant(
        3,
        "Explicit JOINs: Convert implicit joins to explicit JOIN syntax",
        """\

select
    100.00 * sum(case
        when p.p_type like 'PROMO%'
            then l.l_extendedprice * (1 - l.l_discount)
        else 0
    end) / sum(l.l_extendedprice * (1 - l.l_discount)) as promo_revenue
from
    lineitem l
    inner join part p on l.l_partkey = p.p_partkey
where
    l.l_shipdate >= date '1995-09-01'
    and l.l_shipdate < date '1995-09-01' + interval '1' month
""",
    ),
    4: StaticSQLVariant(
        4,
        "UNION ALL: Split by part type categories",
        """\

select
    100.00 * sum(promo_revenue) / sum(total_revenue) as promo_revenue
from (
    select
        sum(case when p_type like 'PROMO%' then l_extendedprice * (1 - l_discount) else 0 end) as promo_revenue,
        sum(l_extendedprice * (1 - l_discount)) as total_revenue
    from lineitem, part
    where l_partkey = p_partkey
      and l_shipdate >= date '1995-09-01'
      and l_shipdate < date '1995-09-01' + interval '1' month
      and p_type like 'PROMO%'

    union all

    select
        0 as promo_revenue,
        sum(l_extendedprice * (1 - l_discount)) as total_revenue
    from lineitem, part
    where l_partkey = p_partkey
      and l_shipdate >= date '1995-09-01'
      and l_shipdate < date '1995-09-01' + interval '1' month
      and p_type not like 'PROMO%'
) combined
""",
    ),
    5: StaticSQLVariant(
        5,
        "Common Table Expression: Use CTEs to break down revenue calculation",
        """\

with qualified_lineitems as (
    select l_partkey, l_extendedprice, l_discount
    from lineitem
    where l_shipdate >= date '1995-09-01'
      and l_shipdate < date '1995-09-01' + interval '1' month
),
promo_parts as (
    select p_partkey
    from part
    where p_type like 'PROMO%'
),
revenue_calculation as (
    select
        case when pp.p_partkey is not null then ql.l_extendedprice * (1 - ql.l_discount) else 0 end as promo_revenue,
        ql.l_extendedprice * (1 - ql.l_discount) as total_revenue
    from qualified_lineitems ql
    join part p on ql.l_partkey = p.p_partkey
    left join promo_parts pp on ql.l_partkey = pp.p_partkey
)
select
    100.00 * sum(promo_revenue) / sum(total_revenue) as promo_revenue
from revenue_calculation
""",
    ),
    6: StaticSQLVariant(
        6,
        "Derived table: Wrap main calculation in derived table",
        """\

select
    100.00 * promotional_revenue / total_revenue as promo_revenue
from (
    select
        sum(case
            when p_type like 'PROMO%'
                then l_extendedprice * (1 - l_discount)
            else 0
        end) as promotional_revenue,
        sum(l_extendedprice * (1 - l_discount)) as total_revenue
    from
        lineitem,
        part
    where
        l_partkey = p_partkey
        and l_shipdate >= date '1995-09-01'
        and l_shipdate < date '1995-09-01' + interval '1' month
) revenue_analysis
""",
    ),
    7: StaticSQLVariant(
        7,
        "FILTER clause: Use FILTER for conditional revenue aggregation (OLAP)",
        """\

select
    100.00 *
    sum(l_extendedprice * (1 - l_discount)) filter (where p_type like 'PROMO%') /
    sum(l_extendedprice * (1 - l_discount)) as promo_revenue
from
    lineitem,
    part
where
    l_partkey = p_partkey
    and l_shipdate >= date '1995-09-01'
    and l_shipdate < date '1995-09-01' + interval '1' month
""",
    ),
    8: StaticSQLVariant(
        8,
        "EXISTS pattern: Use EXISTS for part type filtering",
        """\

select
    100.00 * sum(case
        when exists (
            select 1
            from part p2
            where p2.p_partkey = l.l_partkey
              and p2.p_type like 'PROMO%'
        ) then l.l_extendedprice * (1 - l.l_discount)
        else 0
    end) / sum(l.l_extendedprice * (1 - l.l_discount)) as promo_revenue
from
    lineitem l
where
    l.l_shipdate >= date '1995-09-01'
    and l.l_shipdate < date '1995-09-01' + interval '1' month
    and exists (
        select 1
        from part p
        where p.p_partkey = l.l_partkey
    )
""",
    ),
    9: StaticSQLVariant(
        9,
        "Window functions: Use window functions for revenue analysis (OLAP)",
        """\

select distinct
    100.00 *
    sum(case when p_type like 'PROMO%' then l_extendedprice * (1 - l_discount) else 0 end) over () /
    sum(l_extendedprice * (1 - l_discount)) over () as promo_revenue,
    rank() over (order by l_partkey) as part_rank
from
    lineitem,
    part
where
    l_partkey = p_partkey
    and l_shipdate >= date '1995-09-01'
    and l_shipdate < date '1995-09-01' + interval '1' month
limit 1
""",
    ),
    10: StaticSQLVariant(
        10,
        "CASE expressions: Add extensive CASE logic for promotion classification",
        """\

select
    100.00 *
    sum(
        case
            when case
                when p_type like 'PROMO%' then 1
                else 0
            end = 1 then
                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
            else 0
        end
    ) /
    sum(
        case
            when l_extendedprice > 0 and l_discount >= 0 then l_extendedprice * (1 - l_discount)
            else 0
        end
    ) as promo_revenue
from
    lineitem,
    part
where
    l_partkey = p_partkey
    and case
        when l_shipdate >= date '1995-09-01' then 1
        else 0
    end = 1
    and case
        when l_shipdate < date '1995-09-01' + interval '1' month then 1
        else 0
    end = 1
""",
    ),
}

__all__ = ["VARIANTS"]
