import 'package:firebase_auth/firebase_auth.dart';
import 'package:krow_data_connect/krow_data_connect.dart' as dc;
import '../../domain/repositories/experience_repository_interface.dart';

/// Implementation of [ExperienceRepositoryInterface] that delegates to Data Connect.
class ExperienceRepositoryImpl implements ExperienceRepositoryInterface {
  final dc.ExampleConnector _dataConnect;
  // ignore: unused_field
  final FirebaseAuth _firebaseAuth;

  /// Creates a [ExperienceRepositoryImpl] using Da  a Connect and Auth.
  ExperienceRepositoryImpl({
    required dc.ExampleConnector dataConnect,
    required FirebaseAuth firebaseAuth,
  })  : _dataConnect = dataConnect,
        _firebaseAuth = firebaseAuth;

  Future<dc.GetStaffByUserIdStaffs> _getStaff() async {
    final user = _firebaseAuth.currentUser;
    if (user == null) throw Exception('User not authenticated');

    final result =
        await _dataConnect.getStaffByUserId(userId: user.uid).execute();
    if (result.data.staffs.isEmpty) {
      throw Exception('Staff profile not found');
    }
    return result.data.staffs.first;
  }

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

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

  @override
  Future<void> saveExperience(
    List<String> industries,
    List<String> skills,
  ) async {
    try {
    final staff = await _getStaff();
    await _dataConnect
        .updateStaff(id: staff.id)
        .industries(industries)
        .skills(skills)
        .execute();
    } catch (e) {
      throw Exception('Failed to save experience: $e');
    }
  }
}
