/// Providers: Sample Project
///
/// Riverpod providers for managing sample/model house projects state.
library;

import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:worker/core/network/dio_client.dart';
import 'package:worker/features/showrooms/data/datasources/sample_project_remote_datasource.dart';
import 'package:worker/features/showrooms/data/repositories/sample_project_repository_impl.dart';
import 'package:worker/features/showrooms/domain/entities/sample_project.dart';
import 'package:worker/features/showrooms/domain/repositories/sample_project_repository.dart';

part 'sample_project_provider.g.dart';

/// Sample Project Remote Data Source Provider
@riverpod
Future<SampleProjectRemoteDataSource> sampleProjectRemoteDataSource(Ref ref) async {
  final dioClient = await ref.watch(dioClientProvider.future);
  return SampleProjectRemoteDataSourceImpl(dioClient);
}

/// Sample Project Repository Provider
@riverpod
Future<SampleProjectRepository> sampleProjectRepository(Ref ref) async {
  final remoteDataSource = await ref.watch(sampleProjectRemoteDataSourceProvider.future);
  return SampleProjectRepositoryImpl(remoteDataSource);
}

/// Sample Projects List Provider
///
/// Fetches and manages sample/model house projects from API.
@riverpod
class SampleProjectsList extends _$SampleProjectsList {
  @override
  Future<List<SampleProject>> build() async {
    final repository = await ref.watch(sampleProjectRepositoryProvider.future);
    return repository.getSampleProjects();
  }

  /// Refresh sample projects from remote
  Future<void> refresh() async {
    state = const AsyncValue.loading();
    state = await AsyncValue.guard(() async {
      final repository = await ref.read(sampleProjectRepositoryProvider.future);
      return repository.getSampleProjects();
    });
  }
}

/// Sample Project Detail Provider
///
/// Fetches detail of a specific sample project by name.
/// Uses family modifier to cache by project name.
@riverpod
Future<SampleProject> sampleProjectDetail(Ref ref, String name) async {
  final repository = await ref.watch(sampleProjectRepositoryProvider.future);
  return repository.getSampleProjectDetail(name);
}
