import { z } from "zod";
import { makeCrudRoute } from "@open-mercato/shared/lib/crud/factory";
import { resolveTranslations } from "@open-mercato/shared/lib/i18n/server";
import { resolveCrudRecordId, parseScopedCommandInput } from "@open-mercato/shared/lib/api/scoped";
import { StaffTeamMemberJobHistory } from "../data/entities.js";
import {
  optimisticUpdatedAtSchema,
  staffTeamMemberJobHistoryCreateSchema,
  staffTeamMemberJobHistoryUpdateSchema
} from "../data/validators.js";
import { createStaffCrudOpenApi, createPagedListResponseSchema, defaultOkResponseSchema } from "./openapi.js";
import { E } from "../../../generated/entities.ids.generated.js";
const rawBodySchema = z.object({}).passthrough();
const optimisticDeleteSchema = z.object({
  id: z.string().uuid(),
  updatedAt: optimisticUpdatedAtSchema.optional()
});
const listSchema = z.object({
  page: z.coerce.number().min(1).default(1),
  pageSize: z.coerce.number().min(1).max(100).default(50),
  entityId: z.string().uuid().optional(),
  sortField: z.string().optional(),
  sortDir: z.enum(["asc", "desc"]).optional()
}).passthrough();
const routeMetadata = {
  GET: { requireAuth: true, requireFeatures: ["staff.view"] },
  POST: { requireAuth: true, requireFeatures: ["staff.manage_team"] },
  PUT: { requireAuth: true, requireFeatures: ["staff.manage_team"] },
  DELETE: { requireAuth: true, requireFeatures: ["staff.manage_team"] }
};
const metadata = routeMetadata;
const crud = makeCrudRoute({
  metadata: routeMetadata,
  orm: {
    entity: StaffTeamMemberJobHistory,
    idField: "id",
    orgField: "organizationId",
    tenantField: "tenantId"
  },
  indexer: { entityType: E.staff.staff_team_member_job_history },
  list: {
    schema: listSchema,
    entityId: E.staff.staff_team_member_job_history,
    fields: [
      "id",
      "member_id",
      "name",
      "company_name",
      "description",
      "start_date",
      "end_date",
      "organization_id",
      "tenant_id",
      "created_at",
      "updated_at"
    ],
    sortFieldMap: {
      startDate: "start_date",
      endDate: "end_date",
      createdAt: "created_at",
      updatedAt: "updated_at"
    },
    buildFilters: async (query) => {
      const filters = {};
      if (query.entityId) filters.member_id = { $eq: query.entityId };
      return filters;
    }
  },
  actions: {
    create: {
      commandId: "staff.team-member-job-histories.create",
      schema: rawBodySchema,
      mapInput: async ({ raw, ctx }) => {
        const { translate } = await resolveTranslations();
        return parseScopedCommandInput(staffTeamMemberJobHistoryCreateSchema, raw ?? {}, ctx, translate);
      },
      response: ({ result }) => ({ id: result?.jobHistoryId ?? null }),
      status: 201
    },
    update: {
      commandId: "staff.team-member-job-histories.update",
      schema: rawBodySchema,
      mapInput: async ({ raw, ctx }) => {
        const { translate } = await resolveTranslations();
        return parseScopedCommandInput(staffTeamMemberJobHistoryUpdateSchema, raw ?? {}, ctx, translate);
      },
      response: () => ({ ok: true })
    },
    delete: {
      commandId: "staff.team-member-job-histories.delete",
      schema: rawBodySchema,
      mapInput: async ({ parsed, ctx }) => {
        const { translate } = await resolveTranslations();
        const id = resolveCrudRecordId(parsed, ctx, translate);
        const parsedBody = parsed?.body;
        return {
          id,
          updatedAt: typeof parsedBody?.updatedAt === "string" ? parsedBody.updatedAt : void 0
        };
      },
      response: () => ({ ok: true })
    }
  }
});
const GET = crud.GET;
const POST = crud.POST;
const PUT = crud.PUT;
const DELETE = crud.DELETE;
const jobHistoryListItemSchema = z.object({
  id: z.string().uuid(),
  member_id: z.string().uuid().nullable().optional(),
  name: z.string().nullable().optional(),
  company_name: z.string().nullable().optional(),
  description: z.string().nullable().optional(),
  start_date: z.string().nullable().optional(),
  end_date: z.string().nullable().optional(),
  organization_id: z.string().uuid().nullable().optional(),
  tenant_id: z.string().uuid().nullable().optional(),
  created_at: z.string().nullable().optional(),
  updated_at: z.string().nullable().optional()
}).passthrough();
const openApi = createStaffCrudOpenApi({
  resourceName: "TeamMemberJobHistory",
  querySchema: listSchema,
  listResponseSchema: createPagedListResponseSchema(jobHistoryListItemSchema),
  create: {
    schema: staffTeamMemberJobHistoryCreateSchema,
    responseSchema: z.object({ id: z.string().uuid().nullable() }),
    description: "Adds a team member job history entry."
  },
  update: {
    schema: staffTeamMemberJobHistoryUpdateSchema,
    responseSchema: defaultOkResponseSchema,
    description: "Updates a team member job history entry."
  },
  del: {
    schema: optimisticDeleteSchema,
    responseSchema: defaultOkResponseSchema,
    description: "Deletes a team member job history entry."
  }
});
export {
  DELETE,
  GET,
  POST,
  PUT,
  metadata,
  openApi
};
//# sourceMappingURL=job-histories.js.map
