import { z } from 'zod';

// List people query params
export const listPeopleSchema = z.object({
  page: z.coerce.number().int().positive().default(1),
  limit: z.coerce.number().int().positive().max(100).default(20),
  search: z.string().optional(),
  source: z.enum(['USER', 'ADDRESS_OCCUPANT', 'CAMPAIGN_SENDER', 'SHIFT_SIGNUP', 'SMS_CONTACT', 'DONATION', 'MANUAL']).optional(),
  supportLevel: z.enum(['LEVEL_1', 'LEVEL_2', 'LEVEL_3', 'LEVEL_4']).optional(),
  tag: z.string().optional(),
  managedOnly: z.coerce.boolean().optional(),
});

// Manage (promote) a virtual person to Contact
export const managePersonSchema = z.object({
  sourceType: z.enum(['user', 'addr', 'contact', 'donor', 'cemail', 'signup', 'sms']),
  sourceId: z.string(),
  displayName: z.string().optional(),
  firstName: z.string().optional(),
  lastName: z.string().optional(),
  email: z.string().email().optional().or(z.literal('')),
  phone: z.string().optional(),
});

// Update Contact
export const updateContactSchema = z.object({
  displayName: z.string().min(1).optional(),
  firstName: z.string().optional(),
  lastName: z.string().optional(),
  email: z.string().email().optional().nullable().or(z.literal('')),
  phone: z.string().optional().nullable(),
  tags: z.array(z.string()).optional(),
  notes: z.string().optional().nullable(),
  supportLevel: z.enum(['LEVEL_1', 'LEVEL_2', 'LEVEL_3', 'LEVEL_4']).optional().nullable(),
  signRequested: z.boolean().optional(),
  emailOptOut: z.boolean().optional(),
  smsOptOut: z.boolean().optional(),
  doNotContact: z.boolean().optional(),
});

// Create contact directly (manual add)
export const createContactSchema = z.object({
  displayName: z.string().min(1).max(200),
  firstName: z.string().max(100).optional(),
  lastName: z.string().max(100).optional(),
  email: z.string().email().optional().or(z.literal('')),
  phone: z.string().max(30).optional(),
  tags: z.array(z.string()).optional(),
  notes: z.string().optional(),
  supportLevel: z.enum(['LEVEL_1', 'LEVEL_2', 'LEVEL_3', 'LEVEL_4']).optional(),
});

// Create connection
export const createConnectionSchema = z.object({
  toPersonType: z.enum(['user', 'addr', 'contact']),
  toPersonId: z.string(),
  type: z.enum(['HOUSEHOLD', 'FAMILY', 'COLLEAGUE', 'REFERRED_BY', 'CUSTOM']),
  label: z.string().optional(),
  notes: z.string().optional(),
  isBidirectional: z.boolean().default(true),
});

// Merge
export const mergeContactSchema = z.object({
  sourceType: z.enum(['user', 'addr', 'contact', 'donor', 'cemail', 'signup', 'sms']),
  sourceId: z.string(),
  keepFields: z.record(z.enum(['source', 'target'])).optional(),
});

// Graph query
export const graphQuerySchema = z.object({
  center: z.string().optional(),
  depth: z.coerce.number().int().min(1).max(3).default(2),
  source: z.enum(['USER', 'ADDRESS_OCCUPANT', 'CAMPAIGN_SENDER', 'SHIFT_SIGNUP', 'SMS_CONTACT', 'DONATION', 'MANUAL']).optional(),
  tag: z.string().optional(),
  minScore: z.coerce.number().int().min(0).max(100).optional(),
});

// Activity list
export const activityListSchema = z.object({
  page: z.coerce.number().int().positive().default(1),
  limit: z.coerce.number().int().positive().max(50).default(20),
  type: z.enum(['EMAIL_SENT', 'RESPONSE_SUBMITTED', 'SHIFT_SIGNUP', 'CANVASS_VISIT', 'DONATION', 'PURCHASE', 'SMS_SENT', 'SMS_RECEIVED', 'VIDEO_VIEW', 'NOTE_ADDED', 'CONTACT_MERGED']).optional(),
});

// Create user account from a Contact
export const createUserFromContactSchema = z.object({
  password: z.string().min(12).regex(/[A-Z]/, 'Must contain uppercase').regex(/[a-z]/, 'Must contain lowercase').regex(/[0-9]/, 'Must contain digit'),
  role: z.enum(['USER', 'TEMP', 'MAP_ADMIN', 'INFLUENCE_ADMIN', 'SUPER_ADMIN']).default('USER'),
  sendWelcomeEmail: z.boolean().default(false),
});

export type CreateUserFromContactInput = z.infer<typeof createUserFromContactSchema>;

// Add address to a contact (with optional map integration)
export const addContactAddressSchema = z.object({
  address: z.string().min(1).max(500),
  unitNumber: z.string().max(20).optional(),
  isPrimary: z.boolean().default(false),
  addToMap: z.boolean().default(true),
});

export const removeContactAddressSchema = z.object({
  contactAddressId: z.string(),
});

// Add email to a contact
export const addContactEmailSchema = z.object({
  email: z.string().email(),
  label: z.string().max(50).optional(),
  isPrimary: z.boolean().optional(),
});

// Add phone to a contact
export const addContactPhoneSchema = z.object({
  phone: z.string().min(1).max(30),
  label: z.string().max(50).optional(),
  isPrimary: z.boolean().optional(),
});

// Generate profile link with optional expiration + password
export const generateProfileLinkSchema = z.object({
  expiresIn: z.enum(['24h', '7d', '30d', '90d', '1y', 'never']).default('never'),
  password: z.string().min(4).max(128).optional().nullable(),
});

// Update profile link settings (without regenerating token)
export const updateProfileLinkSchema = z.object({
  expiresIn: z.enum(['24h', '7d', '30d', '90d', '1y', 'never']).optional(),
  password: z.string().min(4).max(128).optional().nullable(),
  removePassword: z.boolean().optional(),
});

export type GenerateProfileLinkInput = z.infer<typeof generateProfileLinkSchema>;
export type UpdateProfileLinkInput = z.infer<typeof updateProfileLinkSchema>;

export type AddContactAddressInput = z.infer<typeof addContactAddressSchema>;
export type AddContactEmailInput = z.infer<typeof addContactEmailSchema>;
export type AddContactPhoneInput = z.infer<typeof addContactPhoneSchema>;
export type ListPeopleInput = z.infer<typeof listPeopleSchema>;
export type ManagePersonInput = z.infer<typeof managePersonSchema>;
export type CreateContactInput = z.infer<typeof createContactSchema>;
export type UpdateContactInput = z.infer<typeof updateContactSchema>;
export type CreateConnectionInput = z.infer<typeof createConnectionSchema>;
export type MergeContactInput = z.infer<typeof mergeContactSchema>;
export type GraphQueryInput = z.infer<typeof graphQuerySchema>;
export type ActivityListInput = z.infer<typeof activityListSchema>;
