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

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

  createTopic(courseId: string, title: string, description: string | null, position: number): Observable<Topic> {
    return this.api.post<Topic>('/api/topics', { courseId, title, description, position });
  }

  updateTopic(id: string, title: string, description: string, position: number): Observable<Topic> {
    return this.api.put<Topic>(`/api/topics/${id}`, { id, title, description, position });
  }

  deleteTopic(id: string): Observable<void> {
    return this.api.delete<void>(`/api/topics/${id}`);
  }

  linkResource(topicId: string, resourceId: string, position: number): Observable<void> {
    return this.api.post<void>(`/api/topics/${topicId}/resources/${resourceId}`, { position });
  }

  unlinkResource(topicId: string, resourceId: string): Observable<void> {
    return this.api.delete<void>(`/api/topics/${topicId}/resources/${resourceId}`);
  }
}
