// @todo: sample API controller
import type {
  IApiSuccess,
  IDataTableQuery,
  IDataFetcherResponse,
} from "@actindo/core-ui";
import { httpClient } from "@actindo/core-ui";
import type { ISampleEntity } from "./Entity.models";

import { BASE_API } from "../base";

import { MOCK_DATA } from "@/services/api/Sample/Entity/mock";
const randomTimeout = () => Math.random() * 3000 + 500;

const MODULE_BASE_API = `${BASE_API}MatchRules.`;

export const sampleEntityApi = {
  /** getList */
  async getList(
    query?: IDataTableQuery,
  ): Promise<IDataFetcherResponse<ISampleEntity>> {
    return httpClient.post(`${MODULE_BASE_API}getList`, query);
  },
  /** create entity */
  async create(entity: ISampleEntity): Promise<IApiSuccess> {
    return httpClient.post(`${MODULE_BASE_API}create`, entity);
  },
  /** remove entity */
  async remove(id: number): Promise<IApiSuccess> {
    return httpClient.post(`${MODULE_BASE_API}remove`, { id });
  },
  /** update entity */
  async update(entity: Partial<ISampleEntity>): Promise<IApiSuccess> {
    return httpClient.post(`${MODULE_BASE_API}update`, entity);
  },

  // mock APIs

  /** getList */
  async getMockedList(
    query?: IDataTableQuery,
  ): Promise<IDataFetcherResponse<ISampleEntity>> {
    return new Promise((resolve) => {
      const start = query?.start ?? 0;
      const limit = query?.limit ?? MOCK_DATA.length;
      setTimeout(() => {
        // eslint-disable-next-line no-console
        console.log("==> getMockedList request sent");
        resolve({
          data: MOCK_DATA.slice(start, limit),
          n_rows: MOCK_DATA.length,
          displayMessage: "",
          displayMessageTitle: "",
          error: "",
          job_id: 0,
          success: true,
        });
      }, randomTimeout());
    });
  },
  /** create entity */
  async mockedCreate(entity: ISampleEntity): Promise<IApiSuccess> {
    MOCK_DATA.push({
      id: new Date().getTime(),
      ...entity,
    });
    return new Promise((resolve) => {
      setTimeout(() => {
        // eslint-disable-next-line no-console
        console.log("==> mockedCreate request sent");
        resolve({
          displayMessage: "",
          displayMessageTitle: "",
          error: "",
          job_id: 0,
          success: true,
        });
      }, randomTimeout());
    });
  },
  /** remove entity */
  async mockedRemove(id: number): Promise<IApiSuccess> {
    const index = MOCK_DATA.findIndex((item) => item.id === id);
    const success = index >= 0;
    if (success) {
      MOCK_DATA.splice(index, 1);
    }
    return new Promise((resolve) => {
      setTimeout(() => {
        // eslint-disable-next-line no-console
        console.log("==> mockedRemove request sent");
        resolve({
          displayMessage: "",
          displayMessageTitle: "",
          error: "",
          job_id: 0,
          success,
        });
      }, randomTimeout());
    });
  },
  /** update entity */
  async mockedUpdate(entity: Partial<ISampleEntity>): Promise<IApiSuccess> {
    const index = MOCK_DATA.findIndex(({ id }) => id === entity.id);
    const success = index >= 0;
    if (success) {
      MOCK_DATA[index] = { ...MOCK_DATA[index], ...entity };
    }
    return new Promise((resolve) => {
      setTimeout(() => {
        // eslint-disable-next-line no-console
        console.log("==> mockedUpdate request sent");
        resolve({
          displayMessage: "",
          displayMessageTitle: "",
          error: "",
          job_id: 0,
          success,
        });
      }, randomTimeout());
    });
  },
};
