"""Variant definitions for Query 16."""

from __future__ import annotations

from benchbox.core.tpchavoc.variant_base import StaticSQLVariant

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

select
    p_brand,
    p_type,
    p_size,
    (select count(distinct ps2.ps_suppkey)
     from partsupp ps2, part p2
     where p2.p_partkey = ps2.ps_partkey
       and p2.p_brand = p.p_brand
       and p2.p_type = p.p_type
       and p2.p_size = p.p_size
       and ps2.ps_suppkey not in (
           select s_suppkey
           from supplier
           where s_comment like '%Customer%Complaints%'
       )) as supplier_cnt
from
    part p
where
    p_brand <> 'Brand#45'
    and p_type not like 'MEDIUM POLISHED%'
    and p_size in (49, 14, 23, 45, 19, 3, 36, 9)
    and exists (
        select 1
        from partsupp ps3
        where ps3.ps_partkey = p.p_partkey
          and ps3.ps_suppkey not in (
              select s_suppkey
              from supplier
              where s_comment like '%Customer%Complaints%'
          )
    )
group by
    p_brand,
    p_type,
    p_size
order by
    supplier_cnt desc,
    p_brand,
    p_type,
    p_size
""",
    ),
    2: StaticSQLVariant(
        2,
        "HAVING clause: Use HAVING for post-aggregation filtering",
        """\

select
    p_brand,
    p_type,
    p_size,
    count(distinct ps_suppkey) as supplier_cnt
from
    partsupp,
    part
where
    p_partkey = ps_partkey
    and p_brand <> 'Brand#45'
    and p_type not like 'MEDIUM POLISHED%'
    and p_size in (49, 14, 23, 45, 19, 3, 36, 9)
    and ps_suppkey not in (
        select
            s_suppkey
        from
            supplier
        where
            s_comment like '%Customer%Complaints%'
    )
group by
    p_brand,
    p_type,
    p_size
having
    count(distinct ps_suppkey) > 0
order by
    supplier_cnt desc,
    p_brand,
    p_type,
    p_size
""",
    ),
    3: StaticSQLVariant(
        3,
        "Explicit JOINs: Replace comma-separated tables with explicit JOINs",
        """\

select
    p.p_brand,
    p.p_type,
    p.p_size,
    count(distinct ps.ps_suppkey) as supplier_cnt
from
    part p
    inner join partsupp ps on p.p_partkey = ps.ps_partkey
    left join (
        select s_suppkey
        from supplier
        where s_comment like '%Customer%Complaints%'
    ) excluded_suppliers on ps.ps_suppkey = excluded_suppliers.s_suppkey
where
    p.p_brand <> 'Brand#45'
    and p.p_type not like 'MEDIUM POLISHED%'
    and p.p_size in (49, 14, 23, 45, 19, 3, 36, 9)
    and excluded_suppliers.s_suppkey is null
group by
    p.p_brand,
    p.p_type,
    p.p_size
order by
    supplier_cnt desc,
    p.p_brand,
    p.p_type,
    p.p_size
""",
    ),
    4: StaticSQLVariant(
        4,
        "UNION ALL: Split query by conditions",
        """\

select
    p_brand,
    p_type,
    p_size,
    count(distinct ps_suppkey) as supplier_cnt
from (
    select
        p.p_brand,
        p.p_type,
        p.p_size,
        ps.ps_suppkey
    from
        partsupp ps,
        part p
    where
        p.p_partkey = ps.ps_partkey
        and p.p_brand <> 'Brand#45'
        and p.p_type not like 'MEDIUM POLISHED%'
        and p.p_size in (49, 14, 23, 45, 19, 3, 36, 9)
        and ps.ps_suppkey not in (
            select s_suppkey
            from supplier
            where s_comment like '%Customer%Complaints%'
        )

    union all

    select
        p.p_brand,
        p.p_type,
        p.p_size,
        null as ps_suppkey
    from
        part p
    where
        p.p_brand <> 'Brand#45'
        and p.p_type not like 'MEDIUM POLISHED%'
        and p.p_size in (49, 14, 23, 45, 19, 3, 36, 9)
        and not exists (
            select 1
            from partsupp ps2
            where ps2.ps_partkey = p.p_partkey
              and ps2.ps_suppkey not in (
                  select s_suppkey
                  from supplier
                  where s_comment like '%Customer%Complaints%'
              )
        )
) combined_results
where ps_suppkey is not null
group by
    p_brand,
    p_type,
    p_size
order by
    supplier_cnt desc,
    p_brand,
    p_type,
    p_size
""",
    ),
    5: StaticSQLVariant(
        5,
        "CTE: Use common table expressions for modular design",
        """\

with excluded_suppliers as (
    select
        s_suppkey
    from
        supplier
    where
        s_comment like '%Customer%Complaints%'
),
filtered_parts as (
    select
        p_partkey,
        p_brand,
        p_type,
        p_size
    from
        part
    where
        p_brand <> 'Brand#45'
        and p_type not like 'MEDIUM POLISHED%'
        and p_size in (49, 14, 23, 45, 19, 3, 36, 9)
),
valid_partsupp as (
    select
        fp.p_brand,
        fp.p_type,
        fp.p_size,
        ps.ps_suppkey
    from
        filtered_parts fp
        inner join partsupp ps on fp.p_partkey = ps.ps_partkey
        left join excluded_suppliers es on ps.ps_suppkey = es.s_suppkey
    where
        es.s_suppkey is null
)
select
    p_brand,
    p_type,
    p_size,
    count(distinct ps_suppkey) as supplier_cnt
