import 'dart:async';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../../../core/network/dio_client.dart';
import '../../../../core/network/websocket_client.dart';
import '../../../auth/data/providers/auth_provider.dart';
import '../../data/datasources/chat_local_datasource.dart';
import '../../data/datasources/chat_remote_datasource.dart';
import '../../data/models/chat_message_model.dart';
import '../../data/repositories/chat_repository_impl.dart';
import '../../../../core/errors/error_handler.dart';
import '../../domain/entities/chat_message.dart';
import '../../domain/entities/stream_event.dart';
import '../../domain/repositories/chat_repository.dart';
import '../../domain/usecases/cancel_task.dart';
import '../../domain/usecases/get_session_history.dart';
import '../../domain/usecases/send_message.dart';

// ---------------------------------------------------------------------------
// Dependency providers
// ---------------------------------------------------------------------------

final sharedPreferencesProvider = FutureProvider<SharedPreferences>((ref) {
  return SharedPreferences.getInstance();
});

final chatRemoteDatasourceProvider = Provider<ChatRemoteDatasource>((ref) {
  final dio = ref.watch(dioClientProvider);
  return ChatRemoteDatasource(dio);
});

final chatLocalDatasourceProvider = Provider<ChatLocalDatasource?>((ref) {
  final prefsAsync = ref.watch(sharedPreferencesProvider);
  return prefsAsync.whenOrNull(
    data: (prefs) => ChatLocalDatasource(prefs),
  );
});

final chatRepositoryProvider = Provider<ChatRepository>((ref) {
  final remote = ref.watch(chatRemoteDatasourceProvider);
  final local = ref.watch(chatLocalDatasourceProvider);
  final ws = ref.watch(webSocketClientProvider);
  final storage = ref.watch(secureStorageProvider);

  // Use a no-op local datasource if SharedPreferences is not yet ready
  return ChatRepositoryImpl(
    remoteDatasource: remote,
    localDatasource: local ?? _NoOpLocalDatasource(),
    webSocketClient: ws,
    getAccessToken: () => storage.read(key: 'access_token'),
  );
});

// ---------------------------------------------------------------------------
// Use case providers
// ---------------------------------------------------------------------------

final sendMessageUseCaseProvider = Provider<SendMessage>((ref) {
  return SendMessage(ref.watch(chatRepositoryProvider));
});

final getSessionHistoryUseCaseProvider = Provider<GetSessionHistory>((ref) {
  return GetSessionHistory(ref.watch(chatRepositoryProvider));
});

final cancelTaskUseCaseProvider = Provider<CancelTask>((ref) {
  return CancelTask(ref.watch(chatRepositoryProvider));
});

// ---------------------------------------------------------------------------
// Chat state
// ---------------------------------------------------------------------------

enum AgentStatus { idle, thinking, executing, awaitingApproval, error }

class ChatState {
  final List<ChatMessage> messages;
  final AgentStatus agentStatus;
  final String? sessionId;
  final String? error;

  const ChatState({
    this.messages = const [],
    this.agentStatus = AgentStatus.idle,
    this.sessionId,
    this.error,
  });

  bool get isStreaming => agentStatus != AgentStatus.idle && agentStatus != AgentStatus.error;

  ChatState copyWith({
    List<ChatMessage>? messages,
    AgentStatus? agentStatus,
    String? sessionId,
    String? error,
  }) {
    return ChatState(
      messages: messages ?? this.messages,
      agentStatus: agentStatus ?? this.agentStatus,
      sessionId: sessionId ?? this.sessionId,
      error: error,
    );
  }
}

// ---------------------------------------------------------------------------
// Chat notifier
// ---------------------------------------------------------------------------

class ChatNotifier extends StateNotifier<ChatState> {
  final Ref _ref;
  StreamSubscription<StreamEvent>? _eventSubscription;

  ChatNotifier(this._ref) : super(const ChatState());

  /// Sends a user message to the agent and processes the streamed response.
  Future<void> sendMessage(String prompt) async {
    if (prompt.trim().isEmpty) return;

    // Add the user message locally
    final userMsg = ChatMessage(
      id: DateTime.now().microsecondsSinceEpoch.toString(),
      role: MessageRole.user,
      content: prompt,
      timestamp: DateTime.now(),
      type: MessageType.text,
    );

    state = state.copyWith(
      messages: [...state.messages, userMsg],
      agentStatus: AgentStatus.thinking,
      error: null,
    );

    try {
      final useCase = _ref.read(sendMessageUseCaseProvider);
      final sessionId = state.sessionId ?? 'new';

      final stream = useCase.execute(
        sessionId: sessionId,
        message: prompt,
      );

      _eventSubscription?.cancel();
      _eventSubscription = stream.listen(
        (event) => _handleStreamEvent(event),
        onError: (error) {
          state = state.copyWith(
            agentStatus: AgentStatus.error,
            error: ErrorHandler.friendlyMessage(error),
          );
        },
        onDone: () {
          if (state.agentStatus != AgentStatus.error) {
            state = state.copyWith(agentStatus: AgentStatus.idle);
          }
        },
      );
    } catch (e) {
      state = state.copyWith(
        agentStatus: AgentStatus.error,
        error: ErrorHandler.friendlyMessage(e),
      );
    }
  }

