/**
 * Interview Scheduling System Types
 *
 * TypeScript interfaces for interview booking workflow:
 * - Application notes (staff internal notes)
 * - Interview booking tokens (secure link management)
 * - Interview bookings (scheduled interviews)
 */

// =====================================================
// Enums
// =====================================================

/**
 * Interview booking status
 */
export enum InterviewStatus {
  PENDING = 'pending',
  CONFIRMED = 'confirmed',
  RESCHEDULED = 'rescheduled',
  CANCELLED = 'cancelled',
  COMPLETED = 'completed'
}

/**
 * Token status enum (derived from database checks)
 */
export enum TokenStatus {
  VALID = 'valid',
  EXPIRED = 'expired',
  USED = 'used',
  REVOKED = 'revoked',
  NOT_FOUND = 'not_found'
}

/**
 * Meeting platform options
 */
export enum MeetingPlatform {
  ZOOM = 'zoom',
  GOOGLE_MEET = 'google_meet',
  TEAMS = 'teams',
  MANUAL = 'manual'
}

// =====================================================
// Database Models
// =====================================================

/**
 * Application Note
 * Staff internal notes on job applications
 */
export interface ApplicationNote {
  id: string;
  applicationId: string;
  staffId: string;
  staffName: string;
  noteText: string;
  createdAt: Date;
  updatedAt: Date;
}

/**
 * Interview Booking Token
 * Secure token for interview scheduling link
 * Expires after 3 days (72 hours)
 */
export interface InterviewBookingToken {
  id: string;
  token: string;
  applicationId: string;
  jobId: string | null;
  applicantEmail: string;
  expiresAt: Date;
  usedAt: Date | null;
  isRevoked: boolean;
  createdAt: Date;
  updatedAt: Date;
}

/**
 * Interview Booking
 * Scheduled interview with applicant preferences and staff confirmation
 */
export interface InterviewBooking {
  id: string;
  applicationId: string;
  jobId: string | null;
  tokenId: string | null;

  // Applicant info (denormalized)
  applicantName: string;
  applicantEmail: string;
  applicantPhone: string | null;

  // Interview scheduling
  preferredDate: Date;
  preferredTime: string; // HH:MM format
  timezone: string;
  durationMinutes: number;

  // Meeting details (populated by staff)
  confirmedDate: Date | null;
  confirmedTime: string | null; // HH:MM format
  meetingLink: string | null;
  meetingPlatform: MeetingPlatform | null;
  meetingId: string | null;
  meetingPassword: string | null;

  // Status and notes
  status: InterviewStatus;
  applicantNotes: string | null;
  staffNotes: string | null;

  // Metadata
  assignedTo: string | null;
  confirmedAt: Date | null;
  confirmedBy: string | null;
  cancelledAt: Date | null;
  cancelledBy: string | null;
  cancellationReason: string | null;

  // Timestamps
  createdAt: Date;
  updatedAt: Date;
}

// =====================================================
// DTOs (Data Transfer Objects)
// =====================================================

/**
 * Create Application Note DTO
 */
export interface CreateApplicationNoteDTO {
  applicationId: string;
  staffId: string;
  staffName: string;
  noteText: string;
}

/**
 * Create Interview Token DTO
 */
export interface CreateInterviewTokenDTO {
  applicationId: string;
  jobId: string;
  applicantEmail: string;
  expiryHours?: number; // Default: 72 hours (3 days)
}

/**
 * Create Interview Booking DTO
 * Submitted by applicant through booking form
 */
export interface CreateInterviewBookingDTO {
  token: string;
  preferredDate: string; // ISO date string
  preferredTime: string; // HH:MM format
  timezone: string;
  additionalNotes?: string;
}

/**
 * Update Interview Booking DTO
 * Used by staff to confirm/update booking
 */
export interface UpdateInterviewBookingDTO {
  status?: InterviewStatus;
  confirmedDate?: string; // ISO date string
  confirmedTime?: string; // HH:MM format
  meetingLink?: string;
  meetingPlatform?: MeetingPlatform;
  meetingId?: string;
  meetingPassword?: string;
  staffNotes?: string;
  assignedTo?: string;
  cancellationReason?: string;
}

