// Bun-compatible decorators (hybrid approach)

/**
 * Handler decorator - marks a class as a handler
 * @param name Handler name for registration
 */
export function Handler(name: string) {
  return function <T extends { new (...args: any[]): {} }>(target: T, _context?: any) {
    // Store handler name on the constructor
    (target as any).__handlerName = name;
    (target as any).__needsAutoRegistration = true;

    return target;
  };
}

/**
 * Operation decorator - marks a method as an operation
 * @param name Operation name
 */
export function Operation(name: string): any {
  return function (target: any, methodName: string, descriptor?: PropertyDescriptor): any {
    // Store metadata directly on the class constructor
    const constructor = target.constructor;

    if (!constructor.__operations) {
      constructor.__operations = [];
    }
    constructor.__operations.push({
      name,
      method: methodName,
    });

    return descriptor;
  };
}

/**
 * Queue schedule decorator - marks an operation as scheduled
 * @param cronPattern Cron pattern for scheduling
 * @param options Additional scheduling options
 */
export function QueueSchedule(
  cronPattern: string,
  options?: {
    priority?: number;
    immediately?: boolean;
    description?: string;
  }
): any {
  return function (target: any, methodName: string, descriptor?: PropertyDescriptor): any {
    // Store metadata directly on the class constructor
    const constructor = target.constructor;

    if (!constructor.__schedules) {
      constructor.__schedules = [];
    }
    constructor.__schedules.push({
      operation: methodName,
      cronPattern,
      ...options,
    });

    return descriptor;
  };
}

/**
 * Disabled decorator - marks a handler as disabled for auto-registration
 * Handlers marked with @Disabled() will be skipped during auto-registration
 */
export function Disabled() {
  return function <T extends { new (...args: any[]): {} }>(target: T, _context?: any) {
    // Store disabled flag on the constructor
    (target as any).__disabled = true;

    return target;
  };
}

/**
 * Combined decorator for scheduled operations
 * Automatically creates both an operation and a schedule
 * @param name Operation name
 * @param cronPattern Cron pattern for scheduling
 * @param options Schedule options
 */
export function ScheduledOperation(
  name: string,
  cronPattern: string,
  options?: {
    priority?: number;
    immediately?: boolean;
    description?: string;
  }
): any {
  return function (target: any, methodName: string, descriptor?: PropertyDescriptor): any {
    // Apply both decorators
    Operation(name)(target, methodName, descriptor);
    QueueSchedule(cronPattern, options)(target, methodName, descriptor);
    return descriptor;
  };
}

// Future event decorators - commented for now
// export function EventListener(eventName: string) {
//   return function (target: any, propertyName: string, descriptor: PropertyDescriptor) {
//     if (!target.constructor.__eventListeners) {
//       target.constructor.__eventListeners = [];
//     }
//     target.constructor.__eventListeners.push({
//       eventName,
//       method: propertyName,
//     });
//     return descriptor;
//   };
// }

// export function EventPublisher(eventName: string) {
//   return function (target: any, propertyName: string, descriptor: PropertyDescriptor) {
//     if (!target.constructor.__eventPublishers) {
//       target.constructor.__eventPublishers = [];
//     }
//     target.constructor.__eventPublishers.push({
//       eventName,
//       method: propertyName,
//     });
//     return descriptor;
//   };
// }
