/**
 * @ai-summary Type definitions for notifications feature
 * @ai-context Email and toast notifications for maintenance and documents
 */

import { z } from 'zod';

// Template key union type
export type TemplateKey =
  | 'maintenance_due_soon'
  | 'maintenance_overdue'
  | 'document_expiring'
  | 'document_expired';

// Email template API response type (camelCase for frontend)
export interface EmailTemplate {
  id: string;
  templateKey: TemplateKey;
  name: string;
  description?: string;
  subject: string;
  body: string;
  variables: string[];
  isActive: boolean;
  createdAt: string;
  updatedAt: string;
}

// Notification log database type
export interface NotificationLog {
  id: string;
  user_id: string;
  notification_type: 'email' | 'toast';
  template_key: TemplateKey;
  recipient_email?: string;
  subject?: string;
  reference_type?: string;
  reference_id?: string;
  status: 'pending' | 'sent' | 'failed';
  error_message?: string;
  sent_at: string;
}

// Summary for login toast (camelCase for frontend compatibility)
export interface NotificationSummary {
  maintenanceDueSoon: number;
  maintenanceOverdue: number;
  documentsExpiringSoon: number;
  documentsExpired: number;
}

// Due maintenance item (camelCase for frontend)
export interface DueMaintenanceItem {
  id: string;
  vehicleId: string;
  vehicleName: string;
  category: string;
  subtypes: string[];
  nextDueDate?: string;
  nextDueMileage?: number;
  isDueSoon: boolean;
  isOverdue: boolean;
  emailNotifications: boolean;
  reminderDays1?: number | null;
  reminderDays2?: number | null;
  reminderDays3?: number | null;
}

// Expiring document (camelCase for frontend)
export interface ExpiringDocument {
  id: string;
  vehicleId: string;
  vehicleName: string;
  documentType: string;
  title: string;
  expirationDate?: string;
  isExpiringSoon: boolean;
  isExpired: boolean;
  emailNotifications: boolean;
}

// Zod schemas for validation
export const TemplateKeySchema = z.enum([
  'maintenance_due_soon',
  'maintenance_overdue',
  'document_expiring',
  'document_expired'
]);

export const UpdateEmailTemplateSchema = z.object({
  subject: z.string().min(1).max(255).optional(),
  body: z.string().min(1).optional(),
  isActive: z.boolean().optional(),
});
export type UpdateEmailTemplateRequest = z.infer<typeof UpdateEmailTemplateSchema>;

export const PreviewTemplateSchema = z.object({
  subject: z.string().min(1).max(255),
  body: z.string().min(1),
  variables: z.record(z.string()),
});
export type PreviewTemplateRequest = z.infer<typeof PreviewTemplateSchema>;

// User notification types (camelCase for frontend)
export interface UserNotification {
  id: string;
  userId: string;
  notificationType: string;
  title: string;
  message: string;
  referenceType?: string | null;
  referenceId?: string | null;
  vehicleId?: string | null;
  isRead: boolean;
  createdAt: string;
  readAt?: string | null;
}

export interface UnreadNotificationCount {
  total: number;
  maintenance: number;
  documents: number;
}

export interface SentNotificationRecord {
  id: string;
  scheduleId: string;
  notificationDate: string;
  reminderLevel: 1 | 2 | 3;
  deliveryMethod: 'email' | 'in_app' | 'both';
  createdAt: string;
}
