# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import typing

import pytest

from bigframes import operations as ops
from bigframes.core import expression as ex
import bigframes.pandas as bpd
from bigframes.testing import utils

pytest.importorskip("pytest_snapshot")


def _apply_nary_op(
    obj: bpd.DataFrame,
    op: ops.NaryOp,
    *args: typing.Union[str, ex.Expression],
) -> str:
    """Applies a nary op to the given DataFrame and return the SQL representing
    the resulting DataFrame."""
    array_value = obj._block.expr
    op_expr = op.as_expr(*args)
    result, col_ids = array_value.compute_values([op_expr])

    # Rename columns for deterministic golden SQL results.
    assert len(col_ids) == 1
    result = result.rename_columns({col_ids[0]: "result_col"}).select_columns(
        ["result_col"]
    )

    sql = result.session._executor.to_sql(result, enable_cache=False)
    return sql


def test_struct_field(nested_structs_types_df: bpd.DataFrame, snapshot):
    col_name = "people"
    bf_df = nested_structs_types_df[[col_name]]

    ops_map = {
        # When a name string is provided.
        "string": ops.StructFieldOp("name").as_expr(col_name),
        # When an index integer is provided.
        "int": ops.StructFieldOp(0).as_expr(col_name),
    }
    sql = utils._apply_ops_to_sql(bf_df, list(ops_map.values()), list(ops_map.keys()))

    snapshot.assert_match(sql, "out.sql")


def test_struct_op(scalar_types_df: bpd.DataFrame, snapshot):
    bf_df = scalar_types_df[["bool_col", "int64_col", "float64_col", "string_col"]]
    op = ops.StructOp(column_names=tuple(bf_df.columns.tolist()))
    sql = _apply_nary_op(bf_df, op, *bf_df.columns.tolist())

    snapshot.assert_match(sql, "out.sql")
