const Joi = require('joi');

// ==========================================
// SUPER ADMIN VALIDATIONS
// ==========================================

// Ticket status update validation
exports.updateTicketStatusSchema = Joi.object({
    status: Joi.string().valid('open', 'pending', 'closed').required()
});

// Ticket escalation validation
exports.escalateTicketSchema = Joi.object({
    reason: Joi.string().min(10).max(500).required(),
    priority: Joi.string().valid('low', 'normal', 'high', 'urgent').default('high')
});

// Ticket assignment validation
exports.assignTicketSchema = Joi.object({
    assignTo: Joi.string().pattern(/^[0-9a-fA-F]{24}$/).required(),
    assignType: Joi.string().valid('vendor', 'admin').required()
});

// Add ticket message validation
exports.addTicketMessageSchema = Joi.object({
    text: Joi.string().min(1).max(1000).required(),
    isInternal: Joi.boolean().default(false)
});

// Propose compensation validation
exports.proposeCompensationSchema = Joi.object({
    ticketId: Joi.string().pattern(/^[0-9a-fA-F]{24}$/).required(),
    amount: Joi.number().positive().max(10000).required(),
    reason: Joi.string().min(10).max(500).required()
});

// Process compensation validation
exports.processCompensationSchema = Joi.object({
    paymentMethod: Joi.string().valid('wallet', 'bank_transfer', 'refund').required(),
    transactionId: Joi.string().min(5).max(50).required(),
    notes: Joi.string().max(500).optional()
});

// Reject compensation validation
exports.rejectCompensationSchema = Joi.object({
    reason: Joi.string().min(10).max(500).required()
});

// Create callback request validation (super admin)
exports.createCallbackRequestSchema = Joi.object({
    ticketId: Joi.string().pattern(/^[0-9a-fA-F]{24}$/).optional(),
    customerId: Joi.string().pattern(/^[0-9a-fA-F]{24}$/).required(),
    name: Joi.string().min(2).max(100).optional(),
    contact: Joi.string().pattern(/^\+?[1-9]\d{1,14}$/).required(),
    reason: Joi.string().min(10).max(500).required(),
    reasonCategory: Joi.string().valid(
        'Booking Inquiry',
        'Payment Issue',
        'Refund Request',
        'Route Information',
        'Driver Complaint',
        'Lost Item',
        'General Inquiry',
        'Other'
    ).optional(),
    preferredSlot: Joi.object({
        date: Joi.date().min('now').optional(),
        timeSlot: Joi.string().valid(
            '10:00 AM - 12:00 PM',
            '12:00 PM - 2:00 PM',
            '2:00 PM - 4:00 PM',
            '4:00 PM - 6:00 PM'
        ).optional()
    }).optional()
});

// Schedule callback validation
exports.scheduleCallbackSchema = Joi.object({
    scheduledAt: Joi.date().min('now').required(),
    assignedTo: Joi.string().pattern(/^[0-9a-fA-F]{24}$/).required(),
    notes: Joi.string().max(500).optional()
});

// Complete callback validation
exports.completeCallbackSchema = Joi.object({
    notes: Joi.string().max(500).optional()
});

// Query validation schemas
exports.paginationSchema = Joi.object({
    page: Joi.number().integer().min(1).default(1),
    limit: Joi.number().integer().min(1).max(100).default(10)
});

exports.ticketFilterSchema = Joi.object({
    status: Joi.string().valid('open', 'pending', 'closed', 'All Status').optional(),
    category: Joi.string().valid('Booking Issue', 'Request Off Time', 'Payment Inquiry', 'Driver Issue', 'Refund Request', 'Safety Concern', 'Lost Item', 'Fare Dispute', 'App Issue', 'Other', 'All Status').optional(),
    zone: Joi.string().optional(),
    vendor: Joi.string().optional(),
    search: Joi.string().max(100).optional(),
    dateRange: Joi.string().pattern(/^\d{4}-\d{2}-\d{2},\d{4}-\d{2}-\d{2}$/).optional(),
    creatorType: Joi.string().valid('user', 'driver', 'vendor', 'super-admin', 'zone-admin', 'all').optional(),
    escalated: Joi.boolean().optional(),
    priority: Joi.string().valid('low', 'normal', 'high', 'urgent', 'all').optional()
});

exports.compensationFilterSchema = Joi.object({
    status: Joi.string().valid('proposed', 'approved', 'processed', 'rejected', 'All Status').optional()
});

exports.callbackFilterSchema = Joi.object({
    status: Joi.string().valid('pending', 'scheduled', 'completed', 'cancelled', 'All Status').optional()
});

exports.analyticsSchema = Joi.object({
    period: Joi.string().valid('daily', 'weekly', 'monthly').default('weekly'),
    startDate: Joi.date().optional(),
    endDate: Joi.date().min(Joi.ref('startDate')).optional()
});

// ==========================================
// USER SUPPORT VALIDATIONS
// ==========================================

// User create ticket validation
exports.userCreateTicketSchema = Joi.object({
    category: Joi.string().valid(
        'Booking Issue',
        'Payment Inquiry',
        'Driver Issue',
        'Refund Request',
        'Safety Concern',
        'Lost Item',
        'Fare Dispute',
        'App Issue',
        'Other'
    ).required(),
    subject: Joi.string().min(5).max(200).required(),
    text: Joi.string().min(10).max(2000).required(),
    orderId: Joi.string().required()
});

// User add message validation
exports.userAddMessageSchema = Joi.object({
    text: Joi.string().min(1).max(1000).required()
});

// User callback request validation
exports.userCallbackRequestSchema = Joi.object({
    name: Joi.string().min(2).max(100).optional(),
    contact: Joi.string().pattern(/^\+?[1-9]\d{1,14}$/).required(),
    reasonCategory: Joi.string().valid(
        'Booking Inquiry',
        'Payment Issue',
        'Refund Request',
        'Route Information',
        'Driver Complaint',
        'Lost Item',
        'General Inquiry',
        'Other'
    ).optional(),
    orderId: Joi.string().required(),
    preferredSlot: Joi.object({
        date: Joi.date().optional(),
        timeSlot: Joi.string().valid(
            '10:00 AM - 12:00 PM',
            '12:00 PM - 2:00 PM',
            '2:00 PM - 4:00 PM',
            '4:00 PM - 6:00 PM'
        ).optional()
    }).optional()
});