import logging
import sys
import typing as t

from sqlglot import exp

from sqlframe.base.catalog import Column
from sqlframe.base.dataframe import (
    GROUP_DATA,
    NA,
    SESSION,
    STAT,
    WRITER,
    _BaseDataFrame,
)

if sys.version_info >= (3, 11):
    from typing import Self
else:
    from typing_extensions import Self


logger = logging.getLogger(__name__)


class NoCachePersistSupportMixin(_BaseDataFrame, t.Generic[SESSION, WRITER, NA, STAT, GROUP_DATA]):
    def cache(self) -> Self:
        logger.warning("This engine does not support caching. Ignoring cache() call.")
        return self

    def persist(self) -> Self:
        logger.warning("This engine does not support persist. Ignoring persist() call.")
        return self


class TypedColumnsFromTempViewMixin(
    _BaseDataFrame, t.Generic[SESSION, WRITER, NA, STAT, GROUP_DATA]
):
    @property
    def _typed_columns(self) -> t.List[Column]:
        table = exp.to_table(self.session._random_id)
        self.session._collect(
            exp.Create(
                this=table,
                kind="VIEW",
                replace=True,
                properties=exp.Properties(expressions=[exp.TemporaryProperty()]),
                expression=self.expression,
            )
        )

        return self.session.catalog.listColumns(
            table.sql(dialect=self.session.input_dialect), include_temp=True
        )