// =====================================================
// Response Types
// =====================================================

/**
 * Token Validation Result
 */
export interface TokenValidationResult {
  isValid: boolean;
  status: TokenStatus;
  tokenId?: string;
  applicationId?: string;
  jobId?: string;
  applicantEmail?: string;
  reason?: string;
}

/**
 * Interview Booking Response
 */
export interface InterviewBookingResponse {
  success: boolean;
  bookingId?: string;
  message: string;
  calendarInvite?: string; // Future: ICS file or Google Calendar link
  error?: string;
}

/**
 * Interview Booking List Item
 * Enriched with job and application details for staff dashboard
 */
export interface InterviewBookingListItem extends InterviewBooking {
  jobTitle?: string;
  jobTitleAr?: string;
  applicantCvPath?: string;
  applicationStatus?: string;
  submittedAt?: Date;
}

/**
 * Interview Statistics
 * Dashboard metrics for staff
 */
export interface InterviewStatistics {
  total: number;
  pending: number;
  confirmed: number;
  rescheduled: number;
  cancelled: number;
  completed: number;
  todayCount: number;
  thisWeekCount: number;
  upcomingCount: number;
}

// =====================================================
// Filter & Query Types
// =====================================================

/**
 * Interview Booking Filters
 * For staff dashboard list view
 */
export interface InterviewBookingFilters {
  status?: InterviewStatus | InterviewStatus[];
  jobId?: string;
  assignedTo?: string;
  startDate?: Date;
  endDate?: Date;
  searchQuery?: string; // Search by applicant name or email
  page?: number;
  limit?: number;
}

/**
 * Application Note Filters
 */
export interface ApplicationNoteFilters {
  applicationId?: string;
  staffId?: string;
  startDate?: Date;
  endDate?: Date;
  limit?: number;
}

// =====================================================
// Email Template Data
// =====================================================

/**
 * Interview Invitation Email Data
 */
export interface InterviewInvitationEmailData {
  applicantEmail: string;
  applicantName: string;
  jobTitle: string;
  jobTitleAr: string;
  bookingToken: string;
  bookingUrl: string;
  expiryDate: string;
  expiryTime: string;
  language: 'ar' | 'en';
}

/**
 * Interview Confirmation Email Data
 */
export interface InterviewConfirmationEmailData {
  applicantEmail: string;
  applicantName: string;
  jobTitle: string;
  jobTitleAr: string;
  interviewDate: string;
  interviewTime: string;
  timezone: string;
  meetingLink?: string;
  meetingPlatform?: MeetingPlatform;
  meetingId?: string;
  meetingPassword?: string;
  preparationInstructions?: string;
  language: 'ar' | 'en';
}

/**
 * Interview Reminder Email Data
 */
export interface InterviewReminderEmailData {
  applicantEmail: string;
  applicantName: string;
  jobTitle: string;
  jobTitleAr: string;
  interviewDate: string;
  interviewTime: string;
  timezone: string;
  meetingLink?: string;
  hoursUntil: number;
  language: 'ar' | 'en';
}

/**
 * Interview Cancellation Email Data
 */
export interface InterviewCancellationEmailData {
  applicantEmail: string;
  applicantName: string;
  jobTitle: string;
  jobTitleAr: string;
  cancellationReason?: string;
  canReschedule: boolean;
  rescheduleToken?: string;
  rescheduleUrl?: string;
  language: 'ar' | 'en';
}

// =====================================================
// Validation Types
// =====================================================

/**
 * Time Slot Configuration
 */
export interface TimeSlotConfig {
  startTime: string; // HH:MM
  endTime: string; // HH:MM
  intervalMinutes: number; // e.g., 30
  excludedDays: number[]; // 0 = Sunday, 6 = Saturday
  maxDaysAhead: number; // e.g., 14 days
  timezone: string;
}

/**
 * Business Hours Configuration
 */
export interface BusinessHoursConfig {
  timezone: string;
  workingDays: number[]; // 0-6 (Sunday-Saturday)
  startTime: string; // HH:MM
  endTime: string; // HH:MM
  lunchBreak?: {
    start: string; // HH:MM
    end: string; // HH:MM
  };
}
