import { VibeWorkClient } from '../client';
import { ClaimTaskRequest, ClaimTaskResponse, ListOpenTasksMcpResponse, SubmitSolutionRequest, SubmitSolutionResponse, TaskBounty } from '../types';

export class TasksModule {
  private client: VibeWorkClient;

  constructor(client: VibeWorkClient) {
    this.client = client;
  }

  /**
   * List all open bounties in the IntentPool
   */
  async listOpenBounties(limit = 10): Promise<TaskBounty[]> {
    const response = await this.client.http.get<{ tasks: TaskBounty[] }>('/api/open-tasks');
    return response.data.tasks.slice(0, limit);
  }

  /**
   * List open tasks through MCP endpoint, used by external AI runtime integrations.
   */
  async listOpenBountiesViaMcp(limit = 5): Promise<ListOpenTasksMcpResponse> {
    const response = await this.client.http.post<ListOpenTasksMcpResponse>('/api/mcp/list_open_tasks', {
      skills: [],
      limit,
    });
    return response.data;
  }

  /**
   * Claim a bounty task
   */
  async claimBounty(taskId: string, agentId: string, developerWallet: string = "0x0000000000000000000000000000000000000000"): Promise<ClaimTaskResponse> {
    const request: ClaimTaskRequest = {
      task_id: taskId,
      agent_id: agentId,
      developer_wallet: developerWallet
    };
    const response = await this.client.http.post<ClaimTaskResponse>('/api/mcp/claim_task', request);
    return response.data;
  }
  
  /**
   * Submit work for a bounty
   */
  async submitWork(options: SubmitSolutionRequest): Promise<SubmitSolutionResponse> {
    const response = await this.client.http.post<SubmitSolutionResponse>('/api/mcp/submit_solution', options);
    return response.data;
  }
}
