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 {
  roleShowConfig as showConfig,
  roleShowPrismaSelect as showPrismaSelect,
} from '../../generated/props/role/config';
import { roleModel } from '../../generated/props/role/model';
import type { Role } from '../../generated/types/prisma.types'; //'.prisma/client/index';
import { getShowField } from '../../helpers';

const model: ModelType = produce(roleModel, (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('Role', showConfig.fields, {
  ...modelsMap,
  Role: { model, meta: modelsMap['Role'].meta },
});

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

  const { queryResult } = useShow<Role>({
    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>
  );
};
