import { NextResponse } from "next/server";
import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { getAuthFromRequest } from "@open-mercato/shared/lib/auth/server";
import { resolveTranslations } from "@open-mercato/shared/lib/i18n/server";
import { notificationDeliveryConfigSchema } from "../../data/validators.js";
import {
  errorResponseSchema,
  notificationSettingsResponseSchema,
  notificationSettingsUpdateResponseSchema
} from "../openapi.js";
import {
  DEFAULT_NOTIFICATION_DELIVERY_CONFIG,
  resolveNotificationDeliveryConfig,
  saveNotificationDeliveryConfig
} from "../../lib/deliveryConfig.js";
import {
  NOTIFICATION_SETTINGS_RESOURCE_KIND,
  runGuardedNotificationWrite
} from "../../lib/routeHelpers.js";
const metadata = {
  GET: { requireAuth: true, requireFeatures: ["notifications.manage"] },
  POST: { requireAuth: true, requireFeatures: ["notifications.manage"] }
};
const unauthorized = async () => {
  const { t } = await resolveTranslations();
  return NextResponse.json({ error: t("api.errors.unauthorized", "Unauthorized") }, { status: 401 });
};
async function GET(req) {
  const auth = await getAuthFromRequest(req);
  if (!auth?.sub) return await unauthorized();
  const container = await createRequestContainer();
  try {
    const settings = await resolveNotificationDeliveryConfig(container, {
      defaultValue: DEFAULT_NOTIFICATION_DELIVERY_CONFIG
    });
    return NextResponse.json({ settings });
  } finally {
    const disposable = container;
    if (typeof disposable.dispose === "function") {
      await disposable.dispose();
    }
  }
}
async function POST(req) {
  const { t } = await resolveTranslations();
  const auth = await getAuthFromRequest(req);
  if (!auth?.sub) return await unauthorized();
  let body;
  try {
    body = await req.json();
  } catch {
    return NextResponse.json(
      { error: t("api.errors.invalidPayload", "Invalid request body") },
      { status: 400 }
    );
  }
  const parsed = notificationDeliveryConfigSchema.safeParse(body);
  if (!parsed.success) {
    return NextResponse.json(
      { error: t("notifications.delivery.settings.invalid", "Invalid delivery settings") },
      { status: 400 }
    );
  }
  const container = await createRequestContainer();
  try {
    const guarded = await runGuardedNotificationWrite(
      container,
      {
        tenantId: auth.tenantId ?? "",
        organizationId: auth.orgId ?? null,
        userId: auth.sub ?? null
      },
      req,
      {
        resourceKind: NOTIFICATION_SETTINGS_RESOURCE_KIND,
        operation: "update",
        payload: parsed.data
      },
      async () => {
        await saveNotificationDeliveryConfig(container, parsed.data);
        return resolveNotificationDeliveryConfig(container, {
          defaultValue: DEFAULT_NOTIFICATION_DELIVERY_CONFIG
        });
      }
    );
    if (!guarded.ok) return guarded.response;
    return NextResponse.json({ ok: true, settings: guarded.result });
  } catch (error) {
    return NextResponse.json(
      { error: error instanceof Error ? error.message : t("api.errors.internal", "Internal error") },
      { status: 500 }
    );
  } finally {
    const disposable = container;
    if (typeof disposable.dispose === "function") {
      await disposable.dispose();
    }
  }
}
const openApi = {
  GET: {
    summary: "Get notification delivery settings",
    tags: ["Notifications"],
    responses: {
      200: {
        description: "Current delivery settings",
        content: {
          "application/json": {
            schema: notificationSettingsResponseSchema
          }
        }
      },
      401: {
        description: "Unauthorized",
        content: {
          "application/json": {
            schema: errorResponseSchema
          }
        }
      }
    }
  },
  POST: {
    summary: "Update notification delivery settings",
    tags: ["Notifications"],
    requestBody: {
      required: true,
      content: {
        "application/json": {
          schema: notificationDeliveryConfigSchema
        }
      }
    },
    responses: {
      200: {
        description: "Delivery settings updated",
        content: {
          "application/json": {
            schema: notificationSettingsUpdateResponseSchema
          }
        }
      },
      400: {
        description: "Invalid request body",
        content: {
          "application/json": {
            schema: errorResponseSchema
          }
        }
      },
      401: {
        description: "Unauthorized",
        content: {
          "application/json": {
            schema: errorResponseSchema
          }
        }
      },
      500: {
        description: "Internal error",
        content: {
          "application/json": {
            schema: errorResponseSchema
          }
        }
      }
    }
  }
};
export {
  GET,
  POST,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
