#!/usr/bin/env python3
# Copyright 2026 RisingWave Labs
#
# 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.

import json
import sys
from http.server import BaseHTTPRequestHandler, HTTPServer


class DynamoDbMockHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.end_headers()

    def do_POST(self):
        content_length = int(self.headers.get("content-length", "0"))
        body = self.rfile.read(content_length)
        request = json.loads(body) if body else {}
        table_name = request.get("TableName", "Movies")

        response = {
            "Table": {
                "TableName": table_name,
                "TableStatus": "ACTIVE",
                "KeySchema": [
                    {"AttributeName": "year", "KeyType": "HASH"},
                    {"AttributeName": "title", "KeyType": "RANGE"},
                ],
                "AttributeDefinitions": [
                    {"AttributeName": "year", "AttributeType": "N"},
                    {"AttributeName": "title", "AttributeType": "S"},
                ],
            }
        }
        payload = json.dumps(response).encode()
        self.send_response(200)
        self.send_header("content-type", "application/x-amz-json-1.0")
        self.send_header("content-length", str(len(payload)))
        self.end_headers()
        self.wfile.write(payload)

    def log_message(self, format, *args):
        return


if __name__ == "__main__":
    port = int(sys.argv[1])
    HTTPServer(("", port), DynamoDbMockHandler).serve_forever()
