"""
Unity Catalog Operations

All operation types supported by Unity Catalog provider.
Operations are prefixed with 'unity.' to indicate the provider.
"""

from schemax.providers.base.operations import ManagedCategory, OperationCategory, OperationMetadata

# Operation type constants
UNITY_OPERATIONS = {
    # Catalog operations
    "ADD_CATALOG": "unity.add_catalog",
    "RENAME_CATALOG": "unity.rename_catalog",
    "UPDATE_CATALOG": "unity.update_catalog",
    "DROP_CATALOG": "unity.drop_catalog",
    # Schema operations
    "ADD_SCHEMA": "unity.add_schema",
    "RENAME_SCHEMA": "unity.rename_schema",
    "UPDATE_SCHEMA": "unity.update_schema",
    "DROP_SCHEMA": "unity.drop_schema",
    # Table operations
    "ADD_TABLE": "unity.add_table",
    "RENAME_TABLE": "unity.rename_table",
    "DROP_TABLE": "unity.drop_table",
    "SET_TABLE_COMMENT": "unity.set_table_comment",
    "SET_TABLE_PROPERTY": "unity.set_table_property",
    "UNSET_TABLE_PROPERTY": "unity.unset_table_property",
    # Table tag operations
    "SET_TABLE_TAG": "unity.set_table_tag",
    "UNSET_TABLE_TAG": "unity.unset_table_tag",
    # View operations
    "ADD_VIEW": "unity.add_view",
    "RENAME_VIEW": "unity.rename_view",
    "DROP_VIEW": "unity.drop_view",
    "UPDATE_VIEW": "unity.update_view",
    "SET_VIEW_COMMENT": "unity.set_view_comment",
    "SET_VIEW_PROPERTY": "unity.set_view_property",
    "UNSET_VIEW_PROPERTY": "unity.unset_view_property",
    # Column operations
    "ADD_COLUMN": "unity.add_column",
    "RENAME_COLUMN": "unity.rename_column",
    "DROP_COLUMN": "unity.drop_column",
    "REORDER_COLUMNS": "unity.reorder_columns",
    "CHANGE_COLUMN_TYPE": "unity.change_column_type",
    "SET_NULLABLE": "unity.set_nullable",
    "SET_COLUMN_COMMENT": "unity.set_column_comment",
    # Column tag operations
    "SET_COLUMN_TAG": "unity.set_column_tag",
    "UNSET_COLUMN_TAG": "unity.unset_column_tag",
    # Constraint operations
    "ADD_CONSTRAINT": "unity.add_constraint",
    "DROP_CONSTRAINT": "unity.drop_constraint",
    # Row filter operations
    "ADD_ROW_FILTER": "unity.add_row_filter",
    "UPDATE_ROW_FILTER": "unity.update_row_filter",
    "REMOVE_ROW_FILTER": "unity.remove_row_filter",
    # Column mask operations
    "ADD_COLUMN_MASK": "unity.add_column_mask",
    "UPDATE_COLUMN_MASK": "unity.update_column_mask",
    "REMOVE_COLUMN_MASK": "unity.remove_column_mask",
    # Grant operations
    "ADD_GRANT": "unity.add_grant",
    "REVOKE_GRANT": "unity.revoke_grant",
}

