import copy
from django.db import NotSupportedError
from django.db.models import Expression

from .fields import mysql_compile_json_path, postgres_compile_json_path, FallbackJSONField


class JSONExtract(Expression):
    def __init__(self, expression, *path, output_field=FallbackJSONField(), **extra):
        super().__init__(output_field=output_field)
        self.path = path
        self.source_expression = self._parse_expressions(expression)[0]
        self.extra = extra

    def resolve_expression(self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False):
        c = self.copy()
        c.is_summary = summarize
        c.source_expression = c.source_expression.resolve_expression(query, allow_joins, reuse, summarize, for_save)
        return c

    def as_postgresql(self, compiler, connection, function=None, template=None, arg_joiner=None, **extra_context):
        params = []
        arg_sql, arg_params = compiler.compile(self.source_expression)
        params.extend(arg_params)
        json_path = postgres_compile_json_path(self.path)
        params.append(json_path)
        template = '{} #> %s'.format(arg_sql)
        return template, params

    def as_mysql(self, compiler, connection, function=None, template=None, arg_joiner=None, **extra_context):
        params = []
        arg_sql, arg_params = compiler.compile(self.source_expression)
        params.extend(arg_params)
        json_path = mysql_compile_json_path(self.path)
        params.append(json_path)
        template = 'JSON_EXTRACT({}, %s)'.format(arg_sql)
        return template, params

    def as_sql(self, compiler, connection, function=None, template=None, arg_joiner=None, **extra_context):
        raise NotSupportedError(
            'Functions on JSONFields are only supported on PostgreSQL and MySQL at the moment.'
        )

    def copy(self):
        c = super().copy()
        c.source_expression = copy.copy(self.source_expression)
        c.extra = self.extra.copy()
        return c

[((40, 14, 42, 9), 'django.db.NotSupportedError', 'NotSupportedError', ({(41, 12, 41, 95): '"""Functions on JSONFields are only supported on PostgreSQL and MySQL at the moment."""'}, {}), "(\n    'Functions on JSONFields are only supported on PostgreSQL and MySQL at the moment.'\n    )", False, 'from django.db import NotSupportedError\n'), ((46, 30, 46, 63), 'copy.copy', 'copy.copy', ({(46, 40, 46, 62): 'self.source_expression'}, {}), '(self.source_expression)', False, 'import copy\n')]