import { PUBLIC, arr, created, dateTime, err, int, jsonBody, obj, objectId, ok, pathParam, ref, str } from './helpers';

export const PUBLIC_TAGS = {
  interview: 'Public - Interview Quiz',
  legal: 'Public - Legal Pages',
};

export const publicTags = [
  {
    name: PUBLIC_TAGS.interview,
    description: 'Candidate-facing endpoints reached through the emailed link (no login). The link is created by `POST /api/quizzes/interview/quiz/assign`.',
  },
  { name: PUBLIC_TAGS.legal, description: 'Legal pages readable without login' },
];

const T = PUBLIC_TAGS;
const tokenParam = pathParam('token', 'Interview link token (UUID)', { type: 'string', example: '9b2f6f0e-2a4d-4c1e-9d8a-3c1f5e7b9a10' });

export const publicPaths = {
  '/api/quizzes/interview/quiz/details/{token}': {
    get: {
      tags: [T.interview],
      summary: 'Interview quiz details before starting',
      security: PUBLIC,
      parameters: [tokenParam],
      responses: {
        ...ok(
          'Quiz details fetched',
          obj({
            title: str('Backend Screening'),
            numQuestions: int(15),
            durationSeconds: int(75, { description: '5 seconds per question' }),
            start_datetime: dateTime(),
            end_datetime: dateTime(),
            candidateEmail: str('candidate@example.com'),
          }),
        ),
        ...err(404, 'Invalid or expired quiz link'),
      },
    },
  },
  '/api/quizzes/interview/quiz/attempt/{token}': {
    get: {
      tags: [T.interview],
      summary: 'Start (or resume) the interview quiz',
      description: 'The first call starts the timer (5 seconds per question).',
      security: PUBLIC,
      parameters: [tokenParam],
      responses: {
        ...ok(
          'Quiz loaded',
          obj({
            quiz: obj({
              _id: objectId(),
              title: str('Backend Screening'),
              questions: arr(
                obj({
                  _id: objectId(),
                  question_text: str('What does HTTP stand for?'),
                  option_a: str('HyperText Transfer Protocol'),
                  option_b: str('High Transfer Text Protocol'),
                  option_c: str('Hyperlink Text Transfer Protocol'),
                  option_d: str('None of the above'),
                  points: int(1),
                }),
              ),
            }),
            startedAt: str('2026-09-30 13:10:00', { description: 'Asia/Kolkata local time' }),
            expiresAt: str('2026-09-30 13:11:15', { description: 'Asia/Kolkata local time' }),
            status: str('started', { enum: ['pending', 'started', 'submitted', 'expired'] }),
          }),
        ),
        ...err(400, 'This quiz attempt is already completed or expired'),
        ...err(404, 'Invalid or expired quiz link / Quiz not found'),
      },
    },
  },
  '/api/quizzes/interview/quiz/attempt/{token}/submit': {
    post: {
      tags: [T.interview],
      summary: 'Submit interview quiz answers',
      description: 'Accepted up to 3 seconds after expiry. Answers are stored as submitted (not scored).',
      security: PUBLIC,
      parameters: [tokenParam],
      requestBody: jsonBody(obj({ answers: arr(obj({ question_id: objectId(), selected_option: str('HyperText Transfer Protocol') })) })),
      responses: {
        ...created('Quiz submitted successfully', obj({ submittedAt: dateTime() })),
        ...err(400, 'Already completed/expired, not started, or time is up'),
        ...err(404, 'Invalid or expired quiz link / Quiz not found'),
      },
    },
  },
  '/api/legal/{type}': {
    get: {
      tags: [T.legal],
      summary: 'Get a legal page by type',
      security: PUBLIC,
      parameters: [pathParam('type', 'Page type, case-insensitive (e.g. privacy, terms, user-manual)', { type: 'string', example: 'privacy' })],
      responses: {
        ...ok('Legal page fetched', obj({ page: ref('LegalPage') })),
        ...err(404, 'Legal page not found'),
      },
    },
  },
};