# Export operation metadata for UI and validation
unity_operation_metadata = [
    # Catalog operations
    OperationMetadata(
        type=UNITY_OPERATIONS["ADD_CATALOG"],
        display_name="Add Catalog",
        description="Create a new catalog",
        category=OperationCategory.CATALOG,
        required_fields=["catalogId", "name"],
        optional_fields=["managedLocationName", "comment"],
        is_destructive=False,
        managed_category=ManagedCategory.CATALOG_STRUCTURE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["RENAME_CATALOG"],
        display_name="Rename Catalog",
        description="Rename an existing catalog",
        category=OperationCategory.CATALOG,
        required_fields=["newName"],
        optional_fields=[],
        is_destructive=False,
        managed_category=ManagedCategory.CATALOG_STRUCTURE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["UPDATE_CATALOG"],
        display_name="Update Catalog",
        description="Update catalog properties (e.g., managed location)",
        category=OperationCategory.CATALOG,
        required_fields=[],
        optional_fields=["managedLocationName"],
        is_destructive=False,
        managed_category=ManagedCategory.CATALOG_STRUCTURE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["DROP_CATALOG"],
        display_name="Drop Catalog",
        description="Delete a catalog and all its contents",
        category=OperationCategory.CATALOG,
        required_fields=[],
        optional_fields=[],
        is_destructive=True,
        managed_category=ManagedCategory.CATALOG_STRUCTURE,
    ),
    # Schema operations
    OperationMetadata(
        type=UNITY_OPERATIONS["ADD_SCHEMA"],
        display_name="Add Schema",
        description="Create a new schema in a catalog",
        category=OperationCategory.SCHEMA,
        required_fields=["schemaId", "name", "catalogId"],
        optional_fields=["managedLocationName", "comment"],
        is_destructive=False,
        managed_category=ManagedCategory.SCHEMA_STRUCTURE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["RENAME_SCHEMA"],
        display_name="Rename Schema",
        description="Rename an existing schema",
        category=OperationCategory.SCHEMA,
        required_fields=["newName"],
        optional_fields=[],
        is_destructive=False,
        managed_category=ManagedCategory.SCHEMA_STRUCTURE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["UPDATE_SCHEMA"],
        display_name="Update Schema",
        description="Update schema properties (e.g., managed location)",
        category=OperationCategory.SCHEMA,
        required_fields=[],
        optional_fields=["managedLocationName"],
        is_destructive=False,
        managed_category=ManagedCategory.SCHEMA_STRUCTURE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["DROP_SCHEMA"],
        display_name="Drop Schema",
        description="Delete a schema and all its contents",
        category=OperationCategory.SCHEMA,
        required_fields=[],
        optional_fields=[],
        is_destructive=True,
        managed_category=ManagedCategory.SCHEMA_STRUCTURE,
    ),
    # Table operations
    OperationMetadata(
        type=UNITY_OPERATIONS["ADD_TABLE"],
        display_name="Add Table",
        description="Create a new managed or external table in a schema",
        category=OperationCategory.TABLE,
        required_fields=["tableId", "name", "schemaId", "format"],
        optional_fields=[
            "external",
            "externalLocationName",
            "path",
            "partitionColumns",
            "clusterColumns",
            "comment",
        ],
        is_destructive=False,
        managed_category=ManagedCategory.TABLE_STRUCTURE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["RENAME_TABLE"],
        display_name="Rename Table",
        description="Rename an existing table",
        category=OperationCategory.TABLE,
        required_fields=["newName"],
        optional_fields=[],
        is_destructive=False,
        managed_category=ManagedCategory.TABLE_STRUCTURE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["DROP_TABLE"],
        display_name="Drop Table",
        description="Delete a table",
        category=OperationCategory.TABLE,
        required_fields=[],
        optional_fields=[
            "name",
            "catalogId",
            "schemaId",
        ],  # Added for SQL generation when table no longer in state
        is_destructive=True,
        managed_category=ManagedCategory.TABLE_STRUCTURE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["SET_TABLE_COMMENT"],
        display_name="Set Table Comment",
        description="Add or update table comment",
        category=OperationCategory.METADATA,
        required_fields=["tableId", "comment"],
        optional_fields=[],
        is_destructive=False,
        managed_category=ManagedCategory.GOVERNANCE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["SET_TABLE_PROPERTY"],
        display_name="Set Table Property",
        description="Set a table property (TBLPROPERTIES)",
        category=OperationCategory.METADATA,
        required_fields=["tableId", "key", "value"],
        optional_fields=[],
        is_destructive=False,
        managed_category=ManagedCategory.GOVERNANCE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["UNSET_TABLE_PROPERTY"],
        display_name="Unset Table Property",
        description="Remove a table property",
        category=OperationCategory.METADATA,
        required_fields=["tableId", "key"],
        optional_fields=[],
        is_destructive=False,
        managed_category=ManagedCategory.GOVERNANCE,
    ),
    # Table tag operations
    OperationMetadata(
        type=UNITY_OPERATIONS["SET_TABLE_TAG"],
        display_name="Set Table Tag",
        description="Set a Unity Catalog table tag (for governance)",
        category=OperationCategory.METADATA,
        required_fields=["tableId", "tagName", "tagValue"],
        optional_fields=[],
        is_destructive=False,
        managed_category=ManagedCategory.GOVERNANCE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["UNSET_TABLE_TAG"],
        display_name="Unset Table Tag",
        description="Remove a Unity Catalog table tag",
        category=OperationCategory.METADATA,
        required_fields=["tableId", "tagName"],
        optional_fields=[],
        is_destructive=False,
        managed_category=ManagedCategory.GOVERNANCE,
    ),
    # View operations
    OperationMetadata(
        type=UNITY_OPERATIONS["ADD_VIEW"],
        display_name="Add View",
        description="Create a new view in a schema",
        category=OperationCategory.TABLE,  # Views are table-like objects
        required_fields=["viewId", "name", "schemaId", "definition"],
        optional_fields=["comment", "dependencies", "extractedDependencies"],
        is_destructive=False,
        managed_category=ManagedCategory.VIEW_STRUCTURE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["RENAME_VIEW"],
        display_name="Rename View",
        description="Rename an existing view",
        category=OperationCategory.TABLE,
        required_fields=["newName"],
        optional_fields=[],
        is_destructive=False,
        managed_category=ManagedCategory.VIEW_STRUCTURE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["DROP_VIEW"],
        display_name="Drop View",
        description="Delete a view",
        category=OperationCategory.TABLE,
        required_fields=[],
        optional_fields=["name", "catalogId", "schemaId"],  # For SQL generation
        is_destructive=True,
        managed_category=ManagedCategory.VIEW_STRUCTURE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["UPDATE_VIEW"],
        display_name="Update View",
        description="Update view definition or dependencies",
        category=OperationCategory.TABLE,
        required_fields=[],
        optional_fields=["definition", "dependencies", "extractedDependencies"],
        is_destructive=False,
        managed_category=ManagedCategory.VIEW_STRUCTURE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["SET_VIEW_COMMENT"],
        display_name="Set View Comment",
        description="Add or update view comment",
        category=OperationCategory.METADATA,
        required_fields=["viewId", "comment"],
        optional_fields=[],
        is_destructive=False,
        managed_category=ManagedCategory.GOVERNANCE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["SET_VIEW_PROPERTY"],
        display_name="Set View Property",
        description="Set a view property",
        category=OperationCategory.METADATA,
        required_fields=["viewId", "key", "value"],
        optional_fields=[],
        is_destructive=False,
        managed_category=ManagedCategory.GOVERNANCE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["UNSET_VIEW_PROPERTY"],
        display_name="Unset View Property",
        description="Remove a view property",
        category=OperationCategory.METADATA,
        required_fields=["viewId", "key"],
        optional_fields=[],
        is_destructive=False,
        managed_category=ManagedCategory.GOVERNANCE,
    ),
    # Column operations
    OperationMetadata(
        type=UNITY_OPERATIONS["ADD_COLUMN"],
        display_name="Add Column",
        description="Add a new column to a table",
        category=OperationCategory.COLUMN,
        required_fields=["tableId", "colId", "name", "type", "nullable"],
        optional_fields=["comment"],
        is_destructive=False,
        managed_category=ManagedCategory.TABLE_STRUCTURE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["RENAME_COLUMN"],
        display_name="Rename Column",
        description="Rename an existing column",
        category=OperationCategory.COLUMN,
        required_fields=["tableId", "newName"],
        optional_fields=[],
        is_destructive=False,
        managed_category=ManagedCategory.TABLE_STRUCTURE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["DROP_COLUMN"],
        display_name="Drop Column",
        description="Delete a column from a table",
        category=OperationCategory.COLUMN,
        required_fields=["tableId"],
        optional_fields=[],
        is_destructive=True,
        managed_category=ManagedCategory.TABLE_STRUCTURE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["REORDER_COLUMNS"],
        display_name="Reorder Columns",
        description="Change the order of columns in a table",
        category=OperationCategory.COLUMN,
        required_fields=["tableId", "order"],
        optional_fields=[],
        is_destructive=False,
        managed_category=ManagedCategory.TABLE_STRUCTURE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["CHANGE_COLUMN_TYPE"],
        display_name="Change Column Type",
        description="Change the data type of a column",
        category=OperationCategory.COLUMN,
        required_fields=["tableId", "newType"],
        optional_fields=[],
        is_destructive=False,
        managed_category=ManagedCategory.TABLE_STRUCTURE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["SET_NULLABLE"],
        display_name="Set Column Nullable",
        description="Change whether a column allows NULL values",
        category=OperationCategory.COLUMN,
        required_fields=["tableId", "nullable"],
        optional_fields=[],
        is_destructive=False,
        managed_category=ManagedCategory.TABLE_STRUCTURE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["SET_COLUMN_COMMENT"],
        display_name="Set Column Comment",
        description="Add or update column comment",
        category=OperationCategory.METADATA,
        required_fields=["tableId", "comment"],
        optional_fields=[],
        is_destructive=False,
        managed_category=ManagedCategory.GOVERNANCE,
    ),
    # Column tag operations
    OperationMetadata(
        type=UNITY_OPERATIONS["SET_COLUMN_TAG"],
        display_name="Set Column Tag",
        description="Set a tag on a column",
        category=OperationCategory.METADATA,
        required_fields=["tableId", "tagName", "tagValue"],
        optional_fields=[],
        is_destructive=False,
        managed_category=ManagedCategory.GOVERNANCE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["UNSET_COLUMN_TAG"],
        display_name="Unset Column Tag",
        description="Remove a tag from a column",
        category=OperationCategory.METADATA,
        required_fields=["tableId", "tagName"],
        optional_fields=[],
        is_destructive=False,
        managed_category=ManagedCategory.GOVERNANCE,
    ),
    # Constraint operations
    OperationMetadata(
        type=UNITY_OPERATIONS["ADD_CONSTRAINT"],
        display_name="Add Constraint",
        description="Add a constraint (PRIMARY KEY, FOREIGN KEY, CHECK)",
        category=OperationCategory.CONSTRAINT,
        required_fields=["tableId", "constraintId", "type", "columns"],
        optional_fields=[
            "name",
            "timeseries",
            "parentTable",
            "parentColumns",
            "expression",
            "notEnforced",
            "rely",
        ],
        is_destructive=False,
        managed_category=ManagedCategory.TABLE_STRUCTURE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["DROP_CONSTRAINT"],
        display_name="Drop Constraint",
        description="Remove a constraint from a table",
        category=OperationCategory.CONSTRAINT,
        required_fields=["tableId"],
        optional_fields=[],
        is_destructive=True,
        managed_category=ManagedCategory.TABLE_STRUCTURE,
    ),
    # Row filter operations
    OperationMetadata(
        type=UNITY_OPERATIONS["ADD_ROW_FILTER"],
        display_name="Add Row Filter",
        description="Add row-level security filter",
        category=OperationCategory.SECURITY,
        required_fields=["tableId", "filterId", "name", "udfExpression"],
        optional_fields=["enabled", "description", "columnNames"],
        is_destructive=False,
        managed_category=ManagedCategory.GOVERNANCE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["UPDATE_ROW_FILTER"],
        display_name="Update Row Filter",
        description="Update an existing row filter",
        category=OperationCategory.SECURITY,
        required_fields=["tableId"],
        optional_fields=["name", "udfExpression", "enabled", "description"],
        is_destructive=False,
        managed_category=ManagedCategory.GOVERNANCE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["REMOVE_ROW_FILTER"],
        display_name="Remove Row Filter",
        description="Remove a row filter",
        category=OperationCategory.SECURITY,
        required_fields=["tableId"],
        optional_fields=[],
        is_destructive=True,
        managed_category=ManagedCategory.GOVERNANCE,
    ),
    # Column mask operations
    OperationMetadata(
        type=UNITY_OPERATIONS["ADD_COLUMN_MASK"],
        display_name="Add Column Mask",
        description="Add column-level data masking",
        category=OperationCategory.SECURITY,
        required_fields=["tableId", "maskId", "columnId", "name", "maskFunction"],
        optional_fields=["enabled", "description"],
        is_destructive=False,
        managed_category=ManagedCategory.GOVERNANCE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["UPDATE_COLUMN_MASK"],
        display_name="Update Column Mask",
        description="Update an existing column mask",
        category=OperationCategory.SECURITY,
        required_fields=["tableId"],
        optional_fields=["name", "maskFunction", "enabled", "description"],
        is_destructive=False,
        managed_category=ManagedCategory.GOVERNANCE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["REMOVE_COLUMN_MASK"],
        display_name="Remove Column Mask",
        description="Remove a column mask",
        category=OperationCategory.SECURITY,
        required_fields=["tableId"],
        optional_fields=[],
        is_destructive=True,
        managed_category=ManagedCategory.GOVERNANCE,
    ),
    # Grant operations
    OperationMetadata(
        type=UNITY_OPERATIONS["ADD_GRANT"],
        display_name="Add Grant",
        description="Grant privileges on a catalog, schema, table, or view to a principal",
        category=OperationCategory.SECURITY,
        required_fields=["targetType", "targetId", "principal", "privileges"],
        optional_fields=[],
        is_destructive=False,
        managed_category=ManagedCategory.GOVERNANCE,
    ),
    OperationMetadata(
        type=UNITY_OPERATIONS["REVOKE_GRANT"],
        display_name="Revoke Grant",
        description="Revoke privileges from a principal on a catalog, schema, table, or view",
        category=OperationCategory.SECURITY,
        required_fields=["targetType", "targetId", "principal"],
        optional_fields=["privileges"],
        is_destructive=False,
        managed_category=ManagedCategory.GOVERNANCE,
    ),
]
