/// Providers: Design Request
///
/// Riverpod providers for managing design request state.
library;

import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:worker/core/network/dio_client.dart';
import 'package:worker/features/showrooms/data/datasources/design_request_remote_datasource.dart';
import 'package:worker/features/showrooms/data/repositories/design_request_repository_impl.dart';
import 'package:worker/features/showrooms/domain/entities/design_request.dart';
import 'package:worker/features/showrooms/domain/repositories/design_request_repository.dart';

part 'design_request_provider.g.dart';

/// Design Request Remote Data Source Provider
@riverpod
Future<DesignRequestRemoteDataSource> designRequestRemoteDataSource(Ref ref) async {
  final dioClient = await ref.watch(dioClientProvider.future);
  return DesignRequestRemoteDataSourceImpl(dioClient);
}

/// Design Request Repository Provider
@riverpod
Future<DesignRequestRepository> designRequestRepository(Ref ref) async {
  final remoteDataSource = await ref.watch(designRequestRemoteDataSourceProvider.future);
  return DesignRequestRepositoryImpl(remoteDataSource);
}

/// Design Requests List Provider
///
/// Fetches and manages design requests from API.
@riverpod
class DesignRequestsList extends _$DesignRequestsList {
  @override
  Future<List<DesignRequest>> build() async {
    final repository = await ref.watch(designRequestRepositoryProvider.future);
    return repository.getDesignRequests();
  }

  /// Refresh design requests from remote
  Future<void> refresh() async {
    state = const AsyncValue.loading();
    state = await AsyncValue.guard(() async {
      final repository = await ref.read(designRequestRepositoryProvider.future);
      return repository.getDesignRequests();
    });
  }
}

/// Design Request Detail Provider
///
/// Fetches detail of a specific design request by name.
/// Uses family modifier to cache by request name.
@riverpod
Future<DesignRequest> designRequestDetail(Ref ref, String name) async {
  final repository = await ref.watch(designRequestRepositoryProvider.future);
  return repository.getDesignRequestDetail(name);
}
