import { ThriveIcon } from '@/components/icons'
import { AuthMode, type BlockConfig, type BlockMeta, IntegrationType } from '@/blocks/types'

const PAGINATED_OPS = [
  'search_users',
  'list_audiences',
  'list_audience_members',
  'list_assignments',
  'list_enrolments',
  'list_completions',
  'query_content',
  'query_activities',
  'query_cpd_categories',
  'query_cpd_entries',
  'query_cpd_requirements',
  'query_cpd_user_summaries',
  'list_tags',
]

const UPDATED_SINCE_OPS = [
  'search_users',
  'list_audiences',
  'list_assignments',
  'query_content',
  'query_cpd_categories',
  'query_cpd_requirements',
  'list_tags',
]

const REF_OPS = ['create_user', 'update_user', 'delete_user', 'suspend_user', 'get_user_by_ref']
const ID_OPS = ['get_user_by_id', 'get_completion', 'get_content', 'get_activity']
const USER_FIELD_OPS = ['create_user', 'update_user']

const AUDIENCE_ID_REQUIRED_OPS = [
  'get_audience',
  'update_audience',
  'delete_audience',
  'list_audience_members',
  'add_audience_members',
  'replace_audience_members',
  'remove_audience_member',
  'list_audience_managers',
  'add_audience_managers',
  'replace_audience_managers',
  'remove_audience_manager',
  'create_assignment',
  'update_assignment',
  'delete_assignment',
]
const AUDIENCE_ID_OPS = [...AUDIENCE_ID_REQUIRED_OPS, 'list_assignments']

const ASSIGNMENT_ID_OPS = [
  'get_assignment',
  'update_assignment',
  'delete_assignment',
  'list_enrolments',
  'get_enrolment',
]

const USER_ID_REQUIRED_OPS = [
  'remove_audience_manager',
  'add_user_tags',
  'remove_user_tags',
  'update_user_skills',
  'create_completion',
]
const USER_ID_OPS = [...USER_ID_REQUIRED_OPS, 'list_completions']

