#  Copyright (C) 2023
#  ABM, Moscow
#
#  UNPUBLISHED PROPRIETARY MATERIAL.
#  ALL RIGHTS RESERVED.
#
#  Authors: Mike Orlov <m.orlov@abm-jsc.ru>
from dataclasses import dataclass, field
from typing import Type

# from ..node import NodeType
from entity_read.sql.atoms.node import Node
from .abstract import Selectable, T
from .column import Column


@dataclass(kw_only=True, frozen=True, slots=True)
class Alias(Selectable):
    type_: Type[T] = field(init=False)
    is_nullable: bool = field(init=False)

    body: Selectable
    key: str

    def __post_init__(self):
        object.__setattr__(self, "type_", self.body.type_)
        object.__setattr__(self, "is_nullable", self.body.is_nullable)

    def to_sql(self, context, key_to_expression = None) -> str:
        return self.body.to_sql(context, key_to_expression)

    def requires(self) -> list['Column']:
        return self.body.requires()

    def get_name(self) -> str:
        return self.key
