import { Action, type ActionSchema } from './Action.js'
import { observed } from '../../decorators/observed.js'

export type DeleteStrategy = 'soft' | 'hard'

/**
 * Enables removing an existing resource.
 * Endpoint: DELETE /[entity-collection-name]/{id}
 */
export interface DeleteActionSchema extends ActionSchema {
  kind: 'delete'
  /**
   * The strategy for deletion. Default: Soft Delete.
   *
   * @default 'soft'
   */
  strategy?: DeleteStrategy
  /**
   * The data retention period (in days) for soft-deleted resources.
   * This defines how long the data should be kept before it is permanently deleted.
   *
   * @default 30
   */
  retentionPeriod?: number
}

/**
 * Enables removing an existing resource.
 * Endpoint: DELETE /[entity-collection-name]/{id}
 */
export class DeleteAction extends Action implements DeleteActionSchema {
  @observed() override accessor kind: 'delete'
  @observed() accessor strategy: DeleteStrategy
  @observed() accessor retentionPeriod: number

  constructor(state: Partial<DeleteActionSchema> = {}) {
    super(state)
    this.kind = 'delete'
    this.strategy = state.strategy || 'soft'
    this.retentionPeriod = state.retentionPeriod ?? 30
  }

  override toJSON(): DeleteActionSchema {
    return {
      ...(super.toJSON() as DeleteActionSchema),
      kind: 'delete',
      strategy: this.strategy,
      retentionPeriod: this.retentionPeriod,
    }
  }

  static isDeleteAction(action: Action): action is DeleteAction {
    return action.kind === 'delete'
  }

  static isDeleteActionSchema(schema: ActionSchema): schema is DeleteActionSchema {
    return schema.kind === 'delete'
  }
}
