#  Copyright (c) ZenML GmbH 2025. All Rights Reserved.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at:
#
#       https://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
#  or implied. See the License for the specific language governing
#  permissions and limitations under the License.
"""SQLModel implementation of idempotent API transaction tables."""

from datetime import datetime, timedelta
from typing import Any, Optional
from uuid import UUID

from sqlalchemy import TEXT, Column, LargeBinary
from sqlalchemy.dialects.mysql import MEDIUMBLOB
from sqlmodel import Field, SQLModel

from zenml.models import (
    ApiTransactionRequest,
    ApiTransactionResponse,
    ApiTransactionResponseBody,
    ApiTransactionUpdate,
)
from zenml.utils.time_utils import utc_now
from zenml.zen_stores.schemas.base_schemas import BaseSchema
from zenml.zen_stores.schemas.schema_utils import (
    build_foreign_key_field,
    build_index,
)
from zenml.zen_stores.schemas.user_schemas import UserSchema


class ApiTransactionSchema(BaseSchema, table=True):
    """SQL Model for API transactions.

    The result payload is stored in a separate table to keep this table
    small and fast for cleanup operations. Deleting rows with large blobs
    is expensive because the entire row must be copied to the undo log.
    """

    __tablename__ = "api_transaction"
    __table_args__ = (
        build_index(
            table_name=__tablename__,
            column_names=[
                "completed",
                "expired",
            ],
        ),
    )
    method: str
    url: str = Field(sa_column=Column(TEXT, nullable=False))
    completed: bool = Field(default=False)
    expired: Optional[datetime] = Field(default=None, nullable=True)

    user_id: UUID = build_foreign_key_field(
        source=__tablename__,
        target=UserSchema.__tablename__,
        source_column="user_id",
        target_column="id",
        ondelete="CASCADE",
        nullable=False,
    )

    @classmethod
    def from_request(
        cls, request: ApiTransactionRequest
    ) -> "ApiTransactionSchema":
        """Create a new API transaction from a request.

        Args:
            request: The API transaction request.

        Returns:
            The API transaction schema.
        """
        assert request.user is not None, "User must be set."
        return cls(
            id=request.transaction_id,
            user_id=request.user,
            method=request.method,
            url=request.url,
            completed=False,
        )

    def to_model(
        self,
        include_metadata: bool = False,
        include_resources: bool = False,
        **kwargs: Any,
    ) -> ApiTransactionResponse:
        """Convert the SQL model to a ZenML model.

        Args:
            include_metadata: Whether to include metadata in the response.
            include_resources: Whether to include resources in the response.
            **kwargs: Additional keyword arguments.

        Returns:
            The API transaction response.
        """
        response = ApiTransactionResponse(
            id=self.id,
            body=ApiTransactionResponseBody(
                method=self.method,
                url=self.url,
                created=self.created,
                updated=self.updated,
                user_id=self.user_id,
                completed=self.completed,
            ),
        )
        return response

    def update(self, update: ApiTransactionUpdate) -> "ApiTransactionSchema":
        """Update the API transaction.

        Args:
            update: The API transaction update.

        Returns:
            The API transaction schema.
        """
        self.updated = utc_now()
        self.expired = self.updated + timedelta(seconds=update.cache_time)
        return self


class ApiTransactionResultSchema(SQLModel, table=True):
    """SQL Model for API transaction results."""

    __tablename__ = "api_transaction_result"

    id: UUID = build_foreign_key_field(
        source=__tablename__,
        target=ApiTransactionSchema.__tablename__,
        source_column="id",
        target_column="id",
        ondelete="CASCADE",
        nullable=False,
        primary_key=True,
    )

    result: bytes = Field(
        sa_column=Column(
            LargeBinary().with_variant(MEDIUMBLOB, "mysql"),
            nullable=False,
        ),
    )