  void _handleStreamEvent(StreamEvent event) {
    switch (event) {
      case ThinkingEvent(:final content):
        _appendOrUpdateAssistantMessage(content, MessageType.thinking);
        state = state.copyWith(agentStatus: AgentStatus.thinking);

      case TextEvent(:final content):
        _appendOrUpdateAssistantMessage(content, MessageType.text);
        state = state.copyWith(agentStatus: AgentStatus.executing);

      case ToolUseEvent(:final toolName, :final input):
        final msg = ChatMessage(
          id: DateTime.now().microsecondsSinceEpoch.toString(),
          role: MessageRole.assistant,
          content: '执行: $toolName',
          timestamp: DateTime.now(),
          type: MessageType.toolUse,
          toolExecution: ToolExecution(
            toolName: toolName,
            input: input.toString(),
            riskLevel: 0,
            status: ToolStatus.executing,
          ),
        );
        state = state.copyWith(
          messages: [...state.messages, msg],
          agentStatus: AgentStatus.executing,
        );

      case ToolResultEvent(:final toolName, :final output, :final isError):
        // First, update the matching ToolUse message's status so its spinner
        // transitions to a completed/error icon in the timeline.
        final updatedMessages = [...state.messages];
        for (int i = updatedMessages.length - 1; i >= 0; i--) {
          final m = updatedMessages[i];
          if (m.type == MessageType.toolUse &&
              m.toolExecution?.status == ToolStatus.executing) {
            updatedMessages[i] = m.copyWith(
              toolExecution: m.toolExecution!.copyWith(
                status: isError ? ToolStatus.error : ToolStatus.completed,
              ),
            );
            break;
          }
        }

        final msg = ChatMessage(
          id: DateTime.now().microsecondsSinceEpoch.toString(),
          role: MessageRole.assistant,
          content: output,
          timestamp: DateTime.now(),
          type: MessageType.toolResult,
          toolExecution: ToolExecution(
            toolName: toolName,
            input: '',
            output: output,
            riskLevel: 0,
            status: isError ? ToolStatus.error : ToolStatus.completed,
          ),
        );
        state = state.copyWith(messages: [...updatedMessages, msg]);

      case ApprovalRequiredEvent(:final taskId, :final command, :final riskLevel):
        final msg = ChatMessage(
          id: DateTime.now().microsecondsSinceEpoch.toString(),
          role: MessageRole.assistant,
          content: '需要审批: $command',
          timestamp: DateTime.now(),
          type: MessageType.approval,
          approvalRequest: ApprovalRequest(
            taskId: taskId,
            command: command,
            riskLevel: riskLevel,
            expiresAt: DateTime.now().add(const Duration(minutes: 5)),
          ),
        );
        state = state.copyWith(
          messages: [...state.messages, msg],
          agentStatus: AgentStatus.awaitingApproval,
        );

      case CompletedEvent(:final summary):
        // Only show summary as a message if there were no assistant text messages
        // (avoids duplicate bubble when the SDK already streamed the full response)
        final hasAssistantText = state.messages.any(
          (m) => m.role == MessageRole.assistant && m.type == MessageType.text && m.content.isNotEmpty,
        );
        if (summary.isNotEmpty && !hasAssistantText) {
          _appendOrUpdateAssistantMessage(summary, MessageType.text);
        }
        state = state.copyWith(agentStatus: AgentStatus.idle);

      case ErrorEvent(:final message):
        state = state.copyWith(
          agentStatus: AgentStatus.error,
          error: message,
        );

      case StandingOrderDraftEvent(:final draft):
        final msg = ChatMessage(
          id: DateTime.now().microsecondsSinceEpoch.toString(),
          role: MessageRole.assistant,
          content: '常驻指令草案已生成',
          timestamp: DateTime.now(),
          type: MessageType.standingOrderDraft,
          metadata: draft,
        );
        state = state.copyWith(
          messages: [...state.messages, msg],
          agentStatus: AgentStatus.awaitingApproval,
        );

      case StandingOrderConfirmedEvent(:final orderId, :final orderName):
        final msg = ChatMessage(
          id: DateTime.now().microsecondsSinceEpoch.toString(),
          role: MessageRole.assistant,
          content: '常驻指令「$orderName」已确认 (ID: $orderId)',
          timestamp: DateTime.now(),
          type: MessageType.text,
        );
        state = state.copyWith(
          messages: [...state.messages, msg],
          agentStatus: AgentStatus.idle,
        );
    }
  }

