import { ProfileRepository } from '@app/domain/profiles/profile.repository';
import { from, Observable } from 'rxjs';
import { Profile } from '@app/domain/profiles/profile.model';
import { Injectable } from '@angular/core';
import PocketBase from 'pocketbase';
import { environment } from '@env/environment';
import { ProfileDTO } from '@app/domain/profiles/dto/create-profile.dto';

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

  list(): Observable<Profile[]> {
    return from(this.pb.collection('profiles').getFullList<Profile>({ sort: 'profession' }));
  }

  getByUserId(userId: string): Observable<Profile> {
    return from(
      this.pb.collection('profiles').getFirstListItem<Profile>(`utilisateur="${userId}"`)
    );
  }

  create(profile: ProfileDTO): Observable<Profile> {
    return from(this.pb.collection('profiles').create<Profile>(profile));
  }

  update(id: string, data: Partial<Profile>): Observable<Profile> {
    return from(this.pb.collection('profiles').update<Profile>(id, data));
  }
}
