// src/server/features/reference_books/inventory.schema.ts
// Zod schemas for inventory validation

import { z } from 'zod';

/**
 * Create inventory request
 */
export const createInventorySchema = z.object({
  name: z.string().min(1, 'Название обязательно'),
  category: z.string().min(1, 'Категория обязательна'),
  quantity: z.coerce.string().min(0, 'Количество не может быть отрицательным'),
  unit: z.string().nullable(),
  location: z.string().min(1, 'Локация обязательна'),
  comment: z.string().nullable().optional(),
  weightPerUnit: z.coerce.string().nullable().optional(),
  manufacturer: z.string().nullable().optional(),
  model: z.string().nullable().optional(),
  serialNumber: z.string().nullable().optional(),
  orderingLink: z.string().nullable().optional(),
  articleNumber: z.string().nullable().optional(),
  createdBy: z.string().uuid().optional(),
});

/**
 * Update inventory request
 */
export const updateInventorySchema = z.object({
  id: z.string().uuid().optional(),
  name: z.string().min(1).optional(),
  category: z.string().min(1).optional(),
  quantity: z.coerce.string().min(0).optional(),
  unit: z.string().nullable().optional(),
  location: z.string().min(1).optional(),
  comment: z.string().nullable().optional(),
  weightPerUnit: z.coerce.string().nullable().optional(),
  manufacturer: z.string().nullable().optional(),
  model: z.string().nullable().optional(),
  serialNumber: z.string().nullable().optional(),
  orderingLink: z.string().nullable().optional(),
  articleNumber: z.string().nullable().optional(),
  updatedBy: z.string().uuid().optional(),
});

/**
 * Inventory entry response
 */
export const inventoryEntryResponseSchema = z.object({
  id: z.string().uuid(),
  name: z.string(),
  category: z.string(),
  quantity: z.string(),
  unit: z.string().nullable(),
  location: z.string(),
  comment: z.string().nullable(),
  weightPerUnit: z.string().nullable(),
  manufacturer: z.string().nullable(),
  model: z.string().nullable(),
  serialNumber: z.string().nullable(),
  orderingLink: z.string().nullable(),
  articleNumber: z.string().nullable(),
  isApproved: z.boolean(),
  status: z.enum(['pending', 'approved', 'rejected', 'to_delete']).optional(),
  targetId: z.string().uuid().nullable(),
  createdBy: z.string().uuid().nullable(),
  creatorName: z.string().nullable(),
  reviewedBy: z.string().uuid().nullable(),
  reviewedAt: z.string().nullable(),
  rejectionReason: z.string().nullable(),
  createdAt: z.string(),
  updatedAt: z.string(),
  archivedAt: z.string().nullable(),
});

/**
 * Inventory pending entry response
 */
export const inventoryPendingEntryResponseSchema = z.object({
  id: z.string().uuid(),
  name: z.string(),
  category: z.string(),
  quantity: z.string(),
  unit: z.string().nullable(),
  location: z.string(),
  comment: z.string().nullable(),
  weightPerUnit: z.string().nullable(),
  manufacturer: z.string().nullable(),
  model: z.string().nullable(),
  serialNumber: z.string().nullable(),
  orderingLink: z.string().nullable(),
  articleNumber: z.string().nullable(),
  status: z.enum(['pending', 'approved', 'rejected', 'to_delete']),
  targetId: z.string().uuid().nullable(),
  createdBy: z.string().uuid().nullable(),
  creatorName: z.string().nullable(),
  reviewedBy: z.string().uuid().nullable(),
  reviewedAt: z.string().nullable(),
  rejectionReason: z.string().nullable(),
  createdAt: z.string(),
  updatedAt: z.string(),
});

/**
 * Inventory entry schema (for routes)
 */
