import 'package:krow_data_connect/krow_data_connect.dart' as dc;
import 'package:krow_domain/krow_domain.dart';

import '../../domain/repositories/experience_repository_interface.dart';

/// Implementation of [ExperienceRepositoryInterface] that delegates to Data Connect.
class ExperienceRepositoryImpl implements ExperienceRepositoryInterface {
  final dc.DataConnectService _service;

  /// Creates a [ExperienceRepositoryImpl] using Data Connect Service.
  ExperienceRepositoryImpl({
    dc.DataConnectService? service,
  }) : _service = service ?? dc.DataConnectService.instance;

  Future<dc.GetStaffByIdStaff> _getStaff() async {
    final staffId = await _service.getStaffId();

    final result =
        await _service.connector.getStaffById(id: staffId).execute();
    if (result.data.staff == null) {
      throw const ServerException(technicalMessage: 'Staff profile not found');
    }
    return result.data.staff!;
  }

  @override
  Future<List<String>> getIndustries() async {
    return _service.run(() async {
      final staff = await _getStaff();
      return staff.industries ?? [];
    });
  }

  @override
  Future<List<String>> getSkills() async {
    return _service.run(() async {
      final staff = await _getStaff();
      return staff.skills ?? [];
    });
  }

  @override
  Future<void> saveExperience(
    List<String> industries,
    List<String> skills,
  ) async {
    return _service.run(() async {
      final staff = await _getStaff();
      await _service.connector
          .updateStaff(id: staff.id)
          .industries(industries)
          .skills(skills)
          .execute();
    });
  }
}
