import { AshbyIcon } from '@/components/icons'
import { AuthMode, type BlockConfig, IntegrationType } from '@/blocks/types'
import { getTrigger } from '@/triggers'

export const AshbyBlock: BlockConfig = {
  type: 'ashby',
  name: 'Ashby',
  description: 'Manage candidates, jobs, and applications in Ashby',
  longDescription:
    'Integrate Ashby into the workflow. Manage candidates (list, get, create, update, search, tag), applications (list, get, create, change stage), jobs (list, get), job postings (list, get), offers (list, get), notes (list, create), interviews (list), and reference data (sources, tags, archive reasons, custom fields, departments, locations, openings, users).',
  docsLink: 'https://docs.sim.ai/tools/ashby',
  category: 'tools',
  integrationType: IntegrationType.HR,
  tags: ['hiring'],
  bgColor: '#5D4ED6',
  icon: AshbyIcon,
  authMode: AuthMode.ApiKey,

  triggers: {
    enabled: true,
    available: [
      'ashby_application_submit',
      'ashby_candidate_stage_change',
      'ashby_candidate_hire',
      'ashby_candidate_delete',
      'ashby_job_create',
      'ashby_offer_create',
    ],
  },

  subBlocks: [
    {
      id: 'operation',
      title: 'Operation',
      type: 'dropdown',
      options: [
        { label: 'List Candidates', id: 'list_candidates' },
        { label: 'Get Candidate', id: 'get_candidate' },
        { label: 'Create Candidate', id: 'create_candidate' },
        { label: 'Update Candidate', id: 'update_candidate' },
        { label: 'Search Candidates', id: 'search_candidates' },
        { label: 'List Jobs', id: 'list_jobs' },
        { label: 'Get Job', id: 'get_job' },
        { label: 'Create Note', id: 'create_note' },
        { label: 'List Notes', id: 'list_notes' },
        { label: 'List Applications', id: 'list_applications' },
        { label: 'Get Application', id: 'get_application' },
        { label: 'Create Application', id: 'create_application' },
        { label: 'List Offers', id: 'list_offers' },
        { label: 'Change Application Stage', id: 'change_application_stage' },
        { label: 'Add Candidate Tag', id: 'add_candidate_tag' },
        { label: 'Remove Candidate Tag', id: 'remove_candidate_tag' },
        { label: 'Get Offer', id: 'get_offer' },
        { label: 'List Sources', id: 'list_sources' },
        { label: 'List Candidate Tags', id: 'list_candidate_tags' },
        { label: 'List Archive Reasons', id: 'list_archive_reasons' },
        { label: 'List Custom Fields', id: 'list_custom_fields' },
        { label: 'List Departments', id: 'list_departments' },
        { label: 'List Locations', id: 'list_locations' },
        { label: 'List Job Postings', id: 'list_job_postings' },
        { label: 'Get Job Posting', id: 'get_job_posting' },
        { label: 'List Openings', id: 'list_openings' },
        { label: 'List Users', id: 'list_users' },
        { label: 'List Interviews', id: 'list_interviews' },
      ],
      value: () => 'list_candidates',
    },
    {
      id: 'apiKey',
      title: 'API Key',
      type: 'short-input',
      required: true,
      placeholder: 'Enter your Ashby API key',
      password: true,
    },
    {
      id: 'candidateId',
      title: 'Candidate ID',
      type: 'short-input',
      required: {
        field: 'operation',
        value: [
          'get_candidate',
          'create_note',
          'list_notes',
          'update_candidate',
          'add_candidate_tag',
          'remove_candidate_tag',
        ],
      },
      placeholder: 'Enter candidate UUID',
      condition: {
        field: 'operation',
        value: [
          'get_candidate',
          'create_note',
          'list_notes',
          'update_candidate',
          'add_candidate_tag',
          'remove_candidate_tag',
        ],
      },
    },
    {
      id: 'name',
      title: 'Name',
      type: 'short-input',
      required: { field: 'operation', value: 'create_candidate' },
      placeholder: 'Full name (e.g. Jane Smith)',
      condition: { field: 'operation', value: 'create_candidate' },
    },
    {
      id: 'email',
      title: 'Email',
      type: 'short-input',
      required: { field: 'operation', value: 'create_candidate' },
      placeholder: 'Email address',
      condition: { field: 'operation', value: ['create_candidate', 'update_candidate'] },
    },
    {
      id: 'phoneNumber',
      title: 'Phone Number',
      type: 'short-input',
      placeholder: 'Phone number',
      condition: { field: 'operation', value: ['create_candidate', 'update_candidate'] },
      mode: 'advanced',
    },
    {
      id: 'linkedInUrl',
      title: 'LinkedIn URL',
      type: 'short-input',
      placeholder: 'https://linkedin.com/in/...',
      condition: { field: 'operation', value: ['create_candidate', 'update_candidate'] },
      mode: 'advanced',
    },
    {
      id: 'githubUrl',
      title: 'GitHub URL',
      type: 'short-input',
      placeholder: 'https://github.com/...',
      condition: { field: 'operation', value: ['create_candidate', 'update_candidate'] },
      mode: 'advanced',
    },
    {
      id: 'sourceId',
      title: 'Source ID',
      type: 'short-input',
      placeholder: 'Source UUID to attribute the candidate to',
      condition: {
        field: 'operation',
        value: ['create_candidate', 'update_candidate', 'create_application'],
      },
      mode: 'advanced',
    },
    {
      id: 'updateName',
      title: 'Name',
      type: 'short-input',
      placeholder: 'Updated full name',
      condition: { field: 'operation', value: 'update_candidate' },
      mode: 'advanced',
    },
    {
      id: 'websiteUrl',
      title: 'Website URL',
      type: 'short-input',
      placeholder: 'https://example.com',
      condition: { field: 'operation', value: 'update_candidate' },
      mode: 'advanced',
    },
    {
      id: 'searchName',
      title: 'Name',
      type: 'short-input',
      placeholder: 'Search by candidate name',
      condition: { field: 'operation', value: 'search_candidates' },
    },
    {
      id: 'searchEmail',
      title: 'Email',
      type: 'short-input',
      placeholder: 'Search by candidate email',
      condition: { field: 'operation', value: 'search_candidates' },
    },
    {
      id: 'jobId',
      title: 'Job ID',
      type: 'short-input',
      required: { field: 'operation', value: ['get_job', 'create_application'] },
      placeholder: 'Enter job UUID',
      condition: { field: 'operation', value: ['get_job', 'create_application'] },
    },
    {
      id: 'applicationId',
      title: 'Application ID',
      type: 'short-input',
      required: {
        field: 'operation',
        value: ['get_application', 'change_application_stage'],
      },
      placeholder: 'Enter application UUID',
      condition: {
        field: 'operation',
        value: ['get_application', 'change_application_stage', 'list_interviews'],
      },
    },
    {
      id: 'appCandidateId',
      title: 'Candidate ID',
      type: 'short-input',
      required: { field: 'operation', value: 'create_application' },
      placeholder: 'Enter candidate UUID',
      condition: { field: 'operation', value: 'create_application' },
    },
    {
      id: 'interviewPlanId',
      title: 'Interview Plan ID',
      type: 'short-input',
      placeholder: 'Interview plan UUID (defaults to job default)',
      condition: { field: 'operation', value: 'create_application' },
      mode: 'advanced',
    },
    {
      id: 'interviewStageId',
      title: 'Interview Stage ID',
      type: 'short-input',
      required: { field: 'operation', value: 'change_application_stage' },
      placeholder: 'Interview stage UUID',
      condition: {
        field: 'operation',
        value: ['create_application', 'change_application_stage', 'list_interviews'],
      },
    },
    {
      id: 'creditedToUserId',
      title: 'Credited To User ID',
      type: 'short-input',
      placeholder: 'User UUID the application is credited to',
      condition: { field: 'operation', value: 'create_application' },
      mode: 'advanced',
    },
    {
      id: 'appCreatedAt',
      title: 'Created At',
      type: 'short-input',
      placeholder: 'e.g. 2024-01-01T00:00:00Z',
      condition: { field: 'operation', value: 'create_application' },
      mode: 'advanced',
      wandConfig: {
        enabled: true,
        prompt: `Generate an ISO 8601 timestamp based on the user's description.
Examples:
- "last week" -> One week ago from today at 00:00:00Z
- "January 1st 2024" -> 2024-01-01T00:00:00Z
- "30 days ago" -> 30 days before today at 00:00:00Z
- "start of this month" -> First day of current month at 00:00:00Z
Output only the ISO 8601 timestamp string, nothing else.`,
        generationType: 'timestamp',
      },
    },
    {
      id: 'note',
      title: 'Note',
      type: 'long-input',
      required: { field: 'operation', value: 'create_note' },
      placeholder: 'Enter note content',
      condition: { field: 'operation', value: 'create_note' },
    },
    {
      id: 'noteType',
      title: 'Content Type',
      type: 'dropdown',
      options: [
        { label: 'Plain Text', id: 'text/plain' },
        { label: 'HTML', id: 'text/html' },
      ],
      value: () => 'text/plain',
      condition: { field: 'operation', value: 'create_note' },
      mode: 'advanced',
    },
    {
      id: 'sendNotifications',
      title: 'Send Notifications',
      type: 'switch',
      condition: { field: 'operation', value: 'create_note' },
      mode: 'advanced',
    },
    {
      id: 'filterStatus',
      title: 'Status Filter',
      type: 'dropdown',
      options: [
        { label: 'All', id: '' },
        { label: 'Active', id: 'Active' },
        { label: 'Hired', id: 'Hired' },
        { label: 'Archived', id: 'Archived' },
        { label: 'Lead', id: 'Lead' },
      ],
      value: () => '',
      condition: { field: 'operation', value: 'list_applications' },
      mode: 'advanced',
    },
    {
      id: 'filterJobId',
      title: 'Job ID Filter',
      type: 'short-input',
      placeholder: 'Filter by job UUID',
      condition: { field: 'operation', value: 'list_applications' },
      mode: 'advanced',
    },
    {
      id: 'filterCandidateId',
      title: 'Candidate ID Filter',
      type: 'short-input',
      placeholder: 'Filter by candidate UUID',
      condition: { field: 'operation', value: 'list_applications' },
      mode: 'advanced',
    },
    {
      id: 'createdAfter',
      title: 'Created After',
      type: 'short-input',
      placeholder: 'e.g. 2024-01-01T00:00:00Z',
      condition: { field: 'operation', value: 'list_applications' },
      mode: 'advanced',
      wandConfig: {
        enabled: true,
        prompt: `Generate an ISO 8601 timestamp based on the user's description.
Examples:
- "last week" -> One week ago from today at 00:00:00Z
- "January 1st 2024" -> 2024-01-01T00:00:00Z
- "30 days ago" -> 30 days before today at 00:00:00Z
- "start of this month" -> First day of current month at 00:00:00Z
Output only the ISO 8601 timestamp string, nothing else.`,
        generationType: 'timestamp',
      },
    },
    {
      id: 'jobStatus',
      title: 'Status Filter',
      type: 'dropdown',
      options: [
        { label: 'All', id: '' },
        { label: 'Open', id: 'Open' },
        { label: 'Closed', id: 'Closed' },
        { label: 'Archived', id: 'Archived' },
        { label: 'Draft', id: 'Draft' },
      ],
      value: () => '',
      condition: { field: 'operation', value: 'list_jobs' },
      mode: 'advanced',
    },
    {
      id: 'cursor',
      title: 'Cursor',
      type: 'short-input',
      placeholder: 'Pagination cursor from previous response',
      condition: {
        field: 'operation',
        value: [
          'list_candidates',
          'list_jobs',
          'list_applications',
          'list_notes',
          'list_offers',
          'list_openings',
          'list_users',
          'list_interviews',
        ],
      },
      mode: 'advanced',
    },
    {
      id: 'perPage',
      title: 'Per Page',
      type: 'short-input',
      placeholder: 'Results per page (default 100)',
      condition: {
        field: 'operation',
        value: [
          'list_candidates',
          'list_jobs',
          'list_applications',
          'list_notes',
          'list_offers',
          'list_openings',
          'list_users',
          'list_interviews',
        ],
      },
      mode: 'advanced',
    },
    {
      id: 'tagId',
      title: 'Tag ID',
      type: 'short-input',
      required: {
        field: 'operation',
        value: ['add_candidate_tag', 'remove_candidate_tag'],
      },
      placeholder: 'Enter tag UUID',
      condition: {
        field: 'operation',
        value: ['add_candidate_tag', 'remove_candidate_tag'],
      },
    },
    {
      id: 'archiveReasonId',
      title: 'Archive Reason ID',
      type: 'short-input',
      placeholder: 'Archive reason UUID (required for Archived stages)',
      condition: { field: 'operation', value: 'change_application_stage' },
      mode: 'advanced',
    },
    {
      id: 'offerId',
      title: 'Offer ID',
      type: 'short-input',
      required: { field: 'operation', value: 'get_offer' },
      placeholder: 'Enter offer UUID',
      condition: { field: 'operation', value: 'get_offer' },
    },
    {
      id: 'jobPostingId',
      title: 'Job Posting ID',
      type: 'short-input',
      required: { field: 'operation', value: 'get_job_posting' },
      placeholder: 'Enter job posting UUID',
      condition: { field: 'operation', value: 'get_job_posting' },
    },
    ...getTrigger('ashby_application_submit').subBlocks,
    ...getTrigger('ashby_candidate_stage_change').subBlocks,
    ...getTrigger('ashby_candidate_hire').subBlocks,
    ...getTrigger('ashby_candidate_delete').subBlocks,
    ...getTrigger('ashby_job_create').subBlocks,
    ...getTrigger('ashby_offer_create').subBlocks,
  ],

  tools: {
    access: [
      'ashby_add_candidate_tag',
      'ashby_change_application_stage',
      'ashby_create_application',
      'ashby_create_candidate',
      'ashby_create_note',
      'ashby_get_application',
      'ashby_get_candidate',
      'ashby_get_job',
      'ashby_get_job_posting',
      'ashby_get_offer',
      'ashby_list_applications',
      'ashby_list_archive_reasons',
      'ashby_list_candidate_tags',
      'ashby_list_candidates',
      'ashby_list_custom_fields',
      'ashby_list_departments',
      'ashby_list_interviews',
      'ashby_list_job_postings',
      'ashby_list_jobs',
      'ashby_list_locations',
      'ashby_list_notes',
      'ashby_list_offers',
      'ashby_list_openings',
      'ashby_list_sources',
      'ashby_list_users',
      'ashby_remove_candidate_tag',
      'ashby_search_candidates',
      'ashby_update_candidate',
    ],
    config: {
      tool: (params) => `ashby_${params.operation}`,
      params: (params) => {
        const result: Record<string, unknown> = {}
        if (params.perPage) result.perPage = Number(params.perPage)
        if (params.searchName) result.name = params.searchName
        if (params.searchEmail) result.email = params.searchEmail
        if (params.filterStatus) result.status = params.filterStatus
        if (params.filterJobId) result.jobId = params.filterJobId
        if (params.filterCandidateId) result.candidateId = params.filterCandidateId
        if (params.jobStatus) result.status = params.jobStatus
        if (params.sendNotifications === 'true' || params.sendNotifications === true) {
          result.sendNotifications = true
        }
        if (params.appCandidateId) result.candidateId = params.appCandidateId
        if (params.appCreatedAt) result.createdAt = params.appCreatedAt
        if (params.updateName) result.name = params.updateName
        return result
      },
    },
  },

  inputs: {
    operation: { type: 'string', description: 'Operation to perform' },
    apiKey: { type: 'string', description: 'Ashby API key' },
    candidateId: { type: 'string', description: 'Candidate UUID' },
    name: { type: 'string', description: 'Candidate full name' },
    email: { type: 'string', description: 'Email address' },
    phoneNumber: { type: 'string', description: 'Phone number' },
    linkedInUrl: { type: 'string', description: 'LinkedIn profile URL' },
    githubUrl: { type: 'string', description: 'GitHub profile URL' },
    websiteUrl: { type: 'string', description: 'Personal website URL' },
    sourceId: { type: 'string', description: 'Source UUID' },
    updateName: { type: 'string', description: 'Updated full name' },
    searchName: { type: 'string', description: 'Name to search for' },
    searchEmail: { type: 'string', description: 'Email to search for' },
    jobId: { type: 'string', description: 'Job UUID' },
    applicationId: { type: 'string', description: 'Application UUID' },
    appCandidateId: { type: 'string', description: 'Candidate UUID for application' },
    interviewPlanId: { type: 'string', description: 'Interview plan UUID' },
    interviewStageId: { type: 'string', description: 'Interview stage UUID' },
    creditedToUserId: { type: 'string', description: 'User UUID credited to' },
    appCreatedAt: { type: 'string', description: 'Application creation timestamp' },
    note: { type: 'string', description: 'Note content' },
    noteType: { type: 'string', description: 'Content type (text/plain or text/html)' },
    sendNotifications: { type: 'boolean', description: 'Send notifications' },
    filterStatus: { type: 'string', description: 'Application status filter' },
    filterJobId: { type: 'string', description: 'Job UUID filter' },
    filterCandidateId: { type: 'string', description: 'Candidate UUID filter' },
    createdAfter: { type: 'string', description: 'Filter by creation date' },
    jobStatus: { type: 'string', description: 'Job status filter' },
    cursor: { type: 'string', description: 'Pagination cursor' },
    perPage: { type: 'number', description: 'Results per page' },
    tagId: { type: 'string', description: 'Tag UUID' },
    offerId: { type: 'string', description: 'Offer UUID' },
    jobPostingId: { type: 'string', description: 'Job posting UUID' },
    archiveReasonId: { type: 'string', description: 'Archive reason UUID' },
  },

  outputs: {
    candidates: {
      type: 'json',
      description:
        'List of candidates (id, name, primaryEmailAddress, primaryPhoneNumber, createdAt, updatedAt)',
    },
    jobs: {
      type: 'json',
      description:
        'List of jobs (id, title, status, employmentType, departmentId, locationId, createdAt, updatedAt)',
    },
    applications: {
      type: 'json',
      description:
        'List of applications (id, status, candidate, job, currentInterviewStage, source, createdAt, updatedAt)',
    },
    notes: {
      type: 'json',
      description: 'List of notes (id, content, author, createdAt)',
    },
    offers: {
      type: 'json',
      description:
        'List of offers (id, offerStatus, acceptanceStatus, applicationId, startDate, salary, openingId)',
    },
    archiveReasons: {
      type: 'json',
      description: 'List of archive reasons (id, text, reasonType, isArchived)',
    },
    sources: {
      type: 'json',
      description: 'List of sources (id, title, isArchived)',
    },
    customFields: {
      type: 'json',
      description: 'List of custom fields (id, title, fieldType, objectType, isArchived)',
    },
    departments: {
      type: 'json',
      description: 'List of departments (id, name, isArchived, parentId)',
    },
    locations: {
      type: 'json',
      description: 'List of locations (id, name, isArchived, isRemote, address)',
    },
    jobPostings: {
      type: 'json',
      description:
        'List of job postings (id, title, jobId, locationName, departmentName, employmentType, isListed, publishedDate)',
    },
    openings: {
      type: 'json',
      description: 'List of openings (id, openingState, isArchived, openedAt, closedAt)',
    },
    users: {
      type: 'json',
      description: 'List of users (id, firstName, lastName, email, isEnabled, globalRole)',
    },
    interviewSchedules: {
      type: 'json',
      description:
        'List of interview schedules (id, applicationId, interviewStageId, status, createdAt)',
    },
    tags: {
      type: 'json',
      description: 'List of candidate tags (id, title, isArchived)',
    },
    stageId: { type: 'string', description: 'Interview stage UUID after stage change' },
    success: { type: 'boolean', description: 'Whether the operation succeeded' },
    offerStatus: {
      type: 'string',
      description: 'Offer status (e.g. WaitingOnCandidateResponse, CandidateAccepted)',
    },
    acceptanceStatus: {
      type: 'string',
      description: 'Acceptance status (e.g. Accepted, Declined, Pending)',
    },
    applicationId: { type: 'string', description: 'Associated application UUID' },
    openingId: { type: 'string', description: 'Opening UUID associated with the offer' },
    salary: {
      type: 'json',
      description: 'Salary details from latest version (currencyCode, value)',
    },
    startDate: { type: 'string', description: 'Offer start date from latest version' },
    id: { type: 'string', description: 'Resource UUID' },
    name: { type: 'string', description: 'Resource name' },
    title: { type: 'string', description: 'Job title' },
    status: { type: 'string', description: 'Status' },
    noteId: { type: 'string', description: 'Created note UUID' },
    content: { type: 'string', description: 'Note content' },
    moreDataAvailable: { type: 'boolean', description: 'Whether more pages exist' },
    nextCursor: { type: 'string', description: 'Pagination cursor for next page' },
  },
}
