from dataclasses import dataclass, field
from typing import ClassVar, Any

from entity_read.sql.atoms import Function, Selectable


@dataclass(kw_only=True, frozen=True)
class FunctionJsonGet(Function):
    type_: str = field(default=str, init=False)
    args: tuple[Selectable[Any]]
    code: ClassVar[str] = 'json_get'

    def _to_sql(self, args_sql: list) -> str:
        return f"{args_sql[0]} ->> {args_sql[1]}"


@dataclass(kw_only=True, frozen=True)
class FunctionJsonHas(Function):
    type_: str = field(default=str, init=False)
    args: tuple[Selectable[Any]]
    code: ClassVar[str] = 'json_has'

    def _to_sql(self, args_sql: list):
        return f"{args_sql[0]} ? {args_sql[1]}"
