/**
 * @copyright 2026 NHCarrigan
 * @license Naomi's Public License
 * @author Naomi Carrigan
 */

import { Injectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
import { ApiService } from './api.service';
import type {
  ProfileReportWithUsers,
  CreateReportDto,
  ReportStatus
} from '@library/shared-types';

@Injectable({
  providedIn: 'root'
})
export class ReportService {
  private api = inject(ApiService);

  /**
   * Create a new profile report.
   *
   * @param reportedUserId - The ID of the user being reported
   * @param reason - The reason for the report
   * @param details - Additional details about the report
   * @returns Observable of the created report
   */
  createReport(reportedUserId: string, reason: string, details: string): Observable<ProfileReportWithUsers> {
    const dto: CreateReportDto = {
      reportedUserId,
      reason: reason as CreateReportDto['reason'],
      details
    };
    return this.api.post<ProfileReportWithUsers>('/reports', dto);
  }

  /**
   * Get all reports (admin only). Optionally filter by status.
   *
   * @param status - Optional status to filter by
   * @returns Observable of all matching reports
   */
  getAllReports(status?: ReportStatus): Observable<ProfileReportWithUsers[]> {
    const url = status ? `/reports?status=${status}` : '/reports';
    return this.api.get<ProfileReportWithUsers[]>(url);
  }

  /**
   * Get a specific report by ID (admin only).
   *
   * @param id - The report ID
   * @returns Observable of the report
   */
  getReportById(id: string): Observable<ProfileReportWithUsers> {
    return this.api.get<ProfileReportWithUsers>(`/reports/${id}`);
  }

  /**
   * Update a report's status and review notes (admin only).
   *
   * @param id - The report ID
   * @param status - The new status
   * @param reviewNotes - Optional review notes
   * @returns Observable of the updated report
   */
  updateReport(id: string, status: ReportStatus, reviewNotes?: string): Observable<ProfileReportWithUsers> {
    return this.api.put<ProfileReportWithUsers>(`/reports/${id}`, {
      status,
      reviewNotes
    });
  }
}
