import apiClient from './apiClient';

export interface WorkflowStep {
  title: string;
  description?: string;
  task_type: string;
  priority?: 'low' | 'medium' | 'high' | 'urgent';
  assigned_to?: number;
  estimated_duration_minutes?: number;
  due_date_offset_hours?: number;
  metadata?: Record<string, any>;
}

export interface Workflow {
  id: number;
  name: string;
  description?: string;
  workflow_type: 'pre_arrival' | 'room_preparation' | 'maintenance' | 'guest_communication' | 'follow_up' | 'custom';
  trigger: 'booking_created' | 'booking_confirmed' | 'check_in' | 'check_out' | 'maintenance_request' | 'guest_message' | 'manual' | 'scheduled';
  status: 'active' | 'inactive' | 'archived';
  sla_hours?: number;
  steps: WorkflowStep[];
  trigger_config?: Record<string, any>;
  created_at: string;
  updated_at: string;
}

export interface WorkflowInstance {
  id: number;
  workflow_id: number;
  booking_id?: number;
  room_id?: number;
  user_id?: number;
  status: 'pending' | 'in_progress' | 'completed' | 'cancelled';
  started_at: string;
  completed_at?: string;
  due_date?: string;
}

export interface CreateWorkflowRequest {
  name: string;
  description?: string;
  workflow_type: string;
  trigger: string;
  steps: WorkflowStep[];
  trigger_config?: Record<string, any>;
  sla_hours?: number;
}

export interface UpdateWorkflowRequest {
  name?: string;
  description?: string;
  steps?: WorkflowStep[];
  status?: string;
  trigger_config?: Record<string, any>;
  sla_hours?: number;
}

export interface TriggerWorkflowRequest {
  booking_id?: number;
  room_id?: number;
  user_id?: number;
  metadata?: Record<string, any>;
}

const workflowService = {
  // Workflow CRUD
  createWorkflow: async (data: CreateWorkflowRequest) => {
    return apiClient.post<{ status: string; data: Workflow }>('/workflows/', data);
  },

  getWorkflows: async (params?: {
    workflow_type?: string;
    status?: string;
    skip?: number;
    limit?: number;
  }) => {
    return apiClient.get<{ status: string; data: Workflow[] }>('/workflows/', { params });
  },

  getWorkflow: async (id: number) => {
    return apiClient.get<{ status: string; data: Workflow }>(`/workflows/${id}`);
  },

  updateWorkflow: async (id: number, data: UpdateWorkflowRequest) => {
    return apiClient.put<{ status: string; data: Workflow }>(`/workflows/${id}`, data);
  },

  deleteWorkflow: async (id: number) => {
    return apiClient.delete<{ status: string; message: string }>(`/workflows/${id}`);
  },

  // Workflow Instances
  triggerWorkflow: async (workflowId: number, data: TriggerWorkflowRequest) => {
    return apiClient.post<{ status: string; data: WorkflowInstance }>(`/workflows/${workflowId}/trigger`, data);
  },

  getWorkflowInstances: async (params?: {
    workflow_id?: number;
    booking_id?: number;
    status?: string;
    skip?: number;
    limit?: number;
  }) => {
    return apiClient.get<{ status: string; data: WorkflowInstance[] }>('/workflows/instances/', { params });
  },

  completeWorkflowInstance: async (instanceId: number) => {
    return apiClient.post<{ status: string; data: WorkflowInstance }>(`/workflows/instances/${instanceId}/complete`);
  },

  // Predefined workflows
  getPreArrivalWorkflows: async () => {
    return apiClient.get<{ status: string; data: Workflow[] }>('/workflows/types/pre-arrival');
  },

  getRoomPreparationWorkflows: async () => {
    return apiClient.get<{ status: string; data: Workflow[] }>('/workflows/types/room-preparation');
  },
};

export default workflowService;

