// Customer Success Tracking Types
// Captures milestones, surveys, and success story candidates from day 1

export type MilestoneType =
  | 'signup'           // Account created
  | 'onboarding_complete' // Profile fully built
  | 'first_job_posted' // Contractor posted first job
  | 'first_application' // Worker applied to first job
  | 'first_hire'       // First successful hire (payment completed)
  | 'repeat_hire'      // Second+ hire
  | 'first_rating'     // Left first rating
  | 'five_star_given'  // Gave a 5-star rating
  | 'five_star_received' // Received a 5-star rating
  | 'referral_sent'    // Referred someone
  | 'referral_converted' // Referral signed up
  | 'profile_complete' // 100% profile completion
  | 'first_message'    // Sent first message
  | 'fast_hire';       // Hired within 24h of posting

export type SurveyType = 'onboarding' | 'post_hire' | 'exit';

export type SuccessStoryStatus = 'candidate' | 'contacted' | 'agreed' | 'published' | 'declined';

export interface Milestone {
  id: string;
  user_id: string;
  user_type: 'worker' | 'contractor';
  milestone_type: MilestoneType;
  metadata: Record<string, unknown> | null; // e.g. { job_id, match_id, rating }
  created_at: string;
}

export interface SuccessSurvey {
  id: string;
  user_id: string;
  user_type: 'worker' | 'contractor';
  survey_type: SurveyType;
  responses: Record<string, string>; // question_key → answer
  nps_score: number | null; // 0-10 Net Promoter Score
  created_at: string;
}

export interface SuccessCandidate {
  id: string;
  user_id: string;
  user_type: 'worker' | 'contractor';
  user_name: string | null;
  user_email: string | null;
  score: number; // Computed success score (milestones + NPS + activity)
  milestone_count: number;
  latest_milestone: MilestoneType | null;
  nps_score: number | null;
  status: SuccessStoryStatus;
  admin_notes: string | null;
  created_at: string;
  updated_at: string;
}

// Onboarding survey questions
export const ONBOARDING_QUESTIONS = {
  how_found: {
    label: 'How did you find RateRight?',
    type: 'select' as const,
    options: [
      'Google search',
      'Social media',
      'Friend/colleague',
      'QR code/poster',
      'Job board',
      'Other',
    ],
  },
  why_joining: {
    label: 'What made you sign up?',
    type: 'select' as const,
    options: [
      'Need workers fast',
      'Cheaper than agencies',
      'AI matching sounded good',
      'Looking for construction work',
      'Someone recommended it',
      'Just checking it out',
    ],
  },
  biggest_pain: {
    label: 'What\'s your biggest hiring/job-finding pain?',
    type: 'text' as const,
  },
  expectations: {
    label: 'What would make RateRight a success for you?',
    type: 'text' as const,
  },
} as const;

// Post-hire survey questions
export const POST_HIRE_QUESTIONS = {
  experience_rating: {
    label: 'How was the hiring experience?',
    type: 'select' as const,
    options: ['Excellent', 'Good', 'OK', 'Poor', 'Terrible'],
  },
  speed_satisfaction: {
    label: 'Was the matching speed acceptable?',
    type: 'select' as const,
    options: ['Way faster than expected', 'About right', 'A bit slow', 'Too slow'],
  },
  would_recommend: {
    label: 'Would you recommend RateRight to a mate?',
    type: 'select' as const,
    options: ['Definitely', 'Probably', 'Maybe', 'Probably not', 'No way'],
  },
  nps: {
    label: 'How likely are you to recommend RateRight? (0-10)',
    type: 'nps' as const,
  },
  testimonial: {
    label: 'Want to share a quick quote we can use? (optional)',
    type: 'text' as const,
  },
} as const;

// Milestone display config
export const MILESTONE_LABELS: Record<MilestoneType, { emoji: string; label: string; points: number }> = {
  signup: { emoji: '🎉', label: 'Signed up', points: 1 },
  onboarding_complete: { emoji: '✅', label: 'Onboarding complete', points: 2 },
  first_job_posted: { emoji: '📋', label: 'First job posted', points: 3 },
  first_application: { emoji: '🙋', label: 'First application', points: 3 },
  first_hire: { emoji: '🤝', label: 'First hire', points: 10 },
  repeat_hire: { emoji: '🔄', label: 'Repeat hire', points: 15 },
  first_rating: { emoji: '⭐', label: 'First rating', points: 2 },
  five_star_given: { emoji: '🌟', label: 'Gave 5 stars', points: 3 },
  five_star_received: { emoji: '🏆', label: 'Received 5 stars', points: 5 },
  referral_sent: { emoji: '📤', label: 'Sent referral', points: 5 },
  referral_converted: { emoji: '🎯', label: 'Referral signed up', points: 10 },
  profile_complete: { emoji: '💯', label: 'Profile 100%', points: 2 },
  first_message: { emoji: '💬', label: 'First message', points: 1 },
  fast_hire: { emoji: '⚡', label: 'Hired within 24h', points: 8 },
};
