import crypto from 'crypto';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import { join } from 'path';

const FEEDBACK_FILE_NAME = 'feedback.json';

function ensureLogsDir(baseDir) {
  const logsDir = join(baseDir, '../logs');
  if (!existsSync(logsDir)) {
    mkdirSync(logsDir, { recursive: true });
  }
  return logsDir;
}

function getFeedbackPath(baseDir) {
  return join(ensureLogsDir(baseDir), FEEDBACK_FILE_NAME);
}

export function ensureFeedbackFile(baseDir) {
  const feedbackPath = getFeedbackPath(baseDir);
  if (!existsSync(feedbackPath)) {
    writeFileSync(feedbackPath, '[]\n', 'utf-8');
  }
}

export function loadFeedback(baseDir) {
  ensureFeedbackFile(baseDir);
  const feedbackPath = getFeedbackPath(baseDir);
  const raw = readFileSync(feedbackPath, 'utf-8').trim();
  if (!raw) return [];

  try {
    const parsed = JSON.parse(raw);
    return Array.isArray(parsed) ? parsed : [];
  } catch (error) {
    throw new Error(`Ungültige ${FEEDBACK_FILE_NAME}: ${error.message}`);
  }
}

export function saveFeedback(baseDir, items) {
  const feedbackPath = getFeedbackPath(baseDir);
  writeFileSync(feedbackPath, `${JSON.stringify(items, null, 2)}\n`, 'utf-8');
}

export function createFeedbackEntry(input) {
  return {
    id: crypto.randomUUID(),
    createdAt: new Date().toISOString(),
    name: typeof input.name === 'string' ? input.name.trim() : '',
    age: Number.isFinite(input.age) ? input.age : null,
    country: typeof input.country === 'string' ? input.country.trim() : '',
    gender: typeof input.gender === 'string' ? input.gender.trim() : '',
    comment: typeof input.comment === 'string' ? input.comment.trim() : ''
  };
}
