import { HealthKitRepository } from '../repositories/HealthKitRepository';
import { HealthKitIdentifier } from '../value-objects/HealthKitIdentifier';

export class RequestAuthorizationUseCase {
  constructor(private readonly healthKitRepository: HealthKitRepository) {}

  async execute(): Promise<boolean> {
    try {
      if (!this.healthKitRepository.isAvailable()) {
        throw new Error('HealthKit is not available on this device');
      }

      return await this.healthKitRepository.requestAuthorization();
    } catch (error) {
      throw new Error(`Failed to request authorization: ${error instanceof Error ? error.message : 'Unknown error'}`);
    }
  }

  async executeSelective(identifiers: string[]): Promise<boolean> {
    try {
      if (!this.healthKitRepository.isAvailable()) {
        throw new Error('HealthKit is not available on this device');
      }

      if (!identifiers || identifiers.length === 0) {
        throw new Error('At least one identifier must be provided');
      }

      const healthKitIdentifiers = identifiers.map(id => HealthKitIdentifier.create(id));

      return await this.healthKitRepository.requestSelectiveAuthorization(healthKitIdentifiers);
    } catch (error) {
      throw new Error(`Failed to request selective authorization: ${error instanceof Error ? error.message : 'Unknown error'}`);
    }
  }
} 