export const ThriveBlock: BlockConfig = {
  type: 'thrive',
  name: 'Thrive',
  description: 'Manage users, audiences, learning and CPD on Thrive',
  longDescription:
    'Integrate Thrive Learning into the workflow. Manage user lifecycle, audiences and their members and managers, content assignments and enrolments, learning completions, content and activity records, CPD, tags, and skills.',
  docsLink: 'https://docs.sim.ai/tools/thrive',
  category: 'tools',
  integrationType: IntegrationType.HR,
  bgColor: '#FFFFFF',
  icon: ThriveIcon,
  authMode: AuthMode.ApiKey,

  subBlocks: [
    {
      id: 'operation',
      title: 'Operation',
      type: 'dropdown',
      options: [
        { label: 'Create User', id: 'create_user' },
        { label: 'Update User', id: 'update_user' },
        { label: 'Delete User', id: 'delete_user' },
        { label: 'Suspend User', id: 'suspend_user' },
        { label: 'Search Users', id: 'search_users' },
        { label: 'Get User by ID', id: 'get_user_by_id' },
        { label: 'Get User by Ref', id: 'get_user_by_ref' },
        { label: 'List Audiences', id: 'list_audiences' },
        { label: 'Create Audience', id: 'create_audience' },
        { label: 'Get Audience', id: 'get_audience' },
        { label: 'Update Audience', id: 'update_audience' },
        { label: 'Delete Audience', id: 'delete_audience' },
        { label: 'List Audience Members', id: 'list_audience_members' },
        { label: 'Add Audience Members', id: 'add_audience_members' },
        { label: 'Replace Audience Members', id: 'replace_audience_members' },
        { label: 'Remove Audience Member', id: 'remove_audience_member' },
        { label: 'List Audience Managers', id: 'list_audience_managers' },
        { label: 'Add Audience Managers', id: 'add_audience_managers' },
        { label: 'Replace Audience Managers', id: 'replace_audience_managers' },
        { label: 'Remove Audience Manager', id: 'remove_audience_manager' },
        { label: 'List Assignments', id: 'list_assignments' },
        { label: 'Create Assignment', id: 'create_assignment' },
        { label: 'Get Assignment', id: 'get_assignment' },
        { label: 'Update Assignment', id: 'update_assignment' },
        { label: 'Delete Assignment', id: 'delete_assignment' },
        { label: 'List Enrolments', id: 'list_enrolments' },
        { label: 'Get Enrolment', id: 'get_enrolment' },
        { label: 'List Completions', id: 'list_completions' },
        { label: 'Get Completion', id: 'get_completion' },
        { label: 'Create Completion', id: 'create_completion' },
        { label: 'Query Content', id: 'query_content' },
        { label: 'Get Content', id: 'get_content' },
        { label: 'Query Activities', id: 'query_activities' },
        { label: 'Get Activity', id: 'get_activity' },
        { label: 'Query CPD Categories', id: 'query_cpd_categories' },
        { label: 'Get CPD Category', id: 'get_cpd_category' },
        { label: 'Query CPD Entries', id: 'query_cpd_entries' },
        { label: 'Get CPD Entry', id: 'get_cpd_entry' },
        { label: 'Query CPD Requirements', id: 'query_cpd_requirements' },
        { label: 'Get CPD Requirement', id: 'get_cpd_requirement' },
        { label: 'Query CPD User Summaries', id: 'query_cpd_user_summaries' },
        { label: 'List Tags', id: 'list_tags' },
        { label: 'Get Tag', id: 'get_tag' },
        { label: 'Add User Tags', id: 'add_user_tags' },
        { label: 'Remove User Tags', id: 'remove_user_tags' },
        { label: 'Update User Skills', id: 'update_user_skills' },
        { label: 'Get Skill Levels', id: 'get_skill_levels' },
      ],
      value: () => 'search_users',
    },
    {
      id: 'tenantId',
      title: 'Tenant ID',
      type: 'short-input',
      placeholder: 'Enter your Thrive Tenant ID',
      required: true,
    },
    {
      id: 'apiKey',
      title: 'API Key',
      type: 'short-input',
      placeholder: 'Enter your Thrive API key',
      password: true,
      required: true,
    },
    {
      id: 'host',
      title: 'Region',
      type: 'dropdown',
      options: [
        { label: 'Production', id: 'public.api.learn.link' },
        { label: 'Production (MEA)', id: 'public.api.mea.learn.tech' },
        { label: 'Staging', id: 'public.api.learnstaging.link' },
        { label: 'Staging (MEA)', id: 'public.api.meastaging.learn.tech' },
      ],
      value: () => 'public.api.learn.link',
    },

    {
      id: 'ref',
      title: 'User Ref',
      type: 'short-input',
      placeholder: "Your organisation's unique identifier for the user",
      condition: { field: 'operation', value: REF_OPS },
      required: { field: 'operation', value: REF_OPS },
    },
    {
      id: 'id',
      title: 'Record ID',
      type: 'short-input',
      placeholder: 'The ID of the user, completion, content, or activity record',
      condition: { field: 'operation', value: ID_OPS },
      required: { field: 'operation', value: ID_OPS },
    },
    {
      id: 'firstName',
      title: 'First Name',
      type: 'short-input',
      placeholder: 'The given name of the individual',
      condition: { field: 'operation', value: USER_FIELD_OPS },
      required: { field: 'operation', value: 'create_user' },
    },
    {
      id: 'lastName',
      title: 'Last Name',
      type: 'short-input',
      placeholder: 'The family name of the individual',
      condition: { field: 'operation', value: USER_FIELD_OPS },
      required: { field: 'operation', value: 'create_user' },
    },
    {
      id: 'email',
      title: 'Email',
      type: 'short-input',
      placeholder: "The user's email address (required unless login method is 'ref')",
      condition: { field: 'operation', value: USER_FIELD_OPS },
    },
    {
      id: 'loginMethod',
      title: 'Login Method',
      type: 'dropdown',
      options: [
        { label: 'Email', id: 'email' },
        { label: 'Ref', id: 'ref' },
      ],
      condition: { field: 'operation', value: USER_FIELD_OPS },
      mode: 'advanced',
    },
    {
      id: 'role',
      title: 'Role',
      type: 'dropdown',
      options: [
        { label: 'Learner', id: 'learner' },
        { label: 'Learner Admin', id: 'learneradmin' },
        { label: 'Administrator', id: 'administrator' },
      ],
      condition: { field: 'operation', value: USER_FIELD_OPS },
      mode: 'advanced',
    },
    {
      id: 'jobTitle',
      title: 'Job Title',
      type: 'short-input',
      placeholder: "Name of this individual's role in your organisation",
      condition: { field: 'operation', value: USER_FIELD_OPS },
      mode: 'advanced',
    },
    {
      id: 'managerRef',
      title: 'Manager Ref',
      type: 'short-input',
      placeholder: "The line manager's unique identifier",
      condition: { field: 'operation', value: USER_FIELD_OPS },
      mode: 'advanced',
    },
    {
      id: 'startDate',
      title: 'Start Date',
      type: 'short-input',
      placeholder: 'Date the individual started (ISO 8601)',
      condition: { field: 'operation', value: USER_FIELD_OPS },
      mode: 'advanced',
      wandConfig: {
        enabled: true,
        prompt: 'Generate an ISO 8601 timestamp. Return ONLY the timestamp string.',
        generationType: 'json-object',
      },
    },
    {
      id: 'endDate',
      title: 'End Date',
      type: 'short-input',
      placeholder: 'Date the individual left (ISO 8601)',
      condition: { field: 'operation', value: [...USER_FIELD_OPS, 'suspend_user'] },
      mode: 'advanced',
      wandConfig: {
        enabled: true,
        prompt: 'Generate an ISO 8601 timestamp. Return ONLY the timestamp string.',
        generationType: 'json-object',
      },
    },
    {
      id: 'timeZone',
      title: 'Time Zone',
      type: 'short-input',
      placeholder: "The user's preferred timezone",
      condition: { field: 'operation', value: USER_FIELD_OPS },
      mode: 'advanced',
    },
    {
      id: 'languageCode',
      title: 'Language Code',
      type: 'short-input',
      placeholder: "The user's preferred language (e.g. en-gb)",
      condition: { field: 'operation', value: USER_FIELD_OPS },
      mode: 'advanced',
    },
    {
      id: 'sso',
      title: 'SSO Managed',
      type: 'switch',
      condition: { field: 'operation', value: USER_FIELD_OPS },
      mode: 'advanced',
    },
    {
      id: 'domain',
      title: 'Domain',
      type: 'short-input',
      placeholder: 'Domain this individual is associated with',
      condition: { field: 'operation', value: USER_FIELD_OPS },
      mode: 'advanced',
    },
    {
      id: 'additionalFields',
      title: 'Additional Fields',
      type: 'long-input',
      placeholder: '{"department":"Sales"}',
      condition: { field: 'operation', value: USER_FIELD_OPS },
      mode: 'advanced',
      wandConfig: {
        enabled: true,
        prompt:
          'Generate a JSON object of custom field key-value pairs for a Thrive user. Return ONLY the JSON object.',
        generationType: 'json-object',
      },
    },

    {
      id: 'statuses',
      title: 'Statuses',
      type: 'short-input',
      placeholder: 'Comma-separated: active, inactive, expired, new',
      condition: { field: 'operation', value: 'search_users' },
      mode: 'advanced',
    },
    {
      id: 'omitStatuses',
      title: 'Omit Statuses',
      type: 'short-input',
      placeholder: 'Comma-separated statuses to exclude',
      condition: { field: 'operation', value: 'search_users' },
      mode: 'advanced',
    },
    {
      id: 'userStatus',
      title: 'Status',
      type: 'dropdown',
      options: [
        { label: 'Active', id: 'active' },
        { label: 'Inactive', id: 'inactive' },
        { label: 'Expired', id: 'expired' },
        { label: 'New', id: 'new' },
      ],
      condition: { field: 'operation', value: 'search_users' },
      mode: 'advanced',
    },
    {
      id: 'enrolmentStatus',
      title: 'Status',
      type: 'dropdown',
      options: [
        { label: 'Archived', id: 'archived' },
        { label: 'Complete', id: 'complete' },
        { label: 'Open', id: 'open' },
        { label: 'Overdue', id: 'overdue' },
        { label: 'Scheduled', id: 'scheduled' },
        { label: 'Unassigned', id: 'unassigned' },
      ],
      condition: { field: 'operation', value: 'list_enrolments' },
      mode: 'advanced',
    },

    {
      id: 'audienceId',
      title: 'Audience ID',
      type: 'short-input',
      placeholder: 'The audience ID or reference',
      condition: { field: 'operation', value: AUDIENCE_ID_OPS },
      required: { field: 'operation', value: AUDIENCE_ID_REQUIRED_OPS },
    },
    {
      id: 'name',
      title: 'Audience Name',
      type: 'short-input',
      placeholder: 'The name of the audience',
      condition: { field: 'operation', value: ['create_audience', 'update_audience'] },
    },
    {
      id: 'reference',
      title: 'Audience Reference',
      type: 'short-input',
      placeholder: 'The external reference for the audience',
      condition: { field: 'operation', value: ['create_audience', 'update_audience'] },
      mode: 'advanced',
    },
    {
      id: 'parentId',
      title: 'Parent ID',
      type: 'short-input',
      placeholder: 'The id/reference of the parent audience or structure',
      condition: { field: 'operation', value: ['create_audience', 'update_audience'] },
      mode: 'advanced',
    },
    {
      id: 'category',
      title: 'Category',
      type: 'dropdown',
      options: [
        { label: 'Audience', id: 'audience' },
        { label: 'Structure', id: 'structure' },
      ],
      condition: { field: 'operation', value: 'create_audience' },
      mode: 'advanced',
    },
    {
      id: 'users',
      title: 'Users',
      type: 'long-input',
      placeholder: '["user@example.com", "user-ref-2"]',
      condition: {
        field: 'operation',
        value: ['add_audience_members', 'replace_audience_members'],
      },
      required: {
        field: 'operation',
        value: ['add_audience_members', 'replace_audience_members'],
      },
      wandConfig: {
        enabled: true,
        prompt:
          'Generate a JSON array of user emails, refs, or ids (1-100 items). Return ONLY the JSON array.',
        generationType: 'json-object',
      },
    },
    {
      id: 'userRef',
      title: 'User',
      type: 'short-input',
      placeholder: "The member's email, ref, or id to remove",
      condition: { field: 'operation', value: 'remove_audience_member' },
      required: { field: 'operation', value: 'remove_audience_member' },
    },
    {
      id: 'managers',
      title: 'Managers',
      type: 'long-input',
      placeholder:
        '[{"reference":"user@example.com","permissions":{"audienceManager":{"manageContent":true,"assignments":true},"peopleManager":{"canViewLearnPage":true,"insights":false,"manage":false}}}]',
      condition: {
        field: 'operation',
        value: ['add_audience_managers', 'replace_audience_managers'],
      },
      required: {
        field: 'operation',
        value: ['add_audience_managers', 'replace_audience_managers'],
      },
      wandConfig: {
        enabled: true,
        prompt:
          'Generate a JSON array of Thrive audience manager objects (1-100). Each has "reference" (email or ref) and "permissions" with audienceManager {manageContent, assignments}, peopleManager {canViewLearnPage, insights, manage}, and optionally administrator {canAddAudienceManagers}. Return ONLY the JSON array.',
        generationType: 'json-object',
      },
    },

    {
      id: 'assignmentId',
      title: 'Assignment ID',
      type: 'short-input',
      placeholder: 'The assignment ID',
      condition: { field: 'operation', value: ASSIGNMENT_ID_OPS },
      required: { field: 'operation', value: ASSIGNMENT_ID_OPS },
    },
    {
      id: 'enrolmentId',
      title: 'Enrolment ID',
      type: 'short-input',
      placeholder: 'The enrolment ID',
      condition: { field: 'operation', value: 'get_enrolment' },
      required: { field: 'operation', value: 'get_enrolment' },
    },
    {
      id: 'contentId',
      title: 'Content ID',
      type: 'short-input',
      placeholder: 'The content ID',
      condition: {
        field: 'operation',
        value: ['create_assignment', 'update_assignment', 'list_completions', 'create_completion'],
      },
      required: { field: 'operation', value: ['create_assignment', 'create_completion'] },
    },
    {
      id: 'alternativeContentIds',
      title: 'Alternative Content IDs',
      type: 'long-input',
      placeholder: '["63f57d805d8ecbf46f5b5278"]',
      condition: { field: 'operation', value: ['create_assignment', 'update_assignment'] },
      mode: 'advanced',
      wandConfig: {
        enabled: true,
        prompt:
          'Generate a JSON array of content IDs that can also complete the assignment. Return ONLY the JSON array.',
        generationType: 'json-object',
      },
    },
    {
      id: 'hideAlternativeContent',
      title: 'Hide Alternative Content',
      type: 'switch',
      condition: { field: 'operation', value: 'create_assignment' },
      mode: 'advanced',
    },
    {
      id: 'completionPeriod',
      title: 'Completion Period (days)',
      type: 'short-input',
      placeholder: 'Number of days required to complete (default 30)',
      condition: { field: 'operation', value: ['create_assignment', 'update_assignment'] },
      mode: 'advanced',
    },
    {
      id: 'recurrence',
      title: 'Recurrence (days)',
      type: 'short-input',
      placeholder: 'Number of days until the assignment reoccurs',
      condition: { field: 'operation', value: ['create_assignment', 'update_assignment'] },
      mode: 'advanced',
    },

    {
      id: 'userId',
      title: 'User ID',
      type: 'short-input',
      placeholder: 'The user ID',
      condition: { field: 'operation', value: USER_ID_OPS },
      required: { field: 'operation', value: USER_ID_REQUIRED_OPS },
    },
    {
      id: 'isRPL',
      title: 'Imported via RPL',
      type: 'switch',
      condition: { field: 'operation', value: 'list_completions' },
      mode: 'advanced',
    },
    {
      id: 'completedDateRangeStart',
      title: 'Completed From',
      type: 'short-input',
      placeholder: 'Filter completions on/after this date (e.g. 2024-04-02)',
      condition: { field: 'operation', value: 'list_completions' },
      mode: 'advanced',
    },
    {
      id: 'completedDateRangeEnd',
      title: 'Completed To',
      type: 'short-input',
      placeholder: 'Filter completions on/before this date (e.g. 2024-04-30)',
      condition: { field: 'operation', value: 'list_completions' },
      mode: 'advanced',
    },
    {
      id: 'completedAt',
      title: 'Completed At',
      type: 'short-input',
      placeholder: 'ISO 8601 timestamp when the completion occurred',
      condition: { field: 'operation', value: 'create_completion' },
      required: { field: 'operation', value: 'create_completion' },
      wandConfig: {
        enabled: true,
        prompt: 'Generate an ISO 8601 timestamp. Return ONLY the timestamp string.',
        generationType: 'json-object',
      },
    },

    {
      id: 'types',
      title: 'Types',
      type: 'short-input',
      placeholder: 'Comma-separated content types (e.g. pathway,event)',
      condition: { field: 'operation', value: 'query_content' },
      mode: 'advanced',
    },
    {
      id: 'omitTypes',
      title: 'Omit Types',
      type: 'short-input',
      placeholder: 'Comma-separated content types to exclude',
      condition: { field: 'operation', value: 'query_content' },
      mode: 'advanced',
    },

    {
      id: 'actions',
      title: 'Actions',
      type: 'short-input',
      placeholder: 'Comma-separated activity types (e.g. viewed,completed)',
      condition: { field: 'operation', value: 'query_activities' },
      mode: 'advanced',
    },
    {
      id: 'omitActions',
      title: 'Omit Actions',
      type: 'short-input',
      placeholder: 'Comma-separated activity types to exclude',
      condition: { field: 'operation', value: 'query_activities' },
      mode: 'advanced',
    },
    {
      id: 'contentIds',
      title: 'Content IDs',
      type: 'short-input',
      placeholder: 'Comma-separated content IDs',
      condition: { field: 'operation', value: 'query_activities' },
      mode: 'advanced',
    },
    {
      id: 'contentType',
      title: 'Content Type',
      type: 'short-input',
      placeholder: 'Filter by content type (e.g. content)',
      condition: { field: 'operation', value: 'query_activities' },
      mode: 'advanced',
    },
    {
      id: 'timestampFrom',
      title: 'Timestamp From',
      type: 'short-input',
      placeholder: 'YYYY-MM-DD hh:mm:ss',
      condition: { field: 'operation', value: 'query_activities' },
      mode: 'advanced',
    },
    {
      id: 'timestampTo',
      title: 'Timestamp To',
      type: 'short-input',
      placeholder: 'YYYY-MM-DD hh:mm:ss',
      condition: { field: 'operation', value: 'query_activities' },
      mode: 'advanced',
    },

    {
      id: 'categoryId',
      title: 'CPD Category ID',
      type: 'short-input',
      placeholder: 'The CPD category ID',
      condition: { field: 'operation', value: 'get_cpd_category' },
      required: { field: 'operation', value: 'get_cpd_category' },
    },
    {
      id: 'logEntryId',
      title: 'CPD Entry ID',
      type: 'short-input',
      placeholder: 'The CPD log entry ID',
      condition: { field: 'operation', value: 'get_cpd_entry' },
      required: { field: 'operation', value: 'get_cpd_entry' },
    },
    {
      id: 'audienceRequirementId',
      title: 'CPD Requirement ID',
      type: 'short-input',
      placeholder: 'The CPD requirement ID',
      condition: { field: 'operation', value: 'get_cpd_requirement' },
      required: { field: 'operation', value: 'get_cpd_requirement' },
    },
    {
      id: 'entryDateFrom',
      title: 'Entry Date From',
      type: 'short-input',
      placeholder: 'Entries: YYYY-MM-DD hh:mm:ss · Summaries: YYYY-MM-DDThh:mm:ss',
      condition: { field: 'operation', value: ['query_cpd_entries', 'query_cpd_user_summaries'] },
      required: { field: 'operation', value: 'query_cpd_user_summaries' },
    },
    {
      id: 'entryDateTo',
      title: 'Entry Date To',
      type: 'short-input',
      placeholder: 'Entries: YYYY-MM-DD hh:mm:ss · Summaries: YYYY-MM-DDThh:mm:ss',
      condition: { field: 'operation', value: ['query_cpd_entries', 'query_cpd_user_summaries'] },
      required: { field: 'operation', value: 'query_cpd_user_summaries' },
    },
    {
      id: 'userIds',
      title: 'User IDs',
      type: 'short-input',
      placeholder: 'Comma-separated user IDs to filter by',
      condition: { field: 'operation', value: 'query_cpd_user_summaries' },
      mode: 'advanced',
    },

    {
      id: 'tagId',
      title: 'Tag ID',
      type: 'short-input',
      placeholder: 'The tag ID',
      condition: { field: 'operation', value: 'get_tag' },
      required: { field: 'operation', value: 'get_tag' },
    },
    {
      id: 'tags',
      title: 'Tags',
      type: 'long-input',
      placeholder: '["leadership", "communication"]',
      condition: { field: 'operation', value: ['add_user_tags', 'remove_user_tags'] },
      required: { field: 'operation', value: ['add_user_tags', 'remove_user_tags'] },
      wandConfig: {
        enabled: true,
        prompt: 'Generate a JSON array of tag names (1-100). Return ONLY the JSON array.',
        generationType: 'json-object',
      },
    },
    {
      id: 'skills',
      title: 'Skills',
      type: 'long-input',
      placeholder: '[{"tagName":"leadership","level":1,"targetLevel":3}]',
      condition: { field: 'operation', value: 'update_user_skills' },
      required: { field: 'operation', value: 'update_user_skills' },
      wandConfig: {
        enabled: true,
        prompt:
          'Generate a JSON array of skill objects (1-100). Each has "tagName" and optionally "level" and "targetLevel" (numbers, min -1). Return ONLY the JSON array.',
        generationType: 'json-object',
      },
    },

    {
      id: 'apiControlled',
      title: 'API Controlled Only',
      type: 'switch',
      condition: { field: 'operation', value: 'list_audiences' },
      mode: 'advanced',
    },
    {
      id: 'updatedAtFrom',
      title: 'Updated From',
      type: 'short-input',
      placeholder: 'Filter enrolments updated on/after this date (ISO 8601)',
      condition: { field: 'operation', value: 'list_enrolments' },
      mode: 'advanced',
    },
    {
      id: 'updatedAtTo',
      title: 'Updated To',
      type: 'short-input',
      placeholder: 'Filter enrolments updated on/before this date (ISO 8601)',
      condition: { field: 'operation', value: 'list_enrolments' },
      mode: 'advanced',
    },
    {
      id: 'updatedSince',
      title: 'Updated Since',
      type: 'short-input',
      placeholder: 'Return only items updated on/after this date/time (ISO 8601)',
      condition: { field: 'operation', value: UPDATED_SINCE_OPS },
      mode: 'advanced',
    },
    {
      id: 'page',
      title: 'Page',
      type: 'short-input',
      placeholder: 'Page number (default 1)',
      condition: { field: 'operation', value: PAGINATED_OPS },
      mode: 'advanced',
    },
    {
      id: 'perPage',
      title: 'Per Page',
      type: 'short-input',
      placeholder: 'Results per page',
      condition: { field: 'operation', value: PAGINATED_OPS },
      mode: 'advanced',
    },
  ],

  tools: {
    access: [
      'thrive_create_user',
      'thrive_update_user',
      'thrive_delete_user',
      'thrive_suspend_user',
      'thrive_search_users',
      'thrive_get_user_by_id',
      'thrive_get_user_by_ref',
      'thrive_list_audiences',
      'thrive_create_audience',
      'thrive_get_audience',
      'thrive_update_audience',
      'thrive_delete_audience',
      'thrive_list_audience_members',
      'thrive_add_audience_members',
      'thrive_replace_audience_members',
      'thrive_remove_audience_member',
      'thrive_list_audience_managers',
      'thrive_add_audience_managers',
      'thrive_replace_audience_managers',
      'thrive_remove_audience_manager',
      'thrive_list_assignments',
      'thrive_create_assignment',
      'thrive_get_assignment',
      'thrive_update_assignment',
      'thrive_delete_assignment',
      'thrive_list_enrolments',
      'thrive_get_enrolment',
      'thrive_list_completions',
      'thrive_get_completion',
      'thrive_create_completion',
      'thrive_get_content',
      'thrive_query_content',
      'thrive_get_activity',
      'thrive_query_activities',
      'thrive_get_cpd_category',
      'thrive_query_cpd_categories',
      'thrive_get_cpd_entry',
      'thrive_query_cpd_entries',
      'thrive_get_cpd_requirement',
      'thrive_query_cpd_requirements',
      'thrive_query_cpd_user_summaries',
      'thrive_list_tags',
      'thrive_get_tag',
      'thrive_add_user_tags',
      'thrive_remove_user_tags',
      'thrive_update_user_skills',
      'thrive_get_skill_levels',
    ],
    config: {
      tool: (params) => `thrive_${params.operation}`,
      params: (params) => {
        const result: Record<string, unknown> = {}
        const numberFields = ['page', 'perPage', 'completionPeriod', 'recurrence']
        for (const field of numberFields) {
          const value = params[field]
          if (value !== undefined && value !== '') result[field] = Number(value)
        }
        const booleanFields = ['sso', 'apiControlled', 'hideAlternativeContent', 'isRPL']
        for (const field of booleanFields) {
          const value = params[field]
          if (value !== undefined && value !== '') {
            result[field] = value === true || value === 'true'
          }
        }
        const status = params.userStatus || params.enrolmentStatus
        if (status !== undefined && status !== '') result.status = status
        return result
      },
    },
  },

  inputs: {
    operation: { type: 'string', description: 'Operation to perform' },
    tenantId: { type: 'string', description: 'Thrive Tenant ID (Basic auth username)' },
    apiKey: { type: 'string', description: 'Thrive API key (Basic auth password)' },
    host: { type: 'string', description: 'Region-specific API host' },
    ref: { type: 'string', description: 'The user ref' },
    id: { type: 'string', description: 'The ID of the record' },
    firstName: { type: 'string', description: 'The given name of the individual' },
    lastName: { type: 'string', description: 'The family name of the individual' },
    email: { type: 'string', description: 'The email address for the user' },
    loginMethod: { type: 'string', description: 'How the user logs in (email or ref)' },
    role: { type: 'string', description: 'Role assigned to the user' },
    jobTitle: { type: 'string', description: "Name of the individual's role" },
    managerRef: { type: 'string', description: "The line manager's ref" },
    startDate: { type: 'string', description: 'Date the individual started (ISO 8601)' },
    endDate: { type: 'string', description: 'Date the individual left (ISO 8601)' },
    timeZone: { type: 'string', description: "The user's preferred timezone" },
    languageCode: { type: 'string', description: "The user's preferred language" },
    sso: { type: 'boolean', description: 'Whether the account is managed by an auth provider' },
    domain: { type: 'string', description: 'Domain the individual is associated with' },
    additionalFields: {
      type: 'string',
      description: 'JSON object of custom field key-value pairs',
    },
    statuses: { type: 'string', description: 'Comma-separated user statuses to include' },
    omitStatuses: { type: 'string', description: 'Comma-separated user statuses to exclude' },
    userStatus: { type: 'string', description: 'Filter users by status' },
    enrolmentStatus: { type: 'string', description: 'Filter enrolments by status' },
    audienceId: { type: 'string', description: 'The audience ID or reference' },
    name: { type: 'string', description: 'The name of the audience' },
    reference: { type: 'string', description: 'The external reference for the audience' },
    parentId: {
      type: 'string',
      description: 'The id/reference of the parent audience or structure',
    },
    category: { type: 'string', description: 'Audience category (audience or structure)' },
    users: { type: 'string', description: 'JSON array of user emails, refs, or ids' },
    userRef: { type: 'string', description: "The member's email, ref, or id" },
    managers: { type: 'string', description: 'JSON array of audience manager objects' },
    assignmentId: { type: 'string', description: 'The assignment ID' },
    enrolmentId: { type: 'string', description: 'The enrolment ID' },
    contentId: { type: 'string', description: 'The content ID' },
    alternativeContentIds: { type: 'string', description: 'JSON array of alternative content IDs' },
    hideAlternativeContent: { type: 'boolean', description: 'Whether to hide alternative content' },
    completionPeriod: { type: 'number', description: 'Days required to complete the assignment' },
    recurrence: { type: 'number', description: 'Days until the assignment reoccurs' },
    userId: { type: 'string', description: 'The user ID' },
    isRPL: { type: 'boolean', description: 'Filter by completions imported via RPL' },
    completedDateRangeStart: {
      type: 'string',
      description: 'Filter completions on/after this date',
    },
    completedDateRangeEnd: {
      type: 'string',
      description: 'Filter completions on/before this date',
    },
    completedAt: { type: 'string', description: 'ISO 8601 timestamp when the completion occurred' },
    types: { type: 'string', description: 'Comma-separated content types to include' },
    omitTypes: { type: 'string', description: 'Comma-separated content types to exclude' },
    actions: { type: 'string', description: 'Comma-separated activity types to include' },
    omitActions: { type: 'string', description: 'Comma-separated activity types to exclude' },
    contentIds: { type: 'string', description: 'Comma-separated content IDs' },
    contentType: { type: 'string', description: 'Filter by content type' },
    timestampFrom: { type: 'string', description: 'Filter activities after this timestamp' },
    timestampTo: { type: 'string', description: 'Filter activities before this timestamp' },
    categoryId: { type: 'string', description: 'The CPD category ID' },
    logEntryId: { type: 'string', description: 'The CPD log entry ID' },
    audienceRequirementId: { type: 'string', description: 'The CPD requirement ID' },
    entryDateFrom: { type: 'string', description: 'Filter CPD entries after this date' },
    entryDateTo: { type: 'string', description: 'Filter CPD entries before this date' },
    userIds: { type: 'string', description: 'Comma-separated user IDs to filter by' },
    tagId: { type: 'string', description: 'The tag ID' },
    tags: { type: 'string', description: 'JSON array of tag names' },
    skills: { type: 'string', description: 'JSON array of skill objects' },
    apiControlled: { type: 'boolean', description: 'Filter to API-controlled audiences only' },
    updatedAtFrom: { type: 'string', description: 'Filter enrolments updated on/after this date' },
    updatedAtTo: { type: 'string', description: 'Filter enrolments updated on/before this date' },
    updatedSince: { type: 'string', description: 'Return items updated on/after this date/time' },
    page: { type: 'number', description: 'Page number for pagination' },
    perPage: { type: 'number', description: 'Number of results per page' },
  },

  outputs: {
    user: { type: 'json', description: 'A user record' },
    results: { type: 'json', description: 'A page of results' },
    pagination: { type: 'json', description: 'Pagination details' },
    success: { type: 'boolean', description: 'Whether a delete operation succeeded' },
    result: { type: 'json', description: 'Bulk add/replace result with successes and failures' },
    audience: { type: 'json', description: 'An audience record' },
    managers: { type: 'json', description: 'A list of audience managers' },
    assignment: { type: 'json', description: 'An assignment record' },
    assignments: { type: 'json', description: 'A list of assignments' },
    enrolment: { type: 'json', description: 'An enrolment record' },
    enrolments: { type: 'json', description: 'A list of enrolments' },
    completion: { type: 'json', description: 'A completion record' },
    completions: { type: 'json', description: 'A list of completions' },
    statementId: { type: 'string', description: 'The completion statement ID' },
    content: { type: 'json', description: 'A content record' },
    activity: { type: 'json', description: 'An activity record' },
    category: { type: 'json', description: 'A CPD category record' },
    entry: { type: 'json', description: 'A CPD entry record' },
    requirement: { type: 'json', description: 'A CPD requirement record' },
    tag: { type: 'json', description: 'A tag record' },
    levels: { type: 'json', description: 'The available skill levels' },
    status: { type: 'number', description: 'The status code of a tag/skill mutation' },
    message: { type: 'string', description: 'A human-readable result message' },
  },
}

