import { FieldDataType, getColumns, ModelType } from '@gadmin2/react-common';
import {
  CloneButton,
  DeleteButton,
  EditButton,
  ListButton,
  RefreshButton,
  Show,
} from '@refinedev/antd';
import {
  IResourceComponentsProps,
  useShow,
  useTranslate,
} from '@refinedev/core';
import { modelsMap } from 'generated/models.index';
import produce from 'immer';
import React from 'react';
import {
  logShowConfig as showConfig,
  logShowPrismaSelect as showPrismaSelect,
} from '../../generated/props/log/config';
import { logModel } from '../../generated/props/log/model';
import type { Log } from '../../generated/types/prisma.types'; //'.prisma/client/index';
import { getShowField } from '../../helpers';

const model: ModelType = produce(logModel, (draft: FieldDataType) => {
  /*
  const publishedField = draft['status'] || {};
  publishedField.render = (value: any) => {
    return (
      <BooleanField
        value={value === '1'}
        trueIcon={<CheckCircleTwoTone twoToneColor="#52c41a" />}
        falseIcon={<CloseCircleTwoTone twoToneColor="red" />}
      />
    );
  };
  */
});

const fields = getColumns('Log', showConfig.fields, {
  ...modelsMap,
  Log: { model, meta: modelsMap['Log'].meta },
});

export const LogShow: React.FC<IResourceComponentsProps> = () => {
  const resourceName = 'log';
  const t = useTranslate();

  const { queryResult } = useShow<Log>({
    meta: { select: showPrismaSelect },
  });
  const { data, isLoading } = queryResult;
  const record = data?.data;

  return (
    <Show
      isLoading={isLoading}
      headerButtons={
        <>
          <ListButton />
          <EditButton recordItemId={record?.id} />
          <CloneButton recordItemId={record?.id} />
          <DeleteButton recordItemId={record?.id} />
          <RefreshButton
            recordItemId={record?.id}
            meta={{ select: showPrismaSelect }}
          />
        </>
      }
    >
      {fields.map((field: any) => {
        return getShowField(field, resourceName, record, t, model, modelsMap);
      })}
    </Show>
  );
};
