# Copyright 2026 EcoFuture Technology Services LLC and contributors
#
# 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
#
#     http://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.

from bazis.contrib.users.routes_abstract import UserRequiredRouteBase, UserRouteBase
from bazis.core.routes_abstract.jsonapi import JsonapiRouteBase
from bazis.core.schemas import ApiAction, CrudApiAction, SchemaFields

from .models_abstract import AuthorMixin


class AuthorRouteMixin(JsonapiRouteBase):
    abstract: bool = True

    fields: dict[ApiAction, SchemaFields] = {
        CrudApiAction.CREATE: SchemaFields(
            exclude={'author': None, 'author_updated': None},
        ),
        CrudApiAction.UPDATE: SchemaFields(
            exclude={'author': None, 'author_updated': None},
        ),
    }

    def hook_before_create(self, item: AuthorMixin):
        if not self.inject.user.is_anonymous:
            item.author = self.inject.user
        super().hook_before_create(item)

    def hook_before_update(self, item: AuthorMixin):
        if not self.inject.user.is_anonymous:
            item.author_updated = self.inject.user
        super().hook_before_update(item)


class AuthorRouteBase(AuthorRouteMixin, UserRouteBase):
    abstract: bool = True


class AuthorRequiredRouteBase(AuthorRouteMixin, UserRequiredRouteBase):
    abstract: bool = True
