import { z } from 'zod';

export const loginSchema = z.object({
  role: z.enum(['admin', 'assistant', 'student']),
  username: z.string().min(3, 'Username must be at least 3 characters'),
  password: z.string().min(6, 'Password must be at least 6 characters'),
});

export const studentSchema = z.object({
  fullName: z.string().min(2, 'Full name is required'),
  dateOfBirth: z.string().min(1, 'Date of birth is required'),
  gender: z.enum(['male', 'female', 'other']),
  phone: z.string().regex(/^\d{10}$/, 'Phone must be 10 digits'),
  email: z.string().email('Invalid email').optional().or(z.literal('')),
  address: z.string().optional(),
  guardianName: z.string().min(2, 'Guardian name is required'),
  guardianPhone: z.string().regex(/^\d{10}$/, 'Phone must be 10 digits'),
  guardianRelationship: z.enum(['parent', 'sibling', 'other']),
  batchId: z.number().min(1, 'Select a batch'),
  skillLevel: z.enum(['beginner', 'intermediate', 'advanced']),
  monthlyFee: z.number().min(1, 'Fee must be greater than 0'),
  joiningDate: z.string().min(1, 'Joining date is required'),
});

export const batchSchema = z.object({
  name: z.string().min(2, 'Batch name is required'),
  timing: z.string().min(1, 'Timing is required'),
  skillLevel: z.enum(['beginner', 'intermediate', 'advanced']),
  maxCapacity: z.number().min(1).max(50),
  monthlyFee: z.number().min(1),
});

export const paymentSchema = z.object({
  studentId: z.number().min(1, 'Select a student'),
  amount: z.number().min(1, 'Amount must be greater than 0'),
  paymentMethod: z.enum(['cash', 'upi', 'card', 'bank_transfer', 'other']),
  paymentDate: z.string().min(1, 'Payment date is required'),
  notes: z.string().optional(),
  generateReceipt: z.boolean().default(true),
});

export const equipmentSchema = z.object({
  name: z.string().min(2, 'Equipment name is required'),
  category: z.string().min(1, 'Select a category'),
  initialStock: z.number().min(0),
  unitCost: z.number().min(0),
  description: z.string().optional(),
});

export const timingSchema = z.object({
  studentId: z.number().min(1, 'Select a student'),
  distance: z.string().min(1, 'Select a distance'),
  time: z.string().min(1, 'Time is required'),
  date: z.string().min(1, 'Date is required'),
  notes: z.string().optional(),
});

export const buyTransactionSchema = z.object({
  equipmentId: z.number().min(1, 'Select equipment'),
  quantity: z.number().min(1, 'Quantity must be at least 1'),
  unitCost: z.number().min(0),
  supplier: z.string().optional(),
  purchaseDate: z.string(),
  notes: z.string().optional(),
});

export const sellTransactionSchema = z.object({
  equipmentId: z.number().min(1, 'Select equipment'),
  buyerType: z.enum(['student', 'external']),
  buyerId: z.number().optional(),
  buyerName: z.string().optional(),
  quantity: z.number().min(1, 'Quantity must be at least 1'),
  unitSellingPrice: z.number().min(0),
  saleDate: z.string(),
  notes: z.string().optional(),
});

export type LoginFormData = z.infer<typeof loginSchema>;
export type StudentFormData = z.infer<typeof studentSchema>;
export type BatchFormData = z.infer<typeof batchSchema>;
export type PaymentFormData = z.infer<typeof paymentSchema>;
export type EquipmentFormData = z.infer<typeof equipmentSchema>;
export type TimingFormData = z.infer<typeof timingSchema>;
