import { injectable, unmanaged } from 'inversify';
import { Result } from '@/lib/contracts/Result';
import { DomainError, Service } from '@/lib/contracts/services/Service';
import { AuthService } from './AuthService';
import type { AuthSessionDTO } from '@/lib/types/generated/AuthSessionDTO';
import { SessionViewModel } from '@/lib/view-models/SessionViewModel';

/**
 * Session Service
 *
 * Orchestrates session-related operations.
 * Returns raw API DTOs. No ViewModels or UX logic.
 */
@injectable()
export class SessionService implements Service {
  private authService: AuthService;

  constructor(@unmanaged() apiClient?: any) {
    this.authService = new AuthService(apiClient);
  }

  /**
   * Get current user session
   */
  async getSession(): Promise<Result<SessionViewModel | null, DomainError>> {
    try {
      const res = await this.authService.getSession();
      if (res.isErr()) return Result.err(res.getError());
      
      const data = res.unwrap();
      if (!data || !data.user) return Result.ok(null);
      return Result.ok(new SessionViewModel(data.user));
    } catch (error: unknown) {
      return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to get session' });
    }
  }
}
