import { Injectable } from '@angular/core';
import { environment } from '@env/environment';
import { ProjectRepository } from '@app/domain/projects/project.repository';
import { from, Observable } from 'rxjs';
import PocketBase from 'pocketbase';
import { CreateProjectDto } from '@app/domain/projects/dto/create-project.dto';
import { Project } from '@app/domain/projects/project.model';

@Injectable({
  providedIn: 'root',
})
export class PbProjectRepository implements ProjectRepository {
  private pb = new PocketBase(environment.baseUrl);

  create(project: CreateProjectDto): Observable<Project> {
    return from(this.pb.collection('projets').create<Project>(project));
  }
  list(userId: string): Observable<Project[]> {
    return from(
      this.pb.collection<Project>('projets').getFullList({ filter: `utilisateur='${userId}'` })
    );
  }
  get(projectId: string): Observable<Project> {
    return from(this.pb.collection<Project>('projets').getOne<Project>(projectId));
  }
  update(id: string, data: any): Observable<Project> {
    return from(this.pb.collection('projets').update<Project>(id, data));
  }
}
