import 'package:hooks_riverpod/hooks_riverpod.dart';

import '../data/mock_news_repository.dart';
import '../data/news_models.dart';

final newsControllerProvider = StateNotifierProvider<NewsController, NewsState>(
  (ref) => NewsController(ref.watch(mockNewsRepositoryProvider)),
);

class NewsState {
  const NewsState({required this.items, required this.refreshedAt});

  final List<NewsItem> items;
  final DateTime refreshedAt;
}

class NewsController extends StateNotifier<NewsState> {
  NewsController(this._repository)
    : super(
        NewsState(
          items: _repository.loadFeed(),
          refreshedAt: DateTime(2026, 6, 18, 16, 11),
        ),
      );

  final MockNewsRepository _repository;

  void refresh() {
    state = NewsState(
      items: _repository.loadFeed(),
      refreshedAt: DateTime.now(),
    );
  }
}
