import { Injectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
import { ApiService } from '../../../core/services/api.service';
import {
  WorkflowResponseDto,
  CreateWorkflowDto,
  UpdateWorkflowDto,
  PaginatedWorkflowsResponse,
  WorkflowValidationResultDto,
} from '../../../api/models';

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

  getWorkflows(page = 1, limit = 10): Observable<PaginatedWorkflowsResponse> {
    return this.api.get<PaginatedWorkflowsResponse>('/workflows', { page, limit });
  }

  getWorkflow(id: string): Observable<WorkflowResponseDto> {
    return this.api.get<WorkflowResponseDto>(`/workflows/${id}`);
  }

  createWorkflow(dto: CreateWorkflowDto): Observable<WorkflowResponseDto> {
    return this.api.post<WorkflowResponseDto>('/workflows', dto);
  }

  updateWorkflow(id: string, dto: UpdateWorkflowDto): Observable<WorkflowResponseDto> {
    return this.api.patch<WorkflowResponseDto>(`/workflows/${id}`, dto);
  }

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

  validateWorkflow(dto: CreateWorkflowDto): Observable<WorkflowValidationResultDto> {
    return this.api.post<WorkflowValidationResultDto>('/workflows/validate', dto);
  }

  toggleActive(id: string, isActive: boolean): Observable<WorkflowResponseDto> {
    return this.api.patch<WorkflowResponseDto>(`/workflows/${id}`, { isActive });
  }
}
