// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.

import { defineForm } from '../ui/view.zod';

/**
 * Form Layout for Object Metadata Type
 */
export const objectForm = defineForm({
  schemaId: 'object',
  type: 'simple',
  sections: [
    {
      name: 'basics',
      label: 'Basics',
      description: 'Identity, labels, and taxonomy.',
      columns: 2,
      fields: [
        { field: 'name', type: 'text', required: true, immutable: true, colSpan: 1, helpText: 'snake_case unique identifier (immutable after creation)' },
        { field: 'label', type: 'text', colSpan: 1, helpText: 'Singular display name (e.g. "Account")' },
        { field: 'pluralLabel', type: 'text', colSpan: 1, helpText: 'Plural display name (e.g. "Accounts")' },
        { field: 'icon', type: 'text', colSpan: 1, helpText: 'Lucide icon name (e.g. "building", "users")' },
        { field: 'description', type: 'textarea', colSpan: 2, helpText: 'Developer documentation' },
        { field: 'tags', type: 'tags', colSpan: 2, helpText: 'Categorization tags (e.g. "sales", "system")' },
        { field: 'active', type: 'boolean', colSpan: 1, helpText: 'Is the object active and usable' },
        { field: 'isSystem', type: 'boolean', colSpan: 1, helpText: 'System object (protected from deletion)' },
        { field: 'abstract', type: 'boolean', colSpan: 1, helpText: 'Abstract base (cannot be instantiated)' },
      ],
    },
    {
      name: 'fields',
      label: 'Fields',
      description: 'Define the data model — each entry becomes a column in the database table.',
      fields: [
        {
          field: 'fields',
          type: 'record',
          required: true,
          helpText: 'Add the columns this object will store',
          keyField: {
            field: 'name',
            label: 'Name',
            placeholder: 'snake_case_identifier',
            helpText: 'snake_case machine name (used as column name and API key)',
            regex: '^[a-z_][a-z0-9_]*$',
            immutable: true,
          },
          fields: [
            { field: 'label', type: 'text', helpText: 'Display label' },
            {
              field: 'type',
              type: 'select',
              required: true,
              helpText: 'Field type',
              options: [
                { label: 'Text', value: 'text' },
                { label: 'Textarea', value: 'textarea' },
                { label: 'Email', value: 'email' },
                { label: 'URL', value: 'url' },
                { label: 'Phone', value: 'phone' },
                { label: 'Password', value: 'password' },
                { label: 'Markdown', value: 'markdown' },
                { label: 'HTML', value: 'html' },
                { label: 'Rich Text', value: 'richtext' },
                { label: 'Number', value: 'number' },
                { label: 'Currency', value: 'currency' },
                { label: 'Percent', value: 'percent' },
                { label: 'Date', value: 'date' },
                { label: 'Date & Time', value: 'datetime' },
                { label: 'Time', value: 'time' },
                { label: 'Boolean', value: 'boolean' },
                { label: 'Toggle', value: 'toggle' },
                { label: 'Select', value: 'select' },
                { label: 'Multiselect', value: 'multiselect' },
                { label: 'Radio', value: 'radio' },
                { label: 'Checkboxes', value: 'checkboxes' },
                { label: 'Lookup (reference)', value: 'lookup' },
                { label: 'Master–Detail', value: 'master_detail' },
                { label: 'Tree', value: 'tree' },
                { label: 'Image', value: 'image' },
                { label: 'File', value: 'file' },
                { label: 'Avatar', value: 'avatar' },
                { label: 'Video', value: 'video' },
                { label: 'Audio', value: 'audio' },
                { label: 'Formula (computed)', value: 'formula' },
                { label: 'Summary (rollup)', value: 'summary' },
                { label: 'Autonumber', value: 'autonumber' },
                { label: 'Composite (embedded)', value: 'composite' },
                { label: 'Repeater (embedded array)', value: 'repeater' },
                { label: 'Record (keyed map)', value: 'record' },
                { label: 'Location (GPS)', value: 'location' },
                { label: 'Address', value: 'address' },
                { label: 'Code', value: 'code' },
                { label: 'JSON', value: 'json' },
                { label: 'Color', value: 'color' },
                { label: 'Rating', value: 'rating' },
                { label: 'Slider', value: 'slider' },
                { label: 'Signature', value: 'signature' },
                { label: 'QR / Barcode', value: 'qrcode' },
                { label: 'Progress', value: 'progress' },
                { label: 'Tags', value: 'tags' },
                { label: 'Vector embedding', value: 'vector' },
              ],
            },
            { field: 'description', type: 'textarea', helpText: 'Developer documentation for this column' },
            { field: 'required', type: 'boolean', helpText: 'Must be set on every record' },
            { field: 'unique', type: 'boolean', helpText: 'Disallow duplicate values' },
            { field: 'indexed', type: 'boolean', helpText: 'Create a database index for faster querying' },
            { field: 'readonly', type: 'boolean', helpText: 'Visible but never user-editable' },
            { field: 'immutable', type: 'boolean', helpText: 'Editable on create, locked thereafter' },
            { field: 'hidden', type: 'boolean', helpText: 'Hidden from default UI' },
            { field: 'searchable', type: 'boolean', helpText: 'Include in full-text search' },
            { field: 'sortable', type: 'boolean', helpText: 'Allow sorting on this column' },
            { field: 'filterable', type: 'boolean', helpText: 'Allow filtering on this column' },
            { field: 'defaultValue', type: 'text', helpText: 'Default value for new records (JSON literal)' },
            { field: 'placeholder', type: 'text', helpText: 'Placeholder hint' },

            // Text constraints
            { field: 'maxLength', type: 'number', helpText: 'Max characters', visibleOn: "type in ['text','textarea','email','url','phone','password','markdown','html','richtext']" },
            { field: 'minLength', type: 'number', helpText: 'Min characters', visibleOn: "type in ['text','textarea','email','url','phone','password','markdown','html','richtext']" },

            // Numeric constraints
            { field: 'min', type: 'number', helpText: 'Minimum value', visibleOn: "type in ['number','currency','percent','rating','slider','progress']" },
            { field: 'max', type: 'number', helpText: 'Maximum value', visibleOn: "type in ['number','currency','percent','rating','slider','progress']" },
            { field: 'precision', type: 'number', helpText: 'Total digits', visibleOn: "type in ['number','currency','percent']" },
            { field: 'scale', type: 'number', helpText: 'Decimal places', visibleOn: "type in ['number','currency','percent']" },

            // Selection options
            {
              field: 'options',
              type: 'repeater',
              helpText: 'Available choices',
              visibleOn: "type in ['select','multiselect','radio','checkboxes']",
              fields: [
                { field: 'label', type: 'text', required: true },
                { field: 'value', type: 'text', required: true },
                { field: 'color', type: 'color' },
                { field: 'icon', type: 'text', helpText: 'Lucide icon name' },
                { field: 'description', type: 'text' },
              ],
            },

            // Relational
            { field: 'reference', type: 'text', helpText: 'Target object name', visibleOn: "type in ['lookup','master_detail','tree']" },
            { field: 'referenceFilter', type: 'code', language: 'expression', helpText: 'CEL filter applied to the picker', visibleOn: "type in ['lookup','master_detail']" },
            { field: 'cascadeDelete', type: 'boolean', helpText: 'Delete children when parent is deleted', visibleOn: "type == 'master_detail'" },
            { field: 'multiple', type: 'boolean', helpText: 'Allow selecting multiple records', visibleOn: "type in ['lookup']" },

            // Formula / summary
            { field: 'formula', type: 'code', language: 'expression', helpText: 'CEL formula expression', visibleOn: "type == 'formula'" },
            { field: 'returnType', type: 'select', helpText: 'Result type for formulas', visibleOn: "type == 'formula'", options: [
              { label: 'Text', value: 'text' }, { label: 'Number', value: 'number' }, { label: 'Boolean', value: 'boolean' },
              { label: 'Date', value: 'date' }, { label: 'Datetime', value: 'datetime' }, { label: 'Currency', value: 'currency' },
            ] },
            { field: 'summaryType', type: 'select', helpText: 'Aggregation', visibleOn: "type == 'summary'", options: [
              { label: 'Count', value: 'count' }, { label: 'Sum', value: 'sum' }, { label: 'Avg', value: 'avg' },
              { label: 'Min', value: 'min' }, { label: 'Max', value: 'max' },
            ] },
            { field: 'summaryField', type: 'text', helpText: 'Field on child object to aggregate', visibleOn: "type == 'summary'" },

            // Autonumber
            { field: 'displayFormat', type: 'text', helpText: 'e.g. "INV-{0000}"', visibleOn: "type == 'autonumber'" },
            { field: 'startingNumber', type: 'number', helpText: 'Starting sequence value', visibleOn: "type == 'autonumber'" },

            // Code language
            { field: 'language', type: 'text', helpText: 'Editor language (e.g. sql, javascript)', visibleOn: "type == 'code'" },

            // Validation / governance
            { field: 'validation', type: 'code', language: 'expression', helpText: 'CEL predicate — must evaluate true' },
            { field: 'errorMessage', type: 'text', helpText: 'Shown when validation fails' },
            { field: 'audit', type: 'boolean', helpText: 'Audit changes to this field' },
            { field: 'trackHistory', type: 'boolean', helpText: 'Keep change history' },
            { field: 'pii', type: 'boolean', helpText: 'Personally identifiable information' },
            { field: 'encrypted', type: 'boolean', helpText: 'Encrypt at rest' },
          ],
        },
      ],
    },
    {
      name: 'capabilities',
      label: 'Capabilities',
      description: 'System features and API exposure.',
      collapsible: true,
      collapsed: true,
      fields: [
        {
          field: 'capabilities',
          type: 'composite',
          helpText: 'Enable/disable system features',
          fields: [
            { field: 'trackHistory', type: 'boolean' },
            { field: 'searchable', type: 'boolean' },
            { field: 'apiEnabled', type: 'boolean' },
            { field: 'files', type: 'boolean' },
            { field: 'feeds', type: 'boolean' },
            { field: 'activities', type: 'boolean' },
            { field: 'trash', type: 'boolean' },
            { field: 'mru', type: 'boolean' },
            { field: 'clone', type: 'boolean' },
          ],
        },
      ],
    },
    {
      name: 'advanced',
      label: 'Advanced',
      description: 'State machines, actions, and storage.',
      collapsible: true,
      collapsed: true,
      fields: [
        { field: 'datasource', type: 'text', helpText: 'Target datasource ID (default: "default")' },
      ],
    },
  ],
});
