import { Injectable, inject } from '@angular/core';
import { Observable, of } from 'rxjs';
import { ApiService } from './api.service';
import { Enrollment, CourseProgress } from '../models/types';

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

  enroll(courseId: string, userId: string): Observable<Enrollment> {
    return this.api.post<Enrollment>(`/api/courses/${courseId}/enroll`, { userId, courseId });
  }

  getEnrollments(userId: string): Observable<Enrollment[]> {
    return this.api.get<Enrollment[]>(`/api/users/${userId}/enrollments`);
  }

  getCourseProgress(courseId: string, userId: string): Observable<CourseProgress> {
    return this.api.get<CourseProgress>(`/api/courses/${courseId}/progress`, { userId });
  }

  markTopicProgress(topicId: string, userId: string, completed: boolean): Observable<void> {
    return this.api.post<void>(`/api/topics/${topicId}/progress`, { userId, topicId, completed });
  }

  markResourceProgress(resourceId: string, userId: string, completed: boolean): Observable<void> {
    return this.api.post<void>(`/api/resources/${resourceId}/progress`, { userId, resourceId, completed });
  }
}
