import { apiClient } from '../api/api-client';
import type { RunbookRepository } from '@/domain/repositories/runbook.repository';
import type { Runbook, RunbookExecution } from '@/domain/entities/runbook';

export const apiRunbookRepository: RunbookRepository = {
  async list() {
    return apiClient<Runbook[]>('/api/v1/ops/runbooks');
  },

  async getById(id) {
    return apiClient<Runbook>(`/api/v1/ops/runbooks/${id}`);
  },

  async create(runbook) {
    return apiClient<Runbook>('/api/v1/ops/runbooks', {
      method: 'POST',
      body: runbook,
    });
  },

  async update(id, data) {
    return apiClient<Runbook>(`/api/v1/ops/runbooks/${id}`, {
      method: 'PUT',
      body: data,
    });
  },

  async remove(id) {
    await apiClient(`/api/v1/ops/runbooks/${id}`, {
      method: 'DELETE',
    });
  },

  async execute(id) {
    return apiClient<RunbookExecution>(`/api/v1/ops/runbooks/${id}/execute`, {
      method: 'POST',
    });
  },

  async getExecutions(id) {
    return apiClient<RunbookExecution[]>(`/api/v1/ops/runbooks/${id}/executions`);
  },
};
