from snowddl.blueprint import ObjectType
from snowddl.converter.abc_schema_object_converter import AbstractSchemaObjectConverter, ConvertResult
from snowddl.parser.sequence import sequence_json_schema


class SequenceConverter(AbstractSchemaObjectConverter):
    def get_object_type(self) -> ObjectType:
        return ObjectType.SEQUENCE

    def get_existing_objects_in_schema(self, schema: dict):
        existing_objects = {}

        cur = self.engine.execute_meta("SHOW SEQUENCES IN SCHEMA {database:i}.{schema:i}", {
            "database": schema['database'],
            "schema": schema['schema'],
        })

        for r in cur:
            existing_objects[f"{r['database_name']}.{r['schema_name']}.{r['name']}"] = {
                "database": r['database_name'],
                "schema": r['schema_name'],
                "name": r['name'],
                "owner": r['owner'],
                "next_value": r['next_value'],
                "interval": r['interval'],
                "comment": r['comment'] if r['comment'] else None,
            }

        return existing_objects

    def dump_object(self, row):
        data = {
            "interval": row['interval'],
            "comment": row['comment'],
        }

        object_path = self.base_path / self._normalise_name_with_prefix(row['database']) / self._normalise_name(row['schema']) / 'sequence'
        object_path.mkdir(mode=0o755, parents=True, exist_ok=True)

        if data:
            self._dump_file(object_path / f"{self._normalise_name(row['name'])}.yaml", data, sequence_json_schema)
            return ConvertResult.DUMP

        return ConvertResult.EMPTY