export const ThriveBlockMeta = {
  tags: ['content-management', 'knowledge-base', 'automation'],
  url: 'https://thrivelearning.com',
  templates: [
    {
      icon: ThriveIcon,
      title: 'Onboard new hires into Thrive',
      prompt:
        'Build a workflow that, when a new employee row is added to a Google Sheet, creates the user in Thrive with their ref, name, email, job title, and manager, then adds them to the relevant onboarding audience.',
      modules: ['agent', 'workflows'],
      category: 'operations',
      tags: ['hr', 'onboarding', 'automation'],
      alsoIntegrations: ['google_sheets'],
    },
    {
      icon: ThriveIcon,
      title: 'Compliance completion digest',
      prompt:
        'Create a scheduled weekly workflow that lists Thrive enrolments for a compliance assignment, filters those still open or overdue, and posts a Slack summary to the people-ops channel.',
      modules: ['scheduled', 'agent', 'workflows'],
      category: 'operations',
      tags: ['compliance', 'reporting'],
      alsoIntegrations: ['slack'],
    },
    {
      icon: ThriveIcon,
      title: 'Sync leavers from HRIS',
      prompt:
        'Build a workflow that reads terminated employees from an HRIS export and suspends each matching user in Thrive with their end date, then logs the result to a table.',
      modules: ['tables', 'agent', 'workflows'],
      category: 'operations',
      tags: ['hr', 'lifecycle', 'automation'],
    },
    {
      icon: ThriveIcon,
      title: 'Import historical completions',
      prompt:
        'Create a workflow that reads a CSV of prior learning records and creates a completion in Thrive for each user and content item with the completion date.',
      modules: ['files', 'agent', 'workflows'],
      category: 'operations',
      tags: ['migration', 'learning'],
    },
    {
      icon: ThriveIcon,
      title: 'Assign mandatory training to an audience',
      prompt:
        'Build a workflow that creates a Thrive content assignment for a chosen audience and primary content, sets a 30-day completion period, and reports how many learners were enrolled.',
      modules: ['agent', 'workflows'],
      category: 'operations',
      tags: ['compliance', 'learning'],
    },
    {
      icon: ThriveIcon,
      title: 'CPD shortfall report',
      prompt:
        'Create a scheduled monthly workflow that queries Thrive CPD user summaries for a date range, compares logged minutes against each audience CPD requirement, and emails managers a list of learners below target.',
      modules: ['scheduled', 'agent', 'workflows'],
      category: 'operations',
      tags: ['cpd', 'reporting'],
      alsoIntegrations: ['gmail'],
    },
    {
      icon: ThriveIcon,
      title: 'Tag learners by skill',
      prompt:
        'Build a workflow that searches Thrive users by status, then adds skill tags and updates skill levels for each learner based on a mapping in a spreadsheet.',
      modules: ['agent', 'workflows'],
      category: 'operations',
      tags: ['skills', 'automation'],
      alsoIntegrations: ['google_sheets'],
    },
    {
      icon: ThriveIcon,
      title: 'Trending content to Slack',
      prompt:
        'Create a scheduled workflow that queries recently updated Thrive content and activity records, summarises the most engaged-with learning, and posts a weekly highlight to Slack.',
      modules: ['scheduled', 'agent', 'workflows'],
      category: 'marketing',
      tags: ['content', 'engagement'],
      alsoIntegrations: ['slack'],
    },
  ],
} as const satisfies BlockMeta
