import { z } from 'zod';

export const ScimConnectorStatusSchema = z.enum(['active', 'paused', 'archived']);
export const ScimUserSyncStatusSchema = z.enum(['provisioned', 'updated', 'disabled', 'failed']);
export const ScimGroupSyncStatusSchema = z.enum(['provisioned', 'updated', 'disabled', 'failed']);
export const ScimEventStatusSchema = z.enum(['success', 'failed']);

export const ScimConnectorSchema = z.object({
  id: z.string().min(1),
  orgId: z.string().min(1),
  productId: z.string().min(1),
  idpId: z.string().optional(),
  name: z.string().min(1),
  baseUrl: z.string().url().optional(),
  authMode: z.enum(['bearer', 'oauth']).default('bearer'),
  status: ScimConnectorStatusSchema,
  metadata: z.record(z.unknown()).optional(),
  createdBy: z.string().min(1),
  createdAt: z.string(),
  updatedAt: z.string(),
});

export type ScimConnectorDoc = z.infer<typeof ScimConnectorSchema> & {
  _ts?: number;
  _etag?: string;
};

export const ScimUserSyncSchema = z.object({
  id: z.string().min(1),
  orgId: z.string().min(1),
  connectorId: z.string().min(1),
  productId: z.string().min(1),
  externalUserId: z.string().min(1),
  userId: z.string().optional(),
  email: z.string().email(),
  status: ScimUserSyncStatusSchema,
  lastSyncedAt: z.string(),
  errorMessage: z.string().optional(),
  metadata: z.record(z.unknown()).optional(),
});

export type ScimUserSyncDoc = z.infer<typeof ScimUserSyncSchema> & {
  _ts?: number;
  _etag?: string;
};

export const ScimGroupSyncSchema = z.object({
  id: z.string().min(1),
  orgId: z.string().min(1),
  connectorId: z.string().min(1),
  productId: z.string().min(1),
  externalGroupId: z.string().min(1),
  workspaceId: z.string().optional(),
  displayName: z.string().min(1),
  memberCount: z.number().int().min(0).default(0),
  status: ScimGroupSyncStatusSchema,
  lastSyncedAt: z.string(),
  errorMessage: z.string().optional(),
  metadata: z.record(z.unknown()).optional(),
});

export type ScimGroupSyncDoc = z.infer<typeof ScimGroupSyncSchema> & {
  _ts?: number;
  _etag?: string;
};

export const ScimProvisioningEventSchema = z.object({
  id: z.string().min(1),
  orgId: z.string().min(1),
  connectorId: z.string().min(1),
  productId: z.string().min(1),
  entityType: z.enum(['user', 'group']),
  entityId: z.string().min(1),
  action: z.enum(['provision', 'update', 'deprovision', 'sync']),
  status: ScimEventStatusSchema,
  message: z.string().optional(),
  createdAt: z.string(),
});

export type ScimProvisioningEventDoc = z.infer<typeof ScimProvisioningEventSchema> & {
  _ts?: number;
  _etag?: string;
};

export const CreateScimConnectorSchema = z.object({
  orgId: z.string().min(1),
  idpId: z.string().optional(),
  name: z.string().min(1),
  baseUrl: z.string().url().optional(),
  authMode: z.enum(['bearer', 'oauth']).default('bearer'),
  metadata: z.record(z.unknown()).optional(),
});

export const UpdateScimConnectorSchema = z.object({
  idpId: z.string().optional(),
  name: z.string().min(1).optional(),
  baseUrl: z.string().url().optional(),
  authMode: z.enum(['bearer', 'oauth']).optional(),
  status: ScimConnectorStatusSchema.optional(),
  metadata: z.record(z.unknown()).optional(),
});

export const RecordScimUserSyncSchema = z.object({
  externalUserId: z.string().min(1),
  userId: z.string().optional(),
  email: z.string().email(),
  status: ScimUserSyncStatusSchema,
  errorMessage: z.string().optional(),
  metadata: z.record(z.unknown()).optional(),
});

export const RecordScimGroupSyncSchema = z.object({
  externalGroupId: z.string().min(1),
  workspaceId: z.string().optional(),
  displayName: z.string().min(1),
  memberCount: z.number().int().min(0).default(0),
  status: ScimGroupSyncStatusSchema,
  errorMessage: z.string().optional(),
  metadata: z.record(z.unknown()).optional(),
});

export const RecordScimEventSchema = z.object({
  entityType: z.enum(['user', 'group']),
  entityId: z.string().min(1),
  action: z.enum(['provision', 'update', 'deprovision', 'sync']),
  status: ScimEventStatusSchema,
  message: z.string().optional(),
});
