"""
Secret Manager API API emulator.

Auto-generated from the GCP Discovery API.
Discovery doc: 
"""

from flask import Blueprint, jsonify, request as flask_request

from gcplocal.services.base import GCPService
from gcplocal.utils import error_response, gcp_id, self_link, timestamp_now, paginate

SVC = "secretmanager"
API_PREFIX = ""


class SecretmanagerService(GCPService):
    service_name = "secretmanager"
    api_prefix = API_PREFIX

    def register_routes(self, bp: Blueprint) -> None:
        p = self.api_prefix

        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/secrets/<secretsId>",
            "secretmanager_get_projects_secrets", self.get_projects_secrets, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/secrets",
            "secretmanager_create_projects_secrets", self.create_projects_secrets, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/secrets/<secretsId>:setIamPolicy",
            "secretmanager_setIamPolicy_projects_secrets", self.setIamPolicy_projects_secrets, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/secrets/<secretsId>",
            "secretmanager_patch_projects_secrets", self.patch_projects_secrets, methods=["PATCH"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/secrets/<secretsId>:addVersion",
            "secretmanager_addVersion_projects_secrets", self.addVersion_projects_secrets, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/secrets/<secretsId>:getIamPolicy",
            "secretmanager_getIamPolicy_projects_secrets", self.getIamPolicy_projects_secrets, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/secrets",
            "secretmanager_list_projects_secrets", self.list_projects_secrets, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/secrets/<secretsId>",
            "secretmanager_delete_projects_secrets", self.delete_projects_secrets, methods=["DELETE"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/secrets/<secretsId>:testIamPermissions",
            "secretmanager_testIamPermissions_projects_secrets", self.testIamPermissions_projects_secrets, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/secrets/<secretsId>/versions",
            "secretmanager_list_projects_secrets_versions", self.list_projects_secrets_versions, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/secrets/<secretsId>/versions/<versionsId>",
            "secretmanager_get_projects_secrets_versions", self.get_projects_secrets_versions, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/secrets/<secretsId>/versions/<versionsId>:destroy",
            "secretmanager_destroy_projects_secrets_versions", self.destroy_projects_secrets_versions, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/secrets/<secretsId>/versions/<versionsId>:disable",
            "secretmanager_disable_projects_secrets_versions", self.disable_projects_secrets_versions, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/secrets/<secretsId>/versions/<versionsId>:access",
            "secretmanager_access_projects_secrets_versions", self.access_projects_secrets_versions, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/secrets/<secretsId>/versions/<versionsId>:enable",
            "secretmanager_enable_projects_secrets_versions", self.enable_projects_secrets_versions, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/locations",
            "secretmanager_list_projects_locations", self.list_projects_locations, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/locations/<locationsId>",
            "secretmanager_get_projects_locations", self.get_projects_locations, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/locations/<locationsId>/secrets",
            "secretmanager_create_projects_locations_secrets", self.create_projects_locations_secrets, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/locations/<locationsId>/secrets/<secretsId>",
            "secretmanager_get_projects_locations_secrets", self.get_projects_locations_secrets, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/locations/<locationsId>/secrets/<secretsId>:getIamPolicy",
            "secretmanager_getIamPolicy_projects_locations_secrets", self.getIamPolicy_projects_locations_secrets, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/locations/<locationsId>/secrets",
            "secretmanager_list_projects_locations_secrets", self.list_projects_locations_secrets, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/locations/<locationsId>/secrets/<secretsId>",
            "secretmanager_delete_projects_locations_secrets", self.delete_projects_locations_secrets, methods=["DELETE"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/locations/<locationsId>/secrets/<secretsId>:testIamPermissions",
            "secretmanager_testIamPermissions_projects_locations_secrets", self.testIamPermissions_projects_locations_secrets, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/locations/<locationsId>/secrets/<secretsId>:addVersion",
            "secretmanager_addVersion_projects_locations_secrets", self.addVersion_projects_locations_secrets, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/locations/<locationsId>/secrets/<secretsId>",
            "secretmanager_patch_projects_locations_secrets", self.patch_projects_locations_secrets, methods=["PATCH"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/locations/<locationsId>/secrets/<secretsId>:setIamPolicy",
            "secretmanager_setIamPolicy_projects_locations_secrets", self.setIamPolicy_projects_locations_secrets, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/locations/<locationsId>/secrets/<secretsId>/versions",
            "secretmanager_list_projects_locations_secrets_versions", self.list_projects_locations_secrets_versions, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/locations/<locationsId>/secrets/<secretsId>/versions/<versionsId>:enable",
            "secretmanager_enable_projects_locations_secrets_versions", self.enable_projects_locations_secrets_versions, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/locations/<locationsId>/secrets/<secretsId>/versions/<versionsId>",
            "secretmanager_get_projects_locations_secrets_versions", self.get_projects_locations_secrets_versions, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/locations/<locationsId>/secrets/<secretsId>/versions/<versionsId>:disable",
            "secretmanager_disable_projects_locations_secrets_versions", self.disable_projects_locations_secrets_versions, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/locations/<locationsId>/secrets/<secretsId>/versions/<versionsId>:access",
            "secretmanager_access_projects_locations_secrets_versions", self.access_projects_locations_secrets_versions, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/v1/projects/<projectsId>/locations/<locationsId>/secrets/<secretsId>/versions/<versionsId>:destroy",
            "secretmanager_destroy_projects_locations_secrets_versions", self.destroy_projects_locations_secrets_versions, methods=["POST"],
        )

    def get_projects_secrets(self, projectsId, secretsId):
        item = self.state.get(SVC, projectsId, "projects_secrets", secretsId)
        if not item:
            return error_response(404, f"projects_secrets {secretsId} not found")
        return jsonify(item)

    def create_projects_secrets(self, projectsId):
        body = flask_request.get_json(silent=True) or {}
        name = body.get("name", f"projects_secrets-{gcp_id()[:8]}")
        resource = {
            "kind": "secretmanager#projects_secrets",
            "name": name,
            "id": gcp_id(),
            "creationTimestamp": timestamp_now(),
            **body,
        }
        self.state.put(SVC, projectsId, "projects_secrets", name, resource)
        return jsonify(resource)

    def setIamPolicy_projects_secrets(self, projectsId, secretsId):
        body = flask_request.get_json(silent=True) or {}
        return jsonify({"status": "ok", "method": "setIamPolicy"})

    def patch_projects_secrets(self, projectsId, secretsId):
        existing = self.state.get(SVC, projectsId, "projects_secrets", secretsId)
        if not existing:
            return error_response(404, f"projects_secrets {secretsId} not found")
        body = flask_request.get_json(silent=True) or {}
        existing.update({k: v for k, v in body.items() if v is not None})
        self.state.put(SVC, projectsId, "projects_secrets", secretsId, existing)
        return jsonify(existing)

    def addVersion_projects_secrets(self, projectsId, secretsId):
        body = flask_request.get_json(silent=True) or {}
        return jsonify({"status": "ok", "method": "addVersion"})

    def getIamPolicy_projects_secrets(self, projectsId, secretsId):
        body = flask_request.get_json(silent=True) or {}
        return jsonify({"status": "ok", "method": "getIamPolicy"})

    def list_projects_secrets(self, projectsId):
        items = self.state.list(SVC, projectsId, "projects_secrets")
        return jsonify(paginate(items, flask_request, "secretmanager#projects_secretsList"))

    def delete_projects_secrets(self, projectsId, secretsId):
        if not self.state.delete(SVC, projectsId, "projects_secrets", secretsId):
            return error_response(404, f"projects_secrets {secretsId} not found")
        return "", 204

    def testIamPermissions_projects_secrets(self, projectsId, secretsId):
        body = flask_request.get_json(silent=True) or {}
        return jsonify({"status": "ok", "method": "testIamPermissions"})

    def list_projects_secrets_versions(self, projectsId, secretsId):
        items = self.state.list(SVC, projectsId, "projects_secrets_versions")
        return jsonify(paginate(items, flask_request, "secretmanager#projects_secrets_versionsList"))

    def get_projects_secrets_versions(self, projectsId, secretsId, versionsId):
        item = self.state.get(SVC, projectsId, "projects_secrets_versions", versionsId)
        if not item:
            return error_response(404, f"projects_secrets_versions {versionsId} not found")
        return jsonify(item)

    def destroy_projects_secrets_versions(self, projectsId, secretsId, versionsId):
        body = flask_request.get_json(silent=True) or {}
        return jsonify({"status": "ok", "method": "destroy"})

    def disable_projects_secrets_versions(self, projectsId, secretsId, versionsId):
        body = flask_request.get_json(silent=True) or {}
        return jsonify({"status": "ok", "method": "disable"})

    def access_projects_secrets_versions(self, projectsId, secretsId, versionsId):
        body = flask_request.get_json(silent=True) or {}
        return jsonify({"status": "ok", "method": "access"})

    def enable_projects_secrets_versions(self, projectsId, secretsId, versionsId):
        body = flask_request.get_json(silent=True) or {}
        return jsonify({"status": "ok", "method": "enable"})

    def list_projects_locations(self, projectsId):
        items = self.state.list(SVC, projectsId, "projects_locations")
        return jsonify(paginate(items, flask_request, "secretmanager#projects_locationsList"))

    def get_projects_locations(self, projectsId, locationsId):
        item = self.state.get(SVC, projectsId, "projects_locations", locationsId)
        if not item:
            return error_response(404, f"projects_locations {locationsId} not found")
        return jsonify(item)

    def create_projects_locations_secrets(self, projectsId, locationsId):
        body = flask_request.get_json(silent=True) or {}
        name = body.get("name", f"projects_locations_secrets-{gcp_id()[:8]}")
        resource = {
            "kind": "secretmanager#projects_locations_secrets",
            "name": name,
            "id": gcp_id(),
            "creationTimestamp": timestamp_now(),
            **body,
        }
        self.state.put(SVC, projectsId, "projects_locations_secrets", name, resource)
        return jsonify(resource)

    def get_projects_locations_secrets(self, projectsId, locationsId, secretsId):
        item = self.state.get(SVC, projectsId, "projects_locations_secrets/{locationsId}", secretsId)
        if not item:
            return error_response(404, f"projects_locations_secrets {secretsId} not found")
        return jsonify(item)

    def getIamPolicy_projects_locations_secrets(self, projectsId, locationsId, secretsId):
        body = flask_request.get_json(silent=True) or {}
        return jsonify({"status": "ok", "method": "getIamPolicy"})

    def list_projects_locations_secrets(self, projectsId, locationsId):
        items = self.state.list(SVC, projectsId, "projects_locations_secrets")
        return jsonify(paginate(items, flask_request, "secretmanager#projects_locations_secretsList"))

    def delete_projects_locations_secrets(self, projectsId, locationsId, secretsId):
        if not self.state.delete(SVC, projectsId, "projects_locations_secrets/{locationsId}", secretsId):
            return error_response(404, f"projects_locations_secrets {secretsId} not found")
        return "", 204

    def testIamPermissions_projects_locations_secrets(self, projectsId, locationsId, secretsId):
        body = flask_request.get_json(silent=True) or {}
        return jsonify({"status": "ok", "method": "testIamPermissions"})

    def addVersion_projects_locations_secrets(self, projectsId, locationsId, secretsId):
        body = flask_request.get_json(silent=True) or {}
        return jsonify({"status": "ok", "method": "addVersion"})

    def patch_projects_locations_secrets(self, projectsId, locationsId, secretsId):
        existing = self.state.get(SVC, projectsId, "projects_locations_secrets/{locationsId}", secretsId)
        if not existing:
            return error_response(404, f"projects_locations_secrets {secretsId} not found")
        body = flask_request.get_json(silent=True) or {}
        existing.update({k: v for k, v in body.items() if v is not None})
        self.state.put(SVC, projectsId, "projects_locations_secrets/{locationsId}", secretsId, existing)
        return jsonify(existing)

    def setIamPolicy_projects_locations_secrets(self, projectsId, locationsId, secretsId):
        body = flask_request.get_json(silent=True) or {}
        return jsonify({"status": "ok", "method": "setIamPolicy"})

    def list_projects_locations_secrets_versions(self, projectsId, locationsId, secretsId):
        items = self.state.list(SVC, projectsId, "projects_locations_secrets_versions/{locationsId}")
        return jsonify(paginate(items, flask_request, "secretmanager#projects_locations_secrets_versionsList"))

    def enable_projects_locations_secrets_versions(self, projectsId, locationsId, secretsId, versionsId):
        body = flask_request.get_json(silent=True) or {}
        return jsonify({"status": "ok", "method": "enable"})

    def get_projects_locations_secrets_versions(self, projectsId, locationsId, secretsId, versionsId):
        item = self.state.get(SVC, projectsId, "projects_locations_secrets_versions/{locationsId}", versionsId)
        if not item:
            return error_response(404, f"projects_locations_secrets_versions {versionsId} not found")
        return jsonify(item)

    def disable_projects_locations_secrets_versions(self, projectsId, locationsId, secretsId, versionsId):
        body = flask_request.get_json(silent=True) or {}
        return jsonify({"status": "ok", "method": "disable"})

    def access_projects_locations_secrets_versions(self, projectsId, locationsId, secretsId, versionsId):
        body = flask_request.get_json(silent=True) or {}
        return jsonify({"status": "ok", "method": "access"})

    def destroy_projects_locations_secrets_versions(self, projectsId, locationsId, secretsId, versionsId):
        body = flask_request.get_json(silent=True) or {}
        return jsonify({"status": "ok", "method": "destroy"})

