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

import { z } from 'zod';

/**
 * Create contractor request
 */
export const createContractorSchema = z.object({
  name: z.string().min(1, 'Название обязательно'),
  category: z.string().min(1, 'Категория обязательна'),
  contractNumber: z.string().nullable(),
  phone: z.string().min(1, 'Телефон обязателен'),
  email: z.string().nullable(),
  webLink: z.string().nullable(),
  managerName: z.string().nullable(),
  managerPhone: z.string().nullable(),
  managerEmail: z.string().nullable(),
  comment: z.string().nullable(),
  createdBy: z.string().uuid().optional(),
});

/**
 * Update contractor request
 */
export const updateContractorSchema = z.object({
  id: z.string().uuid().optional(),
  name: z.string().min(1).optional(),
  category: z.string().min(1).optional(),
  contractNumber: z.string().nullable().optional(),
  phone: z.string().min(1).optional(),
  email: z.string().nullable().optional(),
  webLink: z.string().nullable().optional(),
  managerName: z.string().nullable().optional(),
  managerPhone: z.string().nullable().optional(),
  managerEmail: z.string().nullable().optional(),
  comment: z.string().nullable().optional(),
  updatedBy: z.string().uuid().optional(),
});

/**
 * Contractor entry response
 */
export const contractorEntryResponseSchema = z.object({
  id: z.string().uuid(),
  name: z.string(),
  category: z.string(),
  contractNumber: z.string().nullable(),
  phone: z.string(),
  email: z.string().nullable(),
  webLink: z.string().nullable(),
  managerName: z.string().nullable(),
  managerPhone: z.string().nullable(),
  managerEmail: z.string().nullable(),
  comment: 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(),
});

/**
 * Contractor pending entry response
 */
export const contractorPendingEntryResponseSchema = z.object({
  id: z.string().uuid(),
  name: z.string(),
  category: z.string(),
  contractNumber: z.string().nullable(),
  phone: z.string(),
  email: z.string().nullable(),
  webLink: z.string().nullable(),
  managerName: z.string().nullable(),
  managerPhone: z.string().nullable(),
  managerEmail: z.string().nullable(),
  comment: 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(),
});

/**
 * Contractor entry schema (for routes)
 */
export const contractorEntrySchema = z.object({
  id: z.string().uuid(),
  name: z.string(),
  category: z.string(),
  contractNumber: z.string().nullable(),
  phone: z.string(),
  email: z.string().nullable(),
  webLink: z.string().nullable(),
  managerName: z.string().nullable(),
  managerPhone: z.string().nullable(),
  managerEmail: z.string().nullable(),
  comment: z.string().nullable(),
  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(),
});

/**
 * Contractor pending entry schema (for routes)
 */
export const contractorPendingEntrySchema = z.object({
  id: z.string().uuid(),
  name: z.string(),
  category: z.string(),
  contractNumber: z.string().nullable(),
  phone: z.string(),
  email: z.string().nullable(),
  webLink: z.string().nullable(),
  managerName: z.string().nullable(),
  managerPhone: z.string().nullable(),
  managerEmail: z.string().nullable(),
  comment: 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(),
});

/**
 * Contractor entry with metadata (for pending entries)
 */
export interface ContractorEntryWithMetadata {
  id: string;
  name: string;
  category: string;
  contractNumber: string | null;
  phone: string;
  email: string | null;
  webLink: string | null;
  managerName: string | null;
  managerPhone: string | null;
  managerEmail: string | null;
  comment: 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;
}

/**
 * Contractor pending entry
 */
export interface ContractorPendingEntry {
  id: string;
  name: string;
  category: string;
  contractNumber: string | null;
  phone: string;
  email: string | null;
  webLink: string | null;
  managerName: string | null;
  managerPhone: string | null;
  managerEmail: string | null;
  comment: 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;
}

// TypeScript types
export type CreateContractorRequest = z.infer<typeof createContractorSchema>;
export type UpdateContractorRequest = z.infer<typeof updateContractorSchema>;
export type ContractorEntryResponse = z.infer<typeof contractorEntryResponseSchema>;
export type ContractorPendingEntryResponse = z.infer<typeof contractorPendingEntryResponseSchema>;

// Extended types for service layer (with isAdmin flag)
export interface CreateContractorRequestWithAdmin extends CreateContractorRequest {
  isAdmin: boolean;
}

export interface UpdateContractorRequestWithAdmin extends Omit<UpdateContractorRequest, 'id'> {
  isAdmin: boolean;
}

// Re-export schemas for routes
export const contractorCreateSchema = createContractorSchema;
export const contractorUpdateSchema = updateContractorSchema;
