#!/usr/bin/env python3
"""Dora local mock — synthetic evaluation data only; not a production contract."""
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from urllib.parse import urlparse, parse_qs
import json

CATALOG = {"synthetic": True, "vehicles": [{"id": "veh_demo_001", "make": "DORA-DEMO", "model": "SEDAN-SINTETICO", "year": 2026}]}
SCENARIOS = {
    "full": {"status": "full", "quotes": [{"id": "quote_demo_001", "provider": "PROVEEDOR_SINTETICO_A", "amount_mxn": 12345.67}], "limitations": []},
    "partial": {"status": "partial", "quotes": [{"id": "quote_demo_002", "provider": "PROVEEDOR_SINTETICO_A", "amount_mxn": 12345.67}], "limitations": ["provider_timeout"]},
    "no_result": {"status": "no_result", "quotes": [], "limitations": ["unsupported_configuration"]},
}

class Handler(BaseHTTPRequestHandler):
    def send_json(self, status, payload):
        body = json.dumps({"synthetic": True, **payload}, ensure_ascii=False, indent=2).encode()
        self.send_response(status)
        self.send_header("Content-Type", "application/json; charset=utf-8")
        self.send_header("Content-Length", str(len(body)))
        self.send_header("Access-Control-Allow-Origin", "http://localhost")
        self.end_headers(); self.wfile.write(body)

    def do_GET(self):
        path = urlparse(self.path).path
        if path == "/health": return self.send_json(200, {"status": "ok", "environment": "local_mock"})
        if path == "/api/Catalog": return self.send_json(200, CATALOG)
        return self.send_json(404, {"error": "mock_route_not_found"})

    def do_POST(self):
        parsed = urlparse(self.path)
        if parsed.path != "/api/demo/quote": return self.send_json(404, {"error": "mock_route_not_found"})
        scenario = parse_qs(parsed.query).get("scenario", ["full"])[0]
        if scenario not in SCENARIOS: return self.send_json(422, {"error": "unknown_scenario", "allowed": list(SCENARIOS)})
        length = int(self.headers.get("Content-Length", "0")); self.rfile.read(length)
        return self.send_json(200, SCENARIOS[scenario])

    def log_message(self, format, *args):
        print(f"[dora-local-mock] {self.address_string()} {format % args}")

if __name__ == "__main__":
    print("Dora local mock: http://127.0.0.1:8787")
    print("Synthetic data only. Not connected to production and not an API contract.")
    ThreadingHTTPServer(("127.0.0.1", 8787), Handler).serve_forever()
