import { requestClient } from '#/api/request';

export namespace ExperimentApi {
  export interface ExperimentItem {
    id: number;
    name: string;
    description: string;
    status: number;
    start_time: null | string;
    end_time: null | string;
    created_at: string;
    updated_at: string;
  }

  export interface ListParams {
    page?: number;
    page_size?: number;
    status?: number;
  }

  export interface ListResponse {
    list: ExperimentItem[];
    page: number;
    page_size: number;
    total: number;
  }

  export interface CreateParams {
    description: string;
    end_time?: null | string;
    name: string;
    start_time?: null | string;
    status?: number;
    variants?: CreateVariantParams[];
    whitelist?: CreateExperimentWhitelistParams[];
  }

  export interface VariantItem {
    created_at: string;
    description: string;
    experiment_id: number;
    id: number;
    name: string;
    params: null | Record<string, any>;
    updated_at: string;
    weight: number;
  }

  export interface CreateVariantParams {
    description?: string;
    name: string;
    params?: any;
    weight?: number;
  }

  export interface UpdateVariantParams {
    description?: string;
    name?: string;
    params?: any;
    weight?: number;
  }

  export interface UpdateExperimentVariantParams {
    description?: string;
    id?: number;
    name: string;
    params?: any;
    weight?: number;
  }

  export interface WhitelistItem {
    created_at: string;
    experiment_id: number;
    id: number;
    user_id: string;
    variant_id: number;
  }

  export interface CreateWhitelistParams {
    user_id: string;
    variant_id: number;
  }

  export interface CreateExperimentWhitelistParams {
    user_id: string;
    variant_name: string;
  }

  export interface BatchCreateWhitelistParams {
    items: CreateWhitelistParams[];
  }

  export interface VariantStats {
    convert_count: number;
    convert_rate: number;
    exposure_count: number;
    total_value: number;
    user_count: number;
    variant_id: number;
    variant_name: string;
  }

  export interface ExperimentResult {
    experiment_id: number;
    experiment_name: string;
    status: number;
    variants: VariantStats[];
  }

  export interface UserExperimentGroupItem {
    experiment_id: number;
    experiment_name: string;
    params: null | Record<string, any>;
    variant_id: number;
    variant_name: string;
  }

  export interface UpdateParams {
    description?: string;
    end_time?: null | string;
    name?: string;
    start_time?: null | string;
    status?: number;
    variants?: UpdateExperimentVariantParams[];
    whitelist?: CreateExperimentWhitelistParams[];
  }
}

export async function getExperimentListApi(params: ExperimentApi.ListParams) {
  return requestClient.get<ExperimentApi.ListResponse>('/v1/experiments', {
    params,
  });
}

export async function createExperimentApi(params: ExperimentApi.CreateParams) {
  return requestClient.post<ExperimentApi.ExperimentItem>('/v1/experiments', params);
}

export async function updateExperimentApi(
  id: number,
  params: ExperimentApi.UpdateParams,
) {
  return requestClient.put<ExperimentApi.ExperimentItem>(`/v1/experiments/${id}`, params);
}

export async function deleteExperimentApi(id: number) {
  return requestClient.delete(`/v1/experiments/${id}`);
}

export async function getExperimentVariantsApi(experimentId: number) {
  return requestClient.get<ExperimentApi.VariantItem[]>(
    `/v1/experiments/${experimentId}/variants`,
  );
}

export async function createExperimentVariantApi(
  experimentId: number,
  params: ExperimentApi.CreateVariantParams,
) {
  return requestClient.post<ExperimentApi.VariantItem>(
    `/v1/experiments/${experimentId}/variants`,
    params,
  );
}

export async function updateExperimentVariantApi(
  experimentId: number,
  variantId: number,
  params: ExperimentApi.UpdateVariantParams,
) {
  return requestClient.put<ExperimentApi.VariantItem>(
    `/v1/experiments/${experimentId}/variants/${variantId}`,
    params,
  );
}

export async function deleteExperimentVariantApi(
  experimentId: number,
  variantId: number,
) {
  return requestClient.delete(`/v1/experiments/${experimentId}/variants/${variantId}`);
}

export async function getExperimentWhitelistApi(experimentId: number) {
  return requestClient.get<ExperimentApi.WhitelistItem[]>(
    `/v1/experiments/${experimentId}/whitelist`,
  );
}

export async function createExperimentWhitelistApi(
  experimentId: number,
  params: ExperimentApi.CreateWhitelistParams,
) {
  return requestClient.post<ExperimentApi.WhitelistItem>(
    `/v1/experiments/${experimentId}/whitelist`,
    params,
  );
}

export async function batchCreateExperimentWhitelistApi(
  experimentId: number,
  params: ExperimentApi.BatchCreateWhitelistParams,
) {
  return requestClient.post<ExperimentApi.WhitelistItem[]>(
    `/v1/experiments/${experimentId}/whitelist/batch`,
    params,
  );
}

export async function removeExperimentWhitelistApi(
  experimentId: number,
  userId: string,
) {
  return requestClient.delete(`/v1/experiments/${experimentId}/whitelist/${userId}`);
}

export async function getExperimentResultApi(experimentId: number) {
  return requestClient.get<ExperimentApi.ExperimentResult>(
    `/v1/experiments/${experimentId}/results`,
  );
}

export async function getUserExperimentGroupsApi(userId: string) {
  return requestClient.get<ExperimentApi.UserExperimentGroupItem[]>(
    `/v1/users/${encodeURIComponent(userId)}/groups`,
  );
}