import { inject, Injectable, signal } from '@angular/core';
import { PROJECT_REPOSITORY_TOKEN } from '@app/infrastructure/projects/project-repository.token';
import { CreateProjectUseCase } from '@app/usecase/projects/create-project.usecase';
import { ListProjectUseCase } from '@app/usecase/projects/list-project.usecase';
import { GetProjectUseCase } from '@app/usecase/projects/get-project.usecase';
import { UpdateProjectUseCase } from '@app/usecase/projects/update-project.usecase';
import { Project } from '@app/domain/projects/project.model';
import { ProjectViewModel } from '@app/ui/projects/project.presenter.model';
import { ProjectPresenter } from '@app/ui/projects/project.presenter';
import { CreateProjectDto } from '@app/domain/projects/dto/create-project.dto';
import { ErrorResponse } from '@app/domain/error-response.util';
import { ActionType } from '@app/domain/action-type.util';
import { LoaderAction } from '@app/domain/loader-action.util';

@Injectable({
  providedIn: 'root',
})
export class ProjectFacade {
  private readonly projectRepo = inject(PROJECT_REPOSITORY_TOKEN);

  private readonly createUseCase = new CreateProjectUseCase(this.projectRepo);
  private readonly listUseCase = new ListProjectUseCase(this.projectRepo);
  private readonly getUseCase = new GetProjectUseCase(this.projectRepo);
  private readonly UpdateUseCase = new UpdateProjectUseCase(this.projectRepo);

  readonly projects = signal<ProjectViewModel[]>([]);
  readonly project = signal<ProjectViewModel>({} as ProjectViewModel);
  readonly loading = signal<LoaderAction>({ isLoading: false, action: ActionType.NONE });
  readonly error = signal<ErrorResponse>({
    action: ActionType.NONE,
    hasError: false,
    message: null,
  });

  private readonly projectPresenter = new ProjectPresenter();

  load(userId: string) {
    this.handleError(ActionType.READ, false, null, true);

    this.listUseCase.execute(userId).subscribe({
      next: (projects: Project[]) => {
        this.projects.set(this.projectPresenter.toViewModels(projects));
        this.handleError(ActionType.READ, false, null, false);
      },
      error: (err) => {
        this.handleError(ActionType.READ, false, err, false);
      },
      complete: () => {},
    });
  }

  loadOne(projectId: string) {
    this.handleError(ActionType.READ, false, null, true);

    this.getUseCase.execute(projectId).subscribe({
      next: (project: Project) => {
        this.project.set(this.projectPresenter.toViewModel(project));
        this.handleError(ActionType.READ, false, null, false);
      },
      error: (err) => {
        this.handleError(ActionType.READ, false, err, false);
      },
    });
  }

  create(projectDto: CreateProjectDto) {
    this.handleError(ActionType.CREATE, false, null, true);

    this.createUseCase.execute(projectDto).subscribe({
      next: (project: Project) => {
        this.project.set(this.projectPresenter.toViewModel(project));
        this.projects.update((prev) => [...prev, this.projectPresenter.toViewModel(project)]);
        this.handleError(ActionType.CREATE, false, null, false);
      },
      error: (err) => {
        this.handleError(ActionType.CREATE, false, err, false);
      },
    });
  }

  update(userId: string, data: any) {
    this.handleError(ActionType.UPDATE, false, null, true);

    this.UpdateUseCase.execute(userId, data).subscribe({
      next: (project: Project) => {
        this.project.set(this.projectPresenter.toViewModel(project));
        this.handleError(ActionType.UPDATE, false, null, false);
      },
      error: (err) => {
        this.handleError(ActionType.UPDATE, false, err, false);
      },
    });
  }

  private handleError(
    action: ActionType = ActionType.NONE,
    hasError: boolean,
    message: string | null = null,
    isLoading = false
  ) {
    this.error.set({ action, hasError, message });
    this.loading.set({ action, isLoading });
  }
}
