// src/server/features/blacklist/blacklist.schema.ts
// Zod schemas for blacklist feature validation

import { z } from 'zod';

/**
 * Create blacklist entry request schema
 */
export const blacklistCreateSchema = z.object({
  name: z.string().min(1, 'Полное имя обязательно'),
  phone: z.string().min(1, 'Телефон обязателен'),
  bookingEngineCheck: z.enum(['YES', 'NO'], { message: 'Проверка системы бронирования обязательна' }).default('NO'),
  comment: z.string().optional(),
});

export type BlacklistCreateRequest = z.infer<typeof blacklistCreateSchema>;

/**
 * Update blacklist entry request schema
 */
export const blacklistUpdateSchema = z.object({
  name: z.string().min(1).optional(),
  phone: z.string().min(1).optional(),
  bookingEngineCheck: z.enum(['YES', 'NO']).optional(),
  comment: z.string().optional(),
});

export type BlacklistUpdateRequest = z.infer<typeof blacklistUpdateSchema>;

/**
 * Blacklist entry response schema
 */
export const blacklistEntrySchema = z.object({
  id: z.string(),
  name: z.string(),
  phone: z.string(),
  bookingEngineCheck: z.enum(['YES', 'NO']),
  comment: z.string().nullable(),
  isApproved: z.boolean(),
  createdBy: z.string().nullable(),
  createdAt: z.string(),
  updatedAt: z.string(),
  archivedAt: z.string().nullable(),
});

export type BlacklistEntry = z.infer<typeof blacklistEntrySchema>;

/**
 * Blacklist pending entry response schema
 */
export const blacklistPendingEntrySchema = z.object({
  id: z.string(),
  name: z.string(),
  phone: z.string(),
  bookingEngineCheck: z.enum(['YES', 'NO']),
  comment: z.string().nullable(),
  status: z.enum(['pending', 'approved', 'rejected', 'to_delete']),
  targetId: z.string().nullable(), // ID оригинальной записи в таблице blacklist
  createdBy: z.string().nullable(),
  creatorName: z.string().nullable(), // Имя сотрудника, создавшего запись
  reviewedBy: z.string().nullable(),
  reviewedAt: z.string().nullable(),
  rejectionReason: z.string().nullable(),
  createdAt: z.string(),
  updatedAt: z.string(),
});

export type BlacklistPendingEntry = z.infer<typeof blacklistPendingEntrySchema>;

/**
 * Extended Blacklist entry with metadata (for pending entries)
 * Used for socket notifications to include targetId, status, and creatorName
 */
export type BlacklistEntryWithMetadata = BlacklistEntry & {
  targetId?: string | null;
  status?: string;
  creatorName?: string | null;
};

/**
 * Blacklist entries array response schema
 */
export const blacklistEntriesSchema = z.array(blacklistEntrySchema);

/**
 * Action response schema (approve/reject)
 */
export const blacklistActionResponseSchema = z.object({
  success: z.boolean(),
  message: z.string(),
  count: z.number().optional(),
});

export type BlacklistActionResponse = z.infer<typeof blacklistActionResponseSchema>;

/**
 * Error response schema
 */
export const blacklistErrorResponseSchema = z.object({
  success: z.literal(false),
  error: z.object({
    message: z.string(),
    code: z.string(),
  }),
});

export type BlacklistErrorResponse = z.infer<typeof blacklistErrorResponseSchema>;

/**
 * Internal request interfaces for service layer
 */
export interface CreateBlacklistRequest {
  name: string;
  phone: string;
  bookingEngineCheck: 'YES' | 'NO';
  comment?: string;
  createdBy: string;
}

export interface UpdateBlacklistRequest {
  id: string;
  name?: string;
  phone?: string;
  bookingEngineCheck?: 'YES' | 'NO';
  comment?: string;
  updatedBy: string;
}