export const inventoryEntrySchema = z.object({
  id: z.string().uuid(),
  name: z.string(),
  category: z.string(),
  quantity: z.string(),
  unit: z.string().nullable(),
  location: z.string(),
  comment: z.string().nullable(),
  weightPerUnit: z.string().nullable(),
  manufacturer: z.string().nullable(),
  model: z.string().nullable(),
  serialNumber: z.string().nullable(),
  orderingLink: z.string().nullable(),
  articleNumber: z.string().nullable(),
  isApproved: z.boolean(),
  status: z.enum(['pending', 'approved', 'rejected', 'to_delete']).optional(),
  targetId: z.string().uuid().nullable(),
  createdBy: z.string().uuid().nullable(),
  creatorName: z.string().nullable(),
  reviewedBy: z.string().uuid().nullable(),
  reviewedAt: z.string().nullable(),
  rejectionReason: z.string().nullable(),
  createdAt: z.string(),
  updatedAt: z.string(),
  archivedAt: z.string().nullable(),
});

/**
 * Inventory pending entry schema (for routes)
 */
export const inventoryPendingEntrySchema = z.object({
  id: z.string().uuid(),
  name: z.string(),
  category: z.string(),
  quantity: z.string(),
  unit: z.string().nullable(),
  location: z.string(),
  comment: z.string().nullable(),
  weightPerUnit: z.string().nullable(),
  manufacturer: z.string().nullable(),
  model: z.string().nullable(),
  serialNumber: z.string().nullable(),
  orderingLink: z.string().nullable(),
  articleNumber: z.string().nullable(),
  status: z.enum(['pending', 'approved', 'rejected', 'to_delete']),
  targetId: z.string().uuid().nullable(),
  createdBy: z.string().uuid().nullable(),
  creatorName: z.string().nullable(),
  reviewedBy: z.string().uuid().nullable(),
  reviewedAt: z.string().nullable(),
  rejectionReason: z.string().nullable(),
  createdAt: z.string(),
  updatedAt: z.string(),
});

/**
 * Inventory entry with metadata (for pending entries)
 */
export interface InventoryEntryWithMetadata {
  id: string;
  name: string;
  category: string;
  quantity: string;
  unit: string | null;
  location: string;
  comment: string | null;
  weightPerUnit: string | null;
  manufacturer: string | null;
  model: string | null;
  serialNumber: string | null;
  orderingLink: string | null;
  articleNumber: string | null;
  isApproved: boolean;
  status?: 'pending' | 'approved' | 'rejected' | 'to_delete';
  targetId: string | null;
  createdBy: string | null;
  creatorName: string | null;
  reviewedBy: string | null;
  reviewedAt: string | null;
  rejectionReason: string | null;
  createdAt: string;
  updatedAt: string;
  archivedAt: string | null;
}

/**
 * Inventory pending entry
 */
export interface InventoryPendingEntry {
  id: string;
  name: string;
  category: string;
  quantity: string;
  unit: string | null;
  location: string;
  comment: string | null;
  weightPerUnit: string | null;
  manufacturer: string | null;
  model: string | null;
  serialNumber: string | null;
  orderingLink: string | null;
  articleNumber: string | null;
  status: 'pending' | 'approved' | 'rejected' | 'to_delete';
  targetId: string | null;
  createdBy: string | null;
  creatorName: string | null;
  reviewedBy: string | null;
  reviewedAt: string | null;
  rejectionReason: string | null;
  createdAt: string;
  updatedAt: string;
  archivedAt: string | null;
  isApproved: boolean;
}

// TypeScript types
export type CreateInventoryRequest = z.infer<typeof createInventorySchema>;
export type UpdateInventoryRequest = z.infer<typeof updateInventorySchema>;
export type InventoryEntryResponse = z.infer<typeof inventoryEntryResponseSchema>;
export type InventoryPendingEntryResponse = z.infer<typeof inventoryPendingEntryResponseSchema>;

// Re-export schemas for routes
export const inventoryCreateSchema = createInventorySchema;
export const inventoryUpdateSchema = updateInventorySchema;
export const inventoryParamsSchema = z.object({
  id: z.string().uuid('Неверный формат ID'),
});
export const inventoryQuerySchema = z.object({
  category: z.string().optional(),
  status: z.enum(['all', 'pending', 'approved', 'rejected', 'to_delete']).optional(),
  archived: z.coerce.boolean().optional(),
});
