/**
 * QuickCreate Configuration
 * 
 * This file defines the configuration for quick-creating new records
 * from LinkField dropdowns. Add new doctypes as needed.
 */

export interface QuickCreateFieldConfig {
  fieldname: string;
  label: string;
  fieldtype: 'Data' | 'Text' | 'Select' | 'Link' | 'Check' | 'Int' | 'Float' | 'Date' | 'Datetime';
  required?: boolean;
  placeholder?: string;
  options?: string[] | { label: string; value: string }[]; // For Select fieldtype
  linkDoctype?: string; // For Link fieldtype
  linkFilters?: Record<string, any>; // Filters for Link fieldtype
  defaultValue?: any;
  description?: string;
  hidden?: boolean;
  readOnly?: boolean;
  dependsOn?: string; // Field name this field depends on
}

export interface QuickCreateDoctypeConfig {
  doctype: string;
  title: string; // Display title for the modal
  fields: QuickCreateFieldConfig[];
  titleField?: string; // The main field that represents the record name (defaults to 'name')
  afterCreate?: (newRecord: any) => void; // Callback after successful creation
  validateBeforeCreate?: (data: Record<string, any>) => string | null; // Return error message or null
}

/**
 * Configuration for all doctypes that support quick creation
 * Add new doctypes here as needed
 */
