import { jsonFailure, jsonSuccess, readJsonBody } from "@/modules/core/server/api";
import { recordAuditLog } from "@/modules/core/server/audit";
import { requirePermission } from "@/modules/core/server/auth";
import { deleteNotice, isNoticeMutationFailure, markNoticeRead, updateNotice } from "@/modules/notices/server/operations";
import { validateNoticeInput } from "@/modules/notices/types";

type RouteContext = {
  params: Promise<{ id: string }>;
};

export async function PATCH(request: Request, context: RouteContext): Promise<Response> {
  const { id } = await context.params;
  const auth = await requirePermission("notice:manage", { action: "notice.update", targetType: "notice", targetId: id });
  if (!auth.success) {
    return auth.response;
  }

  const organizationId = auth.context.organization?.id;
  if (!organizationId) {
    return jsonFailure("请选择机构后维护公告", 400);
  }

  const input = validateNoticeInput(await readJsonBody(request));
  if (!input.success) {
    return jsonFailure(input.reason);
  }

  const notice = await updateNotice({ ...input.data, id, organizationId, accountId: auth.context.account.id });
  if (isNoticeMutationFailure(notice)) {
    return jsonFailure(notice.reason, notice.status);
  }

  await recordAuditLog({
    actor: auth.context.account,
    organizationId,
    action: "notice.update",
    targetType: "notice",
    targetId: notice.id,
    result: "success",
    reason: `更新公告：${notice.title}`,
  });

  return jsonSuccess("公告已更新", { notice });
}

export async function POST(_request: Request, context: RouteContext): Promise<Response> {
  const { id } = await context.params;
  const auth = await requirePermission("notice:read", { action: "notice.read", targetType: "notice", targetId: id });
  if (!auth.success) {
    return auth.response;
  }

  const organizationId = auth.context.organization?.id;
  if (!organizationId) {
    return jsonFailure("请选择机构后查看公告", 400);
  }

  const notice = await markNoticeRead({ id, organizationId, accountId: auth.context.account.id });
  if (isNoticeMutationFailure(notice)) {
    return jsonFailure(notice.reason, notice.status);
  }

  return jsonSuccess("公告已标记已读", { notice });
}

export async function DELETE(_request: Request, context: RouteContext): Promise<Response> {
  const { id } = await context.params;
  const auth = await requirePermission("notice:manage", { action: "notice.delete", targetType: "notice", targetId: id });
  if (!auth.success) {
    return auth.response;
  }

  const organizationId = auth.context.organization?.id;
  if (!organizationId) {
    return jsonFailure("请选择机构后维护公告", 400);
  }

  const notice = await deleteNotice({ id, organizationId });
  if (isNoticeMutationFailure(notice)) {
    return jsonFailure(notice.reason, notice.status);
  }

  await recordAuditLog({
    actor: auth.context.account,
    organizationId,
    action: "notice.delete",
    targetType: "notice",
    targetId: notice.id,
    result: "success",
    reason: `删除公告：${notice.title}`,
  });

  return jsonSuccess("公告已删除", { notice });
}
