import { IActionExecution } from '@actions/execution/types'

import type z from 'zod'
// import { DataService } from '@data/data.service'
// import { BulkMutation } from '@data/types'

/**
 * Preparation types contain only the information that is needed to prepare the migration.
 * E.g. Excel migrations need the filename of the Excel file, but not the full data.
 */
export type ActionPreparation_Seed =
  | ActionPreparation_Seed_Seed
  | ActionPreparation_Seed_Excel
  | ActionPreparation_Seed_Custom

export type ActionPreparation_Seed_Seed = Action_Seed_Data

export type ActionPreparation_Seed_Excel = {
  type: 'excel'
  name: string
  order: number
  filename: string
  // decoder: z.ZodSchema<BulkMutation[], any, any>
  decoder: z.ZodSchema<any[], any, any>
}

export type ActionPreparation_Seed_Custom = Action_Seed_CustomLogic & {
  handler: ({
    action,
    execution,
    data,
  }: {
    action: Action_Seed_CustomLogic
    execution: IActionExecution
    data: any //DataService
  }) => Promise<void>
}

export type Action_Seed = Action_Seed_Data | Action_Seed_Excel | Action_Seed_ExcelError | Action_Seed_CustomLogic

/**
 * The Result map for all seed actions.
 *
 * Used to determine the return type of any seed action
 */
export type ActionResult_Seed = {
  seed: {
    // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
    data: void
    // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
    excel: void
    // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
    'custom-logic': void
  }
}

// Shared base type, used by all migration actions
type Action_Seed_Shared = {
  scope: 'seed'
  order: number
  name: string
}

export type Action_Seed_Data = Action_Seed_Shared & {
  type: 'data'
  payload: any //BulkMutation[]
}

export type Action_Seed_Excel = Action_Seed_Shared & {
  type: 'excel'
  payload: {
    filename: string
    data: any //BulkMutation[]
  }
}

export type ExcelErrorTypes = 'xlPortError' | 'decoderError'

export type Action_Seed_ExcelError = Action_Seed_Shared & {
  type: 'excel-error'
  payload: { filename: string; errorType: ExcelErrorTypes; data?: any; error: any }
}

/**
 * Triggers custom seed logic (to be defined by the project)
 */
export type Action_Seed_CustomLogic = Action_Seed_Shared & {
  type: 'custom'
  payload: unknown // To be defined based on the custom logic
}
