import { ModelType, getColumns, produceModel } from '@gadmin2n/react-common';
import {
  CloneButton,
  DeleteButton,
  EditButton,
  ListButton,
  RefreshButton,
  Show,
} from '@refinedev/antd';
import {
  IResourceComponentsProps,
  useShow,
  useTranslate,
} from '@refinedev/core';
import React, { useState } from 'react';
import { getShowField } from '../../helpers';
import type { Audit } from '../../generated/types/prisma.types'; //'.prisma/client/index';
import {
  auditShowConfig as showConfig,
  auditShowPrismaSelect as showPrismaSelect,
} from '../../generated/props/audit/config';
import { auditModel } from '../../generated/props/audit/model';
import { modelsMap } from 'generated/models.index';
import { agentAttrs } from '../../components/agentPanel/agentAttributes';

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

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

  const [[fields, model]] = useState(() => {
    const model: ModelType = produceModel(auditModel, {
      // status: { render: xxxxx }
    });

    // getColumns is a function that returns an array of fields
    const fields = getColumns('Audit', showConfig.fields, {
      ...modelsMap,
      Audit: { model, meta: modelsMap['Audit'].meta },
    });

    return [fields, model] as const;
  });

  return (
    <Show
      isLoading={isLoading}
      headerButtons={
        <div
          style={{ display: 'inline-flex', gap: 8 }}
          {...agentAttrs({ type: 'show-header', resource: resourceName })}
        >
          <ListButton />
          <EditButton recordItemId={record?.id} />
          <CloneButton recordItemId={record?.id} />
          <DeleteButton recordItemId={record?.id} />
          <RefreshButton
            recordItemId={record?.id}
            meta={{ select: showPrismaSelect }}
          />
        </div>
      }
    >
      {fields.map((field: any) => {
        return getShowField(field, resourceName, record, t, model, modelsMap);
      })}
    </Show>
  );
};