  /// Appends text content to the last assistant message if it is of the same
  /// type, or creates a new message bubble.
  void _appendOrUpdateAssistantMessage(String content, MessageType type) {
    if (state.messages.isNotEmpty) {
      final last = state.messages.last;
      if (last.role == MessageRole.assistant && last.type == type) {
        final updated = last.copyWith(content: last.content + content);
        state = state.copyWith(
          messages: [
            ...state.messages.sublist(0, state.messages.length - 1),
            updated,
          ],
        );
        return;
      }
    }

    final msg = ChatMessage(
      id: DateTime.now().microsecondsSinceEpoch.toString(),
      role: MessageRole.assistant,
      content: content,
      timestamp: DateTime.now(),
      type: type,
      isStreaming: true,
    );
    state = state.copyWith(messages: [...state.messages, msg]);
  }

  /// Approves a pending command.
  Future<void> approveCommand(String taskId) async {
    try {
      final repo = _ref.read(chatRepositoryProvider);
      await repo.approveCommand(taskId);
      state = state.copyWith(agentStatus: AgentStatus.executing);
    } catch (e) {
      state = state.copyWith(error: '审批失败: $e');
    }
  }

  /// Rejects a pending command.
  Future<void> rejectCommand(String taskId, {String? reason}) async {
    try {
      final repo = _ref.read(chatRepositoryProvider);
      await repo.rejectCommand(taskId, reason: reason);
      state = state.copyWith(agentStatus: AgentStatus.idle);
    } catch (e) {
      state = state.copyWith(error: '拒绝失败: $e');
    }
  }

  /// Confirms a standing order draft.
  Future<void> confirmStandingOrder(Map<String, dynamic> draft) async {
    if (state.sessionId == null) return;
    try {
      final repo = _ref.read(chatRepositoryProvider);
      await repo.confirmStandingOrder(state.sessionId!, draft);
      state = state.copyWith(agentStatus: AgentStatus.idle);
    } catch (e) {
      state = state.copyWith(error: '确认常驻指令失败: $e');
    }
  }

  /// Cancels the current agent task.
  Future<void> cancelCurrentTask() async {
    if (state.sessionId == null) return;
    try {
      final useCase = _ref.read(cancelTaskUseCaseProvider);
      await useCase.execute(state.sessionId!);
      _eventSubscription?.cancel();
      state = state.copyWith(agentStatus: AgentStatus.idle);
    } catch (e) {
      state = state.copyWith(error: '取消失败: $e');
    }
  }

  /// Loads session history from the backend.
  Future<void> loadSessionHistory(String sessionId) async {
    try {
      final useCase = _ref.read(getSessionHistoryUseCaseProvider);
      final messages = await useCase.execute(sessionId);
      state = state.copyWith(
        messages: messages,
        sessionId: sessionId,
      );
    } catch (e) {
      state = state.copyWith(error: '加载历史记录失败: $e');
    }
  }

  /// Clears the chat state and cancels any active subscriptions.
  void clearChat() {
    _eventSubscription?.cancel();
    state = const ChatState();
  }

  @override
  void dispose() {
    _eventSubscription?.cancel();
    super.dispose();
  }
}

// ---------------------------------------------------------------------------
// Main providers
// ---------------------------------------------------------------------------

final chatProvider = StateNotifierProvider<ChatNotifier, ChatState>((ref) {
  return ChatNotifier(ref);
});

final agentStatusProvider = Provider<AgentStatus>((ref) {
  return ref.watch(chatProvider).agentStatus;
});

final currentSessionIdProvider = Provider<String?>((ref) {
  return ref.watch(chatProvider).sessionId;
});

final chatMessagesListProvider = Provider<List<ChatMessage>>((ref) {
  return ref.watch(chatProvider).messages;
});

// ---------------------------------------------------------------------------
// No-op local datasource fallback
// ---------------------------------------------------------------------------

class _NoOpLocalDatasource implements ChatLocalDatasource {
  @override
  Future<void> cacheMessages(
    String sessionId,
    List<ChatMessageModel> messages,
  ) async {}

  @override
  List<ChatMessageModel> getCachedMessages(String sessionId) => [];

  @override
  Future<void> clearCache(String sessionId) async {}

  @override
  Future<void> clearAllCaches() async {}
}