export const QUICK_CREATE_CONFIG: Record<string, QuickCreateDoctypeConfig> = {
  // Location Doctype
  'Location': {
    doctype: 'Location',
    title: 'Create New Location',
    titleField: 'location_name',
    fields: [
      {
        fieldname: 'location_name',
        label: 'Location Name',
        fieldtype: 'Data',
        required: true,
        placeholder: 'Enter location name',
      },
      {
        fieldname: 'parent_location',
        label: 'Parent Location',
        fieldtype: 'Link',
        linkDoctype: 'Location',
        placeholder: 'Select parent location (optional)',
      },
      {
        fieldname: 'is_group',
        label: 'Is Group',
        fieldtype: 'Check',
        defaultValue: 0,
        description: 'Check if this location contains sub-locations',
      },
      {
        fieldname: 'latitude',
        label: 'Latitude',
        fieldtype: 'Float',
        placeholder: 'e.g., 24.7136',
      },
      {
        fieldname: 'longitude',
        label: 'Longitude',
        fieldtype: 'Float',
        placeholder: 'e.g., 46.6753',
      },
    ],
  },

  // Room Doctype
  'Room': {
    doctype: 'Room',
    title: 'Create New Room',
    titleField: 'name',
    fields: [
      {
        fieldname: 'room',
        label: 'Room Name/Number',
        fieldtype: 'Data',
        required: true,
        placeholder: 'Enter room name or number',
      },
      {
        fieldname: 'building',
        label: 'Building',
        fieldtype: 'Link',
        linkDoctype: 'Building',
        placeholder: 'Select building',
      },
      {
        fieldname: 'location_name',
        label: 'Location Name',
        fieldtype: 'Link',
        linkDoctype: 'Location',
        placeholder: 'Select Location',
      },
      {
        fieldname: 'department',
        label: 'Department',
        fieldtype: 'Link',
        linkDoctype: 'Department',
        placeholder: 'Select department',
      },
    ],
  },

  // Building Doctype
  'Building': {
    doctype: 'Building',
    title: 'Create New Building',
    titleField: 'name',
    fields: [
      {
        fieldname: 'building',
        label: 'Building Name',
        fieldtype: 'Data',
        required: true,
        placeholder: 'Enter building name',
      },
    ],
  },

  // Extension Directory Doctype
  'Extension Directory': {
    doctype: 'Extension Directory',
    title: 'Create New Extension',
    titleField: 'name',
    fields: [
      {
        fieldname: 'extension_number',
        label: 'Extension Number',
        fieldtype: 'Data',
        required: true,
        placeholder: 'Enter extension number',
      },
      
    ],
  },

  // Department Doctype
  'Department': {
    doctype: 'Department',
    title: 'Create New Department',
    titleField: 'department_name',
    fields: [
      {
        fieldname: 'department_name',
        label: 'Department Name',
        fieldtype: 'Data',
        required: true,
        placeholder: 'Enter department name',
      },
      {
        fieldname: 'company',
        label: 'Company',
        fieldtype: 'Link',
        linkDoctype: 'Company',
        required: true,
        placeholder: 'Select company',
      },
      {
        fieldname: 'parent_department',
        label: 'Parent Department',
        fieldtype: 'Link',
        linkDoctype: 'Department',
        placeholder: 'Select parent department',
      },
      {
        fieldname: 'is_group',
        label: 'Is Group',
        fieldtype: 'Check',
        defaultValue: 0,
      },
    ],
  },

  // Issue Type (Work Order Type)
  'Issue Type': {
    doctype: 'Issue Type',
    title: 'Create New Issue Type',
    titleField: 'name',
    fields: [
      {
        fieldname: '__newname',
        label: 'Issue Type Name',
        fieldtype: 'Data',
        required: true,
        placeholder: 'Enter issue type name',
      },
      {
        fieldname: 'description',
        label: 'Description',
        fieldtype: 'Text',
        placeholder: 'Enter description',
      },
    ],
  },

  // Manufacturer
  'Manufacturer': {
    doctype: 'Manufacturer',
    title: 'Create New Manufacturer',
    titleField: 'name',
    fields: [
      {
        fieldname: 'short_name',
        label: 'Manufacturer Name',
        fieldtype: 'Data',
        required: true,
        placeholder: 'Enter manufacturer name',
      },
      {
        fieldname: 'full_name',
        label: 'Full Name',
        fieldtype: 'Data',
        placeholder: 'Enter full company name',
      },
      {
        fieldname: 'website',
        label: 'Website',
        fieldtype: 'Data',
        placeholder: 'https://example.com',
      },
      {
        fieldname: 'country',
        label: 'Country',
        fieldtype: 'Link',
        linkDoctype: 'Country',
        placeholder: 'Select country',
      },
    ],
  },

  // Supplier
  'Supplier': {
    doctype: 'Supplier',
    title: 'Create New Supplier',
    titleField: 'supplier_name',
    fields: [
      {
        fieldname: 'supplier_name',
        label: 'Supplier Name',
        fieldtype: 'Data',
        required: true,
        placeholder: 'Enter supplier name',
      },
      {
        fieldname: 'supplier_group',
        label: 'Supplier Group',
        fieldtype: 'Link',
        linkDoctype: 'Supplier Group',
        placeholder: 'Select supplier group',
      },
      {
        fieldname: 'supplier_type',
        label: 'Supplier Type',
        fieldtype: 'Select',
        options: ['Company', 'Individual'],
        defaultValue: 'Company',
      },
      {
        fieldname: 'country',
        label: 'Country',
        fieldtype: 'Link',
        linkDoctype: 'Country',
        placeholder: 'Select country',
      },
    ],
  },

  // Warehouse
  'Warehouse': {
    doctype: 'Warehouse',
    title: 'Create New Warehouse',
    titleField: 'warehouse_name',
    fields: [
      {
        fieldname: 'warehouse_name',
        label: 'Warehouse Name',
        fieldtype: 'Data',
        required: true,
        placeholder: 'Enter warehouse name',
      },
      {
        fieldname: 'company',
        label: 'Company',
        fieldtype: 'Link',
        linkDoctype: 'Company',
        required: true,
        placeholder: 'Select company',
      },
      {
        fieldname: 'parent_warehouse',
        label: 'Parent Warehouse',
        fieldtype: 'Link',
        linkDoctype: 'Warehouse',
        placeholder: 'Select parent warehouse',
      },
      {
        fieldname: 'is_group',
        label: 'Is Group',
        fieldtype: 'Check',
        defaultValue: 0,
      },
    ],
  },

  // Item
  'Item': {
    doctype: 'Item',
    title: 'Create New Item',
    titleField: 'item_name',
    fields: [
      {
        fieldname: 'item_code',
        label: 'Item Code',
        fieldtype: 'Data',
        required: true,
        placeholder: 'Enter item code',
      },
      {
        fieldname: 'item_name',
        label: 'Item Name',
        fieldtype: 'Data',
        required: true,
        placeholder: 'Enter item name',
      },
      {
        fieldname: 'item_group',
        label: 'Item Group',
        fieldtype: 'Link',
        linkDoctype: 'Item Group',
        required: true,
        placeholder: 'Select item group',
      },
      {
        fieldname: 'stock_uom',
        label: 'Default Unit of Measure',
        fieldtype: 'Link',
        linkDoctype: 'UOM',
        required: true,
        defaultValue: 'Nos',
        placeholder: 'Select UOM',
      },
      {
        fieldname: 'is_stock_item',
        label: 'Maintain Stock',
        fieldtype: 'Check',
        defaultValue: 1,
      },
      {
        fieldname: 'description',
        label: 'Description',
        fieldtype: 'Text',
        placeholder: 'Enter item description',
      },
    ],
  },

  // Asset
  'Asset': {
    doctype: 'Asset',
    title: 'Create New Asset',
    titleField: 'asset_name',
    fields: [
      {
        fieldname: 'asset_name',
        label: 'Asset Name',
        fieldtype: 'Data',
        required: true,
        placeholder: 'Enter asset name',
      },
      {
        fieldname: 'item_code',
        label: 'Item Code',
        fieldtype: 'Link',
        linkDoctype: 'Item',
        required: true,
        linkFilters: { is_fixed_asset: 1 },
        placeholder: 'Select item',
      },
      {
        fieldname: 'company',
        label: 'Company',
        fieldtype: 'Link',
        linkDoctype: 'Company',
        required: true,
        placeholder: 'Select company',
      },
      {
        fieldname: 'location',
        label: 'Location',
        fieldtype: 'Link',
        linkDoctype: 'Location',
        placeholder: 'Select location',
      },
      {
        fieldname: 'custodian',
        label: 'Custodian',
        fieldtype: 'Link',
        linkDoctype: 'Employee',
        placeholder: 'Select custodian',
      },
    ],
  },

  // Technical Department
  'Technical Department': {
    doctype: 'Technical Department',
    title: 'Create New Technical Department',
    titleField: 'name',
    fields: [
      {
        fieldname: 'department',
        label: 'Department Name',
        fieldtype: 'Data',
        required: true,
        placeholder: 'Enter technical department name',
      }
    ],
  },
};

/**
 * Helper function to get configuration for a doctype
 */
export const getQuickCreateConfig = (doctype: string): QuickCreateDoctypeConfig | null => {
  return QUICK_CREATE_CONFIG[doctype] || null;
};

/**
 * Check if a doctype supports quick creation
 */
export const supportsQuickCreate = (doctype: string): boolean => {
  return doctype in QUICK_CREATE_CONFIG;
};

/**
 * Add or update a doctype configuration at runtime
 */
export const registerQuickCreateConfig = (config: QuickCreateDoctypeConfig): void => {
  QUICK_CREATE_CONFIG[config.doctype] = config;
};

export default QUICK_CREATE_CONFIG;