/**
 * submit-feedback.use-case.ts
 * Recibe el DTO, lo valida y guarda el feedback.
 * El userId viene del JWT (req.user.id), no del body — RBAC.
 */

import { SubmitFeedbackDtoValidator } from "../../dtos/index.js";
import type {
  FeedbackItem,
  FeedbackRepository,
} from "../../repositories/feedback.repository.js";

export class SubmitFeedbackUseCase {
  constructor(private readonly repository: FeedbackRepository) {}

  async execute(userId: number, data: unknown): Promise<FeedbackItem> {
    const dto = SubmitFeedbackDtoValidator.validate(data);

    const item: FeedbackItem = {
      id: `${userId}-${Date.now()}`,
      userId,
      type: dto.type,
      message: dto.message,
      createdAt: new Date().toISOString(),
    };
    if (dto.rating !== undefined) item.rating = dto.rating;

    await this.repository.save(item);
    return item;
  }
}
