"""
Cloud DNS 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 = "dns"
API_PREFIX = ""


class DnsService(GCPService):
    service_name = "dns"
    api_prefix = API_PREFIX

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

        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/managedZones/<managedZone>/rrsets",
            "dns_list_resourceRecordSets", self.list_resourceRecordSets, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/managedZones/<managedZone>/rrsets",
            "dns_create_resourceRecordSets", self.create_resourceRecordSets, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/managedZones/<managedZone>/rrsets/<name>/<type>",
            "dns_get_resourceRecordSets", self.get_resourceRecordSets, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/managedZones/<managedZone>/rrsets/<name>/<type>",
            "dns_delete_resourceRecordSets", self.delete_resourceRecordSets, methods=["DELETE"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/managedZones/<managedZone>/rrsets/<name>/<type>",
            "dns_patch_resourceRecordSets", self.patch_resourceRecordSets, methods=["PATCH"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/managedZones/<managedZone>/changes",
            "dns_create_changes", self.create_changes, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/managedZones/<managedZone>/changes/<changeId>",
            "dns_get_changes", self.get_changes, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/managedZones/<managedZone>/changes",
            "dns_list_changes", self.list_changes, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/managedZones/<managedZone>/dnsKeys/<dnsKeyId>",
            "dns_get_dnsKeys", self.get_dnsKeys, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/managedZones/<managedZone>/dnsKeys",
            "dns_list_dnsKeys", self.list_dnsKeys, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>",
            "dns_get_projects", self.get_projects, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/managedZones/<managedZone>/operations/<operation>",
            "dns_get_managedZoneOperations", self.get_managedZoneOperations, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/managedZones/<managedZone>/operations",
            "dns_list_managedZoneOperations", self.list_managedZoneOperations, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/managedZones",
            "dns_create_managedZones", self.create_managedZones, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/managedZones/<managedZone>",
            "dns_get_managedZones", self.get_managedZones, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/managedZones",
            "dns_list_managedZones", self.list_managedZones, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/managedZones/<managedZone>",
            "dns_delete_managedZones", self.delete_managedZones, methods=["DELETE"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/managedZones/<managedZone>",
            "dns_patch_managedZones", self.patch_managedZones, methods=["PATCH"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/managedZones/<managedZone>",
            "dns_update_managedZones", self.update_managedZones, methods=["PUT"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<projectsId>/managedZones/<managedZonesId>:setIamPolicy",
            "dns_setIamPolicy_managedZones", self.setIamPolicy_managedZones, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<projectsId>/managedZones/<managedZonesId>:getIamPolicy",
            "dns_getIamPolicy_managedZones", self.getIamPolicy_managedZones, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<projectsId>/managedZones/<managedZonesId>:testIamPermissions",
            "dns_testIamPermissions_managedZones", self.testIamPermissions_managedZones, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/policies",
            "dns_create_policies", self.create_policies, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/policies/<policy>",
            "dns_get_policies", self.get_policies, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/policies",
            "dns_list_policies", self.list_policies, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/policies/<policy>",
            "dns_delete_policies", self.delete_policies, methods=["DELETE"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/policies/<policy>",
            "dns_patch_policies", self.patch_policies, methods=["PATCH"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/policies/<policy>",
            "dns_update_policies", self.update_policies, methods=["PUT"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/responsePolicies",
            "dns_create_responsePolicies", self.create_responsePolicies, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/responsePolicies/<responsePolicy>",
            "dns_get_responsePolicies", self.get_responsePolicies, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/responsePolicies",
            "dns_list_responsePolicies", self.list_responsePolicies, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/responsePolicies/<responsePolicy>",
            "dns_delete_responsePolicies", self.delete_responsePolicies, methods=["DELETE"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/responsePolicies/<responsePolicy>",
            "dns_patch_responsePolicies", self.patch_responsePolicies, methods=["PATCH"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/responsePolicies/<responsePolicy>",
            "dns_update_responsePolicies", self.update_responsePolicies, methods=["PUT"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/responsePolicies/<responsePolicy>/rules",
            "dns_create_responsePolicyRules", self.create_responsePolicyRules, methods=["POST"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/responsePolicies/<responsePolicy>/rules/<responsePolicyRule>",
            "dns_get_responsePolicyRules", self.get_responsePolicyRules, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/responsePolicies/<responsePolicy>/rules/<responsePolicyRule>",
            "dns_delete_responsePolicyRules", self.delete_responsePolicyRules, methods=["DELETE"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/responsePolicies/<responsePolicy>/rules",
            "dns_list_responsePolicyRules", self.list_responsePolicyRules, methods=["GET"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/responsePolicies/<responsePolicy>/rules/<responsePolicyRule>",
            "dns_patch_responsePolicyRules", self.patch_responsePolicyRules, methods=["PATCH"],
        )
        bp.add_url_rule(
            f"{p}/dns/v1/projects/<project>/responsePolicies/<responsePolicy>/rules/<responsePolicyRule>",
            "dns_update_responsePolicyRules", self.update_responsePolicyRules, methods=["PUT"],
        )

    def list_resourceRecordSets(self, project, managedZone):
        items = self.state.list(SVC, project, "resourceRecordSets")
        return jsonify(paginate(items, flask_request, "dns#resourceRecordSetsList"))

    def create_resourceRecordSets(self, project, managedZone):
        body = flask_request.get_json(silent=True) or {}
        name = body.get("name", f"resourceRecordSets-{gcp_id()[:8]}")
        resource = {
            "kind": "dns#resourceRecordSets",
            "name": name,
            "id": gcp_id(),
            "creationTimestamp": timestamp_now(),
            **body,
        }
        self.state.put(SVC, project, "resourceRecordSets", name, resource)
        op = self.state.create_operation(SVC, project)
        return jsonify(op)

    def get_resourceRecordSets(self, project, managedZone, name, type):
        item = self.state.get(SVC, project, "resourceRecordSets", type)
        if not item:
            return error_response(404, f"resourceRecordSets {type} not found")
        return jsonify(item)

    def delete_resourceRecordSets(self, project, managedZone, name, type):
        if not self.state.delete(SVC, project, "resourceRecordSets", type):
            return error_response(404, f"resourceRecordSets {type} not found")
        op = self.state.create_operation(SVC, project, operation_type="delete")
        return jsonify(op)

    def patch_resourceRecordSets(self, project, managedZone, name, type):
        existing = self.state.get(SVC, project, "resourceRecordSets", type)
        if not existing:
            return error_response(404, f"resourceRecordSets {type} 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, project, "resourceRecordSets", type, existing)
        op = self.state.create_operation(SVC, project, operation_type="patch")
        return jsonify(op)

    def create_changes(self, project, managedZone):
        body = flask_request.get_json(silent=True) or {}
        name = body.get("name", f"changes-{gcp_id()[:8]}")
        resource = {
            "kind": "dns#changes",
            "name": name,
            "id": gcp_id(),
            "creationTimestamp": timestamp_now(),
            **body,
        }
        self.state.put(SVC, project, "changes", name, resource)
        op = self.state.create_operation(SVC, project)
        return jsonify(op)

    def get_changes(self, project, managedZone, changeId):
        item = self.state.get(SVC, project, "changes", changeId)
        if not item:
            return error_response(404, f"changes {changeId} not found")
        return jsonify(item)

    def list_changes(self, project, managedZone):
        items = self.state.list(SVC, project, "changes")
        return jsonify(paginate(items, flask_request, "dns#changesList"))

    def get_dnsKeys(self, project, managedZone, dnsKeyId):
        item = self.state.get(SVC, project, "dnsKeys", dnsKeyId)
        if not item:
            return error_response(404, f"dnsKeys {dnsKeyId} not found")
        return jsonify(item)

    def list_dnsKeys(self, project, managedZone):
        items = self.state.list(SVC, project, "dnsKeys")
        return jsonify(paginate(items, flask_request, "dns#dnsKeysList"))

    def get_projects(self, project):
        item = self.state.get(SVC, project, "projects", project)
        if not item:
            return error_response(404, f"projects {project} not found")
        return jsonify(item)

    def get_managedZoneOperations(self, project, managedZone, operation):
        item = self.state.get(SVC, project, "managedZoneOperations", operation)
        if not item:
            return error_response(404, f"managedZoneOperations {operation} not found")
        return jsonify(item)

    def list_managedZoneOperations(self, project, managedZone):
        items = self.state.list(SVC, project, "managedZoneOperations")
        return jsonify(paginate(items, flask_request, "dns#managedZoneOperationsList"))

    def create_managedZones(self, project):
        body = flask_request.get_json(silent=True) or {}
        name = body.get("name", f"managedZones-{gcp_id()[:8]}")
        resource = {
            "kind": "dns#managedZones",
            "name": name,
            "id": gcp_id(),
            "creationTimestamp": timestamp_now(),
            **body,
        }
        self.state.put(SVC, project, "managedZones", name, resource)
        op = self.state.create_operation(SVC, project)
        return jsonify(op)

    def get_managedZones(self, project, managedZone):
        item = self.state.get(SVC, project, "managedZones", managedZone)
        if not item:
            return error_response(404, f"managedZones {managedZone} not found")
        return jsonify(item)

    def list_managedZones(self, project):
        items = self.state.list(SVC, project, "managedZones")
        return jsonify(paginate(items, flask_request, "dns#managedZonesList"))

    def delete_managedZones(self, project, managedZone):
        if not self.state.delete(SVC, project, "managedZones", managedZone):
            return error_response(404, f"managedZones {managedZone} not found")
        op = self.state.create_operation(SVC, project, operation_type="delete")
        return jsonify(op)

    def patch_managedZones(self, project, managedZone):
        existing = self.state.get(SVC, project, "managedZones", managedZone)
        if not existing:
            return error_response(404, f"managedZones {managedZone} 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, project, "managedZones", managedZone, existing)
        op = self.state.create_operation(SVC, project, operation_type="patch")
        return jsonify(op)

    def update_managedZones(self, project, managedZone):
        existing = self.state.get(SVC, project, "managedZones", managedZone)
        if not existing:
            return error_response(404, f"managedZones {managedZone} 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, project, "managedZones", managedZone, existing)
        op = self.state.create_operation(SVC, project, operation_type="patch")
        return jsonify(op)

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

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

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

    def create_policies(self, project):
        body = flask_request.get_json(silent=True) or {}
        name = body.get("name", f"policies-{gcp_id()[:8]}")
        resource = {
            "kind": "dns#policies",
            "name": name,
            "id": gcp_id(),
            "creationTimestamp": timestamp_now(),
            **body,
        }
        self.state.put(SVC, project, "policies", name, resource)
        op = self.state.create_operation(SVC, project)
        return jsonify(op)

    def get_policies(self, project, policy):
        item = self.state.get(SVC, project, "policies", policy)
        if not item:
            return error_response(404, f"policies {policy} not found")
        return jsonify(item)

    def list_policies(self, project):
        items = self.state.list(SVC, project, "policies")
        return jsonify(paginate(items, flask_request, "dns#policiesList"))

    def delete_policies(self, project, policy):
        if not self.state.delete(SVC, project, "policies", policy):
            return error_response(404, f"policies {policy} not found")
        op = self.state.create_operation(SVC, project, operation_type="delete")
        return jsonify(op)

    def patch_policies(self, project, policy):
        existing = self.state.get(SVC, project, "policies", policy)
        if not existing:
            return error_response(404, f"policies {policy} 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, project, "policies", policy, existing)
        op = self.state.create_operation(SVC, project, operation_type="patch")
        return jsonify(op)

    def update_policies(self, project, policy):
        existing = self.state.get(SVC, project, "policies", policy)
        if not existing:
            return error_response(404, f"policies {policy} 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, project, "policies", policy, existing)
        op = self.state.create_operation(SVC, project, operation_type="patch")
        return jsonify(op)

    def create_responsePolicies(self, project):
        body = flask_request.get_json(silent=True) or {}
        name = body.get("name", f"responsePolicies-{gcp_id()[:8]}")
        resource = {
            "kind": "dns#responsePolicies",
            "name": name,
            "id": gcp_id(),
            "creationTimestamp": timestamp_now(),
            **body,
        }
        self.state.put(SVC, project, "responsePolicies", name, resource)
        op = self.state.create_operation(SVC, project)
        return jsonify(op)

    def get_responsePolicies(self, project, responsePolicy):
        item = self.state.get(SVC, project, "responsePolicies", responsePolicy)
        if not item:
            return error_response(404, f"responsePolicies {responsePolicy} not found")
        return jsonify(item)

    def list_responsePolicies(self, project):
        items = self.state.list(SVC, project, "responsePolicies")
        return jsonify(paginate(items, flask_request, "dns#responsePoliciesList"))

    def delete_responsePolicies(self, project, responsePolicy):
        if not self.state.delete(SVC, project, "responsePolicies", responsePolicy):
            return error_response(404, f"responsePolicies {responsePolicy} not found")
        op = self.state.create_operation(SVC, project, operation_type="delete")
        return jsonify(op)

    def patch_responsePolicies(self, project, responsePolicy):
        existing = self.state.get(SVC, project, "responsePolicies", responsePolicy)
        if not existing:
            return error_response(404, f"responsePolicies {responsePolicy} 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, project, "responsePolicies", responsePolicy, existing)
        op = self.state.create_operation(SVC, project, operation_type="patch")
        return jsonify(op)

    def update_responsePolicies(self, project, responsePolicy):
        existing = self.state.get(SVC, project, "responsePolicies", responsePolicy)
        if not existing:
            return error_response(404, f"responsePolicies {responsePolicy} 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, project, "responsePolicies", responsePolicy, existing)
        op = self.state.create_operation(SVC, project, operation_type="patch")
        return jsonify(op)

    def create_responsePolicyRules(self, project, responsePolicy):
        body = flask_request.get_json(silent=True) or {}
        name = body.get("name", f"responsePolicyRules-{gcp_id()[:8]}")
        resource = {
            "kind": "dns#responsePolicyRules",
            "name": name,
            "id": gcp_id(),
            "creationTimestamp": timestamp_now(),
            **body,
        }
        self.state.put(SVC, project, "responsePolicyRules", name, resource)
        op = self.state.create_operation(SVC, project)
        return jsonify(op)

    def get_responsePolicyRules(self, project, responsePolicy, responsePolicyRule):
        item = self.state.get(SVC, project, "responsePolicyRules", responsePolicyRule)
        if not item:
            return error_response(404, f"responsePolicyRules {responsePolicyRule} not found")
        return jsonify(item)

    def delete_responsePolicyRules(self, project, responsePolicy, responsePolicyRule):
        if not self.state.delete(SVC, project, "responsePolicyRules", responsePolicyRule):
            return error_response(404, f"responsePolicyRules {responsePolicyRule} not found")
        op = self.state.create_operation(SVC, project, operation_type="delete")
        return jsonify(op)

    def list_responsePolicyRules(self, project, responsePolicy):
        items = self.state.list(SVC, project, "responsePolicyRules")
        return jsonify(paginate(items, flask_request, "dns#responsePolicyRulesList"))

    def patch_responsePolicyRules(self, project, responsePolicy, responsePolicyRule):
        existing = self.state.get(SVC, project, "responsePolicyRules", responsePolicyRule)
        if not existing:
            return error_response(404, f"responsePolicyRules {responsePolicyRule} 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, project, "responsePolicyRules", responsePolicyRule, existing)
        op = self.state.create_operation(SVC, project, operation_type="patch")
        return jsonify(op)

    def update_responsePolicyRules(self, project, responsePolicy, responsePolicyRule):
        existing = self.state.get(SVC, project, "responsePolicyRules", responsePolicyRule)
        if not existing:
            return error_response(404, f"responsePolicyRules {responsePolicyRule} 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, project, "responsePolicyRules", responsePolicyRule, existing)
        op = self.state.create_operation(SVC, project, operation_type="patch")
        return jsonify(op)

