import { apiSlice } from '.';
import URLS from './constants';

export interface ActivityPoint {
    activityId: string;
    points: number;
  }
  
  export interface LeadScoreConfig {
    id: string;
    companyId: { id: string };
    activityPoints: ActivityPoint[];
    createdBy?: { id: string };
    updatedBy?: { id: string };
  }
  
  export interface GetLeadScoreListResponse {
    data: {
      results: LeadScoreConfig[];
      page: number;
      limit: number;
      totalPages: number;
      totalResults: number;
    };
    code: number;
    message: string;
    success: boolean;
    error?: object;
  }
  
  export interface GetLeadScoreByIdResponse {
    data: LeadScoreConfig;
    code: number;
    message: string;
    success: boolean;
    error?: object;
  }
  
  export interface CreateLeadScoreRequest {
    activityPoints: ActivityPoint[];
  }
  
  export interface UpdateLeadScoreRequest {
    id: string;
    activityPoints: ActivityPoint[];
  }
  

export const leadScoreApi = apiSlice.injectEndpoints({
  endpoints: (builder) => ({
    getLeadScoreConfigs: builder.query<GetLeadScoreListResponse, { companyId: string }>({
      query: ({ companyId }) => ({
        url: `${URLS.LEAD_SCORE}?companyId=${companyId}`,
        method: 'GET',
      }),
      providesTags: ['LeadScore'],
    }),

    createLeadScoreConfig: builder.mutation<LeadScoreConfig, CreateLeadScoreRequest>({
      query: (body) => ({
        url: URLS.LEAD_SCORE,
        method: 'POST',
        body,
      }),
      invalidatesTags: ['LeadScore'],
    }),

    updateLeadScoreConfig: builder.mutation<LeadScoreConfig, UpdateLeadScoreRequest>({
      query: ({ id, ...body }) => ({
        url: `${URLS.LEAD_SCORE}/${id}`,
        method: 'PATCH',
        body,
      }),
      invalidatesTags: ['LeadScore'],
    }),
  }),
  overrideExisting: false,
});

export const {
  useGetLeadScoreConfigsQuery,
  useCreateLeadScoreConfigMutation,
  useUpdateLeadScoreConfigMutation,
} = leadScoreApi;
