import { ProjectRepository } from '@app/domain/projects/project.repository';
import { Observable, of } from 'rxjs';
import { CreateProjectDto } from '@app/domain/projects/dto/create-project.dto';
import { mockProjects } from '@app/testing/project.mock';
import { Project } from '@app/domain/projects/project.model';

export class FakeProjectRepository implements ProjectRepository {
  private projects: Project[] = [...mockProjects];

  create(projectDto: CreateProjectDto): Observable<Project> {
    const newProject: Project = {
      ...projectDto,
      fichier: '',
      id: (this.projects.length + 1).toString(),
      created: new Date().toISOString(),
      updated: new Date().toISOString(),
    };
    this.projects.push(newProject);
    return of(newProject);
  }

  list(userId: string): Observable<Project[]> {
    const filtered = this.projects.filter((p) => p.utilisateur === userId);
    return of(filtered);
  }

  get(projectId: string): Observable<Project> {
    const found = this.projects.find((p) => p.id === projectId) ?? ({} as Project);
    return of(found);
  }

  update(id: string, data: Partial<Project>): Observable<Project> {
    const index = this.projects.findIndex((p) => p.id === id);
    if (index === -1) {
      return of({} as Project);
    }
    const updated: Project = {
      ...this.projects[index],
      ...data,
      updated: new Date().toISOString(),
    };
    this.projects[index] = updated;
    return of(updated);
  }
}
