'use client';

import Link from 'next/link';
import { DmIcon } from '@/features/data-model/ui/icons';
import type { OsObject } from './config';
import { Soon } from './primitives';

/**
 * Record / Preview Customization: team views of the record page and preview panel. The API has
 * no record layouts yet (a record's "Customize this record" is kept in the browser), so only the
 * default view is listed.
 */
export function ViewsTab({ object: o, tab }: { object: OsObject; tab: 'record' | 'preview' }) {
  const isRecord = tab === 'record';
  return (
    <section
      className="os-panel"
      aria-label={isRecord ? 'Record Customization' : 'Preview Customization'}
    >
      <p className="os-intro">
        {isRecord
          ? `Create views to customize the layout and content of ${o.singular} records.`
          : `Customize the preview panel for ${o.singular} records.`}
      </p>
      <div className="os-toolbar">
        <label className="os-search">
          <input
            type="search"
            placeholder="Search by view name"
            aria-label="Search by view name"
            disabled
          />
          <DmIcon name="search" size={16} />
        </label>
        <button type="button" className="btn-crm btn-crm--outline btn-crm--sm" disabled>
          Create team view
        </button>
        <Soon>Team views coming soon</Soon>
      </div>
      <div className="os-table-wrap">
        <table className="os-table">
          <thead>
            <tr>
              <th className="os-table__check">
                <span className="check">
                  <input type="checkbox" aria-label="Select all views" disabled />
                  <span className="check__box" aria-hidden />
                </span>
              </th>
              <th>View name</th>
              <th>Assigned to</th>
              <th>Last updated</th>
              <th className="os-table__actions">Actions</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td className="os-table__check">
                <span className="check">
                  <input type="checkbox" aria-label="Select Default view" disabled />
                  <span className="check__box" aria-hidden />
                </span>
              </td>
              <td>
                <span className="os-viewname">
                  <span className="os-viewname__icon">
                    <DmIcon name="pencil" size={12} />
                  </span>
                  {o.listPath ? (
                    <Link className="os-link" href={o.listPath}>
                      Default view
                    </Link>
                  ) : (
                    'Default view'
                  )}
                </span>
              </td>
              <td>All unassigned teams and users</td>
              <td>--</td>
              <td className="os-table__actions">
                <button
                  type="button"
                  className="os-dots"
                  disabled
                  aria-label="Default view can't be edited or deleted"
                  title="The default view can't be edited or deleted"
                >
                  <DmIcon name="more-vertical" size={16} />
                </button>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
      {isRecord && o.listPath ? (
        <p className="os-block__text os-gap-sm">
          To change what one record page shows in your browser, open any {o.singular.toLowerCase()}{' '}
          and use &quot;Customize this record&quot;.
        </p>
      ) : null}
    </section>
  );
}

/** Index Customization: links to the object's list (its views and default tabs live there). */
export function IndexTab({ object: o }: { object: OsObject }) {
  const href = o.listPath ?? '/data-model?tab=manage';
  return (
    <section className="os-panel" aria-label="Index Customization">
      <p className="os-intro">Control the layout and content of your {o.singular} index page.</p>
      <h2 className="os-index__title">Customize Index Page</h2>
      <div className="os-block os-block--tight">
        <Link className="os-link" href={href}>
          All Views
        </Link>
        <p className="os-block__text">See and take action on all of your views in one place.</p>
      </div>
      <div className="os-block os-block--tight">
        <Link className="os-link" href={href}>
          Default view customization
        </Link>
        <p className="os-block__text">
          For new users added to your CRM account, the account-wide default tab configuration will
          automatically appear when they navigate to that object.
        </p>
      </div>
    </section>
  );
}
