import {
  SafetyQuizCompletionRowDto,
  SafetyQuizResultCreateDto,
  SafetyQuizResultDto,
} from '@/shared/types/safetyQuiz';
import { SafetyQuizComplianceRow, SafetyQuizSubmission } from '@/business/safety-quiz/types';

function toRoleLabel(role: string): string {
  if (role === 'owner') {
    return 'Owner';
  }

  if (role === 'principal') {
    return 'Principal';
  }

  if (role === 'registrar') {
    return 'Registrar';
  }

  if (role === 'support_staff') {
    return 'Support Staff';
  }

  if (role === 'director') {
    return 'Director';
  }

  if (role === 'superintendent') {
    return 'Superintendent';
  }

  if (role === 'office_manager') {
    return 'Office';
  }

  return 'Teacher';
}

export function toSafetyQuizComplianceRow(dto: SafetyQuizResultDto): SafetyQuizComplianceRow {
  return {
    userId: dto.userId,
    name: dto.user_name,
    avatar: null,
    tenantName: null,
    tenantLogo: null,
    role: toRoleLabel(dto.user_role),
    status: 'complete',
    score: `${dto.score}/${dto.total_questions}`,
    date: new Date(dto.completed_at).toLocaleDateString('en-US', {
      month: 'short',
      day: 'numeric',
    }),
  };
}

export function toSafetyQuizCompletionRow(dto: SafetyQuizCompletionRowDto): SafetyQuizComplianceRow {
  return {
    userId: dto.userId,
    name: dto.name,
    avatar: dto.avatar,
    tenantName: dto.tenantName,
    tenantLogo: dto.tenantLogo,
    role: dto.role ? toRoleLabel(dto.role) : 'Staff',
    status: dto.status,
    score: dto.result ? `${dto.result.score}/${dto.result.total_questions}` : 'Pending',
    date: dto.result
      ? new Date(dto.result.completed_at).toLocaleDateString('en-US', {
          month: 'short',
          day: 'numeric',
        })
      : 'Not completed',
  };
}

export function toSafetyQuizResultCreateDto(submission: SafetyQuizSubmission): SafetyQuizResultCreateDto {
  return {
    quiz_id: submission.quizId,
    quiz_title: submission.quizTitle,
    week_of: submission.weekOf,
    score: submission.score,
    total_questions: submission.totalQuestions,
    answers: submission.answers,
  };
}
