import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../core/network/dio_client.dart';
import '../data/models/announcement/announcement.dart';
import '../data/services/announcement_service.dart';

// ── State ──────────────────────────────────────────────────

class AppNotificationsState {
  final List<AnnouncementContent> notifications;
  final bool isLoading;
  final bool hasMore;
  final int currentPage;
  final String? errorMessage;

  const AppNotificationsState({
    this.notifications = const [],
    this.isLoading = false,
    this.hasMore = true,
    this.currentPage = 1,
    this.errorMessage,
  });

  AppNotificationsState copyWith({
    List<AnnouncementContent>? notifications,
    bool? isLoading,
    bool? hasMore,
    int? currentPage,
    String? errorMessage,
  }) =>
      AppNotificationsState(
        notifications: notifications ?? this.notifications,
        isLoading: isLoading ?? this.isLoading,
        hasMore: hasMore ?? this.hasMore,
        currentPage: currentPage ?? this.currentPage,
        errorMessage: errorMessage,
      );
}

// ── Notifier ───────────────────────────────────────────────

class AppNotificationsNotifier
    extends AutoDisposeNotifier<AppNotificationsState> {
  static const _pageSize = 10;

  AnnouncementService get _service =>
      AnnouncementService(ref.read(dioClientProvider));

  @override
  AppNotificationsState build() {
    Future.microtask(_loadFirst);
    return const AppNotificationsState(isLoading: true);
  }

  Future<void> _loadFirst() async {
    try {
      final result = await _service.getAnnouncementPage(
        pageNo: 1,
        pageSize: _pageSize,
      );
      state = state.copyWith(
        notifications: result.content,
        isLoading: false,
        currentPage: 1,
        hasMore: result.content.length >= _pageSize,
      );
    } catch (e) {
      state = state.copyWith(isLoading: false, errorMessage: e.toString());
    }
  }

  /// 下拉刷新
  Future<void> refresh() async {
    state = state.copyWith(isLoading: true, errorMessage: null);
    try {
      final result = await _service.getAnnouncementPage(
        pageNo: 1,
        pageSize: _pageSize,
      );
      state = state.copyWith(
        notifications: result.content,
        isLoading: false,
        currentPage: 1,
        hasMore: result.content.length >= _pageSize,
      );
    } catch (e) {
      state = state.copyWith(isLoading: false, errorMessage: e.toString());
    }
  }

  /// 上拉加载更多
  Future<void> loadMore() async {
    if (state.isLoading || !state.hasMore) return;
    state = state.copyWith(isLoading: true);
    try {
      final nextPage = state.currentPage + 1;
      final result = await _service.getAnnouncementPage(
        pageNo: nextPage,
        pageSize: _pageSize,
      );
      state = state.copyWith(
        notifications: [...state.notifications, ...result.content],
        isLoading: false,
        currentPage: nextPage,
        hasMore: result.content.length >= _pageSize,
      );
    } catch (e) {
      state = state.copyWith(isLoading: false, errorMessage: e.toString());
    }
  }
}

// ── Provider ───────────────────────────────────────────────

final notificationsProvider = AutoDisposeNotifierProvider<
    AppNotificationsNotifier, AppNotificationsState>(
  AppNotificationsNotifier.new,
);