from
    valid_partsupp
group by
    p_brand,
    p_type,
    p_size
order by
    supplier_cnt desc,
    p_brand,
    p_type,
    p_size
""",
    ),
    6: StaticSQLVariant(
        6,
        "Derived table: Use derived tables for subqueries",
        """\

select
    supplier_part_data.p_brand,
    supplier_part_data.p_type,
    supplier_part_data.p_size,
    count(distinct supplier_part_data.ps_suppkey) as supplier_cnt
from
    (select
        p.p_brand,
        p.p_type,
        p.p_size,
        ps.ps_suppkey
     from
        partsupp ps,
        part p,
        (select s_suppkey as excluded_suppkey
         from supplier
         where s_comment like '%Customer%Complaints%') excluded
     where
        p.p_partkey = ps.ps_partkey
        and p.p_brand <> 'Brand#45'
        and p.p_type not like 'MEDIUM POLISHED%'
        and p.p_size in (49, 14, 23, 45, 19, 3, 36, 9)
        and ps.ps_suppkey <> excluded.excluded_suppkey) supplier_part_data
group by
    supplier_part_data.p_brand,
    supplier_part_data.p_type,
    supplier_part_data.p_size
order by
    supplier_cnt desc,
    supplier_part_data.p_brand,
    supplier_part_data.p_type,
    supplier_part_data.p_size
""",
    ),
    7: StaticSQLVariant(
        7,
        "FILTER clause (OLAP): Use FILTER for conditional aggregation",
        """\

select
    p_brand,
    p_type,
    p_size,
    count(distinct ps_suppkey) filter (
        where ps_suppkey not in (
            select s_suppkey
            from supplier
            where s_comment like '%Customer%Complaints%'
        )
    ) as supplier_cnt
from
    partsupp,
    part
where
    p_partkey = ps_partkey
    and p_brand <> 'Brand#45'
    and p_type not like 'MEDIUM POLISHED%'
    and p_size in (49, 14, 23, 45, 19, 3, 36, 9)
group by
    p_brand,
    p_type,
    p_size
having
    count(distinct ps_suppkey) filter (
        where ps_suppkey not in (
            select s_suppkey
            from supplier
            where s_comment like '%Customer%Complaints%'
        )
    ) > 0
order by
    supplier_cnt desc,
    p_brand,
    p_type,
    p_size
""",
    ),
    8: StaticSQLVariant(
        8,
        "EXISTS pattern: Use EXISTS instead of NOT IN",
        """\

select
    p_brand,
    p_type,
    p_size,
    count(distinct ps_suppkey) as supplier_cnt
from
    partsupp,
    part
where
    p_partkey = ps_partkey
    and p_brand <> 'Brand#45'
    and p_type not like 'MEDIUM POLISHED%'
    and p_size in (49, 14, 23, 45, 19, 3, 36, 9)
    and not exists (
        select 1
        from supplier
        where s_suppkey = ps_suppkey
          and s_comment like '%Customer%Complaints%'
    )
group by
    p_brand,
    p_type,
    p_size
order by
    supplier_cnt desc,
    p_brand,
    p_type,
    p_size
""",
    ),
    9: StaticSQLVariant(
        9,
        "Window functions (OLAP): Use window functions for ranking",
        """\

with supplier_counts as (
    select
        p_brand,
        p_type,
        p_size,
        count(distinct ps_suppkey) as supplier_cnt,
        rank() over (order by count(distinct ps_suppkey) desc, p_brand, p_type, p_size) as cnt_rank
    from
        partsupp,
        part
    where
        p_partkey = ps_partkey
        and p_brand <> 'Brand#45'
        and p_type not like 'MEDIUM POLISHED%'
        and p_size in (49, 14, 23, 45, 19, 3, 36, 9)
        and ps_suppkey not in (
            select
                s_suppkey
            from
                supplier
            where
                s_comment like '%Customer%Complaints%'
        )
    group by
        p_brand,
        p_type,
        p_size
)
select
    p_brand,
    p_type,
    p_size,
    supplier_cnt
from
    supplier_counts
order by
    supplier_cnt desc,
    p_brand,
    p_type,
    p_size
""",
    ),
    10: StaticSQLVariant(
        10,
        "CASE expressions: Use CASE for conditional logic",
        """\

select
    p_brand,
    p_type,
    p_size,
    count(distinct
        case
            when ps_suppkey not in (
                select s_suppkey
                from supplier
                where s_comment like '%Customer%Complaints%'
            ) then ps_suppkey
            else null
        end
    ) as supplier_cnt
from
    partsupp,
    part
where
    p_partkey = ps_partkey
    and case
        when p_brand <> 'Brand#45' then 1
        else 0
    end = 1
    and case
        when p_type not like 'MEDIUM POLISHED%' then 1
        else 0
    end = 1
    and case
        when p_size in (49, 14, 23, 45, 19, 3, 36, 9) then 1
        else 0
    end = 1
group by
    p_brand,
    p_type,
    p_size
having
    count(distinct
        case
            when ps_suppkey not in (
                select s_suppkey
                from supplier
                where s_comment like '%Customer%Complaints%'
            ) then ps_suppkey
            else null
        end
    ) > 0
order by
    supplier_cnt desc,
    p_brand,
    p_type,
    p_size
""",
    ),
}

__all__ = ["VARIANTS"]
