import { mdiArrowLeft, mdiEyeOutline } from '@mdi/js';
import React from 'react';
import BaseButton from '../BaseButton';
import BaseButtons from '../BaseButtons';
import CardBox from '../CardBox';
import {
  emptyValue,
  formatPrimitiveValue,
  getPrimaryTitle,
  getRecordSubtitle,
  getSummaryEntries,
  humanizeLabel,
} from '../EntityPageUtils';

type Props = {
  entityLabel: string;
  pluralLabel: string;
  listHref: string;
  viewHref?: string;
  record: any;
  children: React.ReactNode;
};

const EnhancedEntityEditShell = ({ entityLabel, pluralLabel, listHref, viewHref, record, children }: Props) => {
  const title = getPrimaryTitle(record, `Edit ${entityLabel}`);
  const subtitle = getRecordSubtitle(record, `${entityLabel} record`);
  const summaryEntries = getSummaryEntries(record, 7);

  return (
    <div className="grid gap-6 xl:grid-cols-[minmax(0,1fr)_320px] xl:items-start">
      <div className="rounded-2xl border border-slate-200/80 bg-white p-5 shadow-sm dark:border-dark-700 dark:bg-dark-900">
        <div className="mb-6 rounded-xl border border-slate-200/80 bg-slate-50/80 p-5 dark:border-dark-700 dark:bg-dark-800/60">
          <div className="flex flex-col gap-4 lg:flex-row lg:items-start lg:justify-between">
            <div className="max-w-2xl">
              <p className="text-xs font-semibold uppercase tracking-[0.2em] text-slate-500 dark:text-slate-400">Edit workspace</p>
              <h3 className="mt-2 text-2xl font-semibold text-slate-900 dark:text-slate-100">{title}</h3>
              <p className="mt-2 text-sm text-slate-600 dark:text-slate-300">{subtitle}. Update the fields below and keep the record details clear, complete, and ready for downstream users.</p>
            </div>
            <BaseButtons type="justify-start md:justify-end" className="w-full lg:w-auto" noWrap>
              <BaseButton color="white" outline icon={mdiArrowLeft} label={`Back to ${pluralLabel.toLowerCase()}`} href={listHref} />
              {viewHref ? <BaseButton color="white" outline icon={mdiEyeOutline} label={`View ${entityLabel.toLowerCase()}`} href={viewHref} /> : null}
            </BaseButtons>
          </div>
        </div>

        <div className="grid gap-6 md:grid-cols-2 [&>div:not(:last-child)]:min-w-0 [&>div:not(:last-child)]:rounded-xl [&>div:not(:last-child)]:border [&>div:not(:last-child)]:border-slate-200/80 [&>div:not(:last-child)]:bg-slate-50/50 [&>div:not(:last-child)]:p-4 dark:[&>div:not(:last-child)]:border-dark-700 dark:[&>div:not(:last-child)]:bg-dark-800/40 [&>hr]:md:col-span-2 [&>div:last-child]:md:col-span-2">
          {children}
        </div>
      </div>

      <div className="space-y-6 xl:sticky xl:top-6">
        <CardBox>
          <div className="space-y-5">
            <div className="flex items-center gap-3">
              <div className="rounded-lg border border-slate-200 bg-slate-50 p-2 text-slate-500 dark:border-dark-700 dark:bg-dark-800 dark:text-slate-300">
                <span className="inline-flex"><svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor"><path d="M4 19H20V21H4V19M17 7L15 5L17 3L19 5L17 7M6 17H4V15L14 5L16 7L6 17Z" /></svg></span>
              </div>
              <div>
                <h3 className="text-lg font-semibold text-slate-900 dark:text-slate-100">Editing summary</h3>
                <p className="mt-1 text-sm text-slate-600 dark:text-slate-300">Key values stay visible while the form is being updated.</p>
              </div>
            </div>

            <div className="space-y-3">
              {summaryEntries.length > 0 ? (
                summaryEntries.map(([key, value]) => (
                  <div key={key} className="rounded-xl border border-slate-200/80 bg-slate-50/70 p-4 dark:border-dark-700 dark:bg-dark-800/60">
                    <p className="text-xs font-semibold uppercase tracking-[0.18em] text-slate-500 dark:text-slate-400">{humanizeLabel(key)}</p>
                    <div className="mt-2 text-sm font-medium text-slate-900 dark:text-slate-100">{value instanceof Date || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' ? formatPrimitiveValue(key, value) : value || emptyValue}</div>
                  </div>
                ))
              ) : (
                <div className="rounded-xl border border-dashed border-slate-200 bg-slate-50/70 px-4 py-6 text-sm text-slate-600 dark:border-dark-700 dark:bg-dark-800/50 dark:text-slate-300">
                  Summary fields will appear here as soon as this record has saved content.
                </div>
              )}
            </div>
          </div>
        </CardBox>

        <CardBox>
          <div className="space-y-3">
            <h3 className="text-lg font-semibold text-slate-900 dark:text-slate-100">Editing guidance</h3>
            <ul className="space-y-2 text-sm text-slate-600 dark:text-slate-300">
              <li>• Keep naming and reference fields consistent with list and dashboard views.</li>
              <li>• Use notes and description fields to add decision-quality context, not placeholders.</li>
              <li>• Reset only if you want to discard unsaved changes in this form session.</li>
            </ul>
          </div>
        </CardBox>
      </div>
    </div>
  );
};

export default EnhancedEntityEditShell;